From 68b7f99fa4ec7fe8ab0ef08508899ce4dff9ef13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Tue, 26 Feb 2019 17:55:34 +0100 Subject: Reduce differences in spec/support/db_cleaner.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- spec/support/database_cleaner.rb | 56 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 spec/support/database_cleaner.rb (limited to 'spec/support/database_cleaner.rb') diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000000..edd7de94203 --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require 'database_cleaner/active_record/deletion' +require_relative 'db_cleaner' + +module FakeInformationSchema + # Work around a bug in DatabaseCleaner when using the deletion strategy: + # https://github.com/DatabaseCleaner/database_cleaner/issues/347 + # + # On MySQL, if the information schema is said to exist, we use an inaccurate + # row count leading to some tables not being cleaned when they should + def information_schema_exists?(_connection) + false + end +end + +DatabaseCleaner::ActiveRecord::Deletion.prepend(FakeInformationSchema) + +RSpec.configure do |config| + include DbCleaner + + # Ensure all sequences are reset at the start of the suite run + config.before(:suite) do + setup_database_cleaner + DatabaseCleaner.clean_with(:truncation) + end + + config.append_after(:context) do + DatabaseCleaner.clean_with(:deletion, cache_tables: false) + end + + config.before do + setup_database_cleaner + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, :js) do + DatabaseCleaner.strategy = :deletion, { except: deletion_except_tables, cache_tables: false } + end + + config.before(:each, :delete) do + DatabaseCleaner.strategy = :deletion, { except: deletion_except_tables, cache_tables: false } + end + + config.before(:each, :migration) do + DatabaseCleaner.strategy = :deletion, { cache_tables: false } + end + + config.before do + DatabaseCleaner.start + end + + config.append_after do + DatabaseCleaner.clean + end +end -- cgit v1.2.3