diff options
Diffstat (limited to 'spec/support/database')
-rw-r--r-- | spec/support/database/multiple_databases.rb | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/spec/support/database/multiple_databases.rb b/spec/support/database/multiple_databases.rb index 96bdab5171d..25c3b6e74ce 100644 --- a/spec/support/database/multiple_databases.rb +++ b/spec/support/database/multiple_databases.rb @@ -2,6 +2,15 @@ module Database module MultipleDatabases + def run_and_cleanup(example) + # Each example may call `migrate!`, so we must ensure we are migrated down every time + schema_migrate_down! + + example.run + + delete_from_all_tables!(except: deletion_except_tables) + end + def skip_if_multiple_databases_not_setup skip 'Skipping because multiple databases not set up' unless Gitlab::Database.has_config?(:ci) end @@ -22,6 +31,21 @@ module Database model.establish_connection(new_db_config) end + def ensure_schema_and_empty_tables + # Ensure all schemas for both databases are migrated back + Gitlab::Database.database_base_models.each do |_, base_model| + with_reestablished_active_record_base do + reconfigure_db_connection( + model: ActiveRecord::Base, + config_model: base_model + ) + + schema_migrate_up! + delete_from_all_tables!(except: deletion_except_tables) + end + end + end + # The usage of this method switches temporarily used `connection_handler` # allowing full manipulation of ActiveRecord::Base connections without # having side effects like: @@ -109,7 +133,15 @@ RSpec.configure do |config| end end + config.append_after(:context, :migration) do + break if recreate_databases_and_seed_if_needed + + ensure_schema_and_empty_tables + end + config.around(:each, :migration) do |example| + self.class.use_transactional_tests = false + migration_schema = example.metadata[:migration] migration_schema = :gitlab_main if migration_schema == true base_model = Gitlab::Database.schemas_to_base_models.fetch(migration_schema).first @@ -122,11 +154,13 @@ RSpec.configure do |config| config_model: base_model ) - example.run + run_and_cleanup(example) end else - example.run + run_and_cleanup(example) end + + self.class.use_transactional_tests = true end end |