diff --git a/spec/migrations/change_public_projects_cost_factor_spec.rb b/spec/migrations/change_public_projects_cost_factor_spec.rb index 039edda750ba9b45cb6f370f2ba3d1f175a5be39..cec0f242b6c9183f972cff6a2489fa4c92ef1dbf 100644 --- a/spec/migrations/change_public_projects_cost_factor_spec.rb +++ b/spec/migrations/change_public_projects_cost_factor_spec.rb @@ -53,6 +53,8 @@ expect(shared_2.public_projects_minutes_cost_factor).to eq(0) expect(shared_3.public_projects_minutes_cost_factor).to eq(1) expect(group_1.public_projects_minutes_cost_factor).to eq(0) + + schema_migrate_up! end end end diff --git a/spec/support/database/multiple_databases.rb b/spec/support/database/multiple_databases.rb index 25c3b6e74ce1be6bb3dc45730c04d2bae0052fe3..96bdab5171d08977b6561fab021b5d0bc411cccf 100644 --- a/spec/support/database/multiple_databases.rb +++ b/spec/support/database/multiple_databases.rb @@ -2,15 +2,6 @@ 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 @@ -31,21 +22,6 @@ def reconfigure_db_connection(name: nil, config_hash: {}, model: ActiveRecord::B 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: @@ -133,15 +109,7 @@ def establish_connection(*args) 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 @@ -154,13 +122,11 @@ def establish_connection(*args) config_model: base_model ) - run_and_cleanup(example) + example.run end else - run_and_cleanup(example) + example.run end - - self.class.use_transactional_tests = true end end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 7bd1f0c5dfa95c307ca4390f44bf2f9218a107c0..f8ddf3e66a52e96ee587cb3e2fd92ade6d07b7aa 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -13,6 +13,19 @@ DatabaseCleaner.clean_with(:deletion) end + config.append_after(:context, :migration) do + delete_from_all_tables!(except: ['work_item_types']) + + # Postgres maximum number of columns in a table is 1600 (https://github.com/postgres/postgres/blob/de41869b64d57160f58852eab20a27f248188135/src/include/access/htup_details.h#L23-L47). + # We drop and recreate the database if any table has more than 1200 columns, just to be safe. + if any_connection_class_with_more_than_allowed_columns? + recreate_all_databases! + + # Seed required data as recreating DBs will delete it + TestEnv.seed_db + end + end + config.around(:each, :delete) do |example| self.class.use_transactional_tests = false @@ -22,4 +35,14 @@ self.class.use_transactional_tests = true end + + config.around(:each, :migration) do |example| + self.class.use_transactional_tests = false + + example.run + + delete_from_all_tables!(except: ['work_item_types']) + + self.class.use_transactional_tests = true + end end diff --git a/spec/support/db_cleaner.rb b/spec/support/db_cleaner.rb index 24cdbe04fc2bc4f31f65d82409e9e89f55697317..2a1302f971a50f3a05a69acbf5d0f32fa83fc2f6 100644 --- a/spec/support/db_cleaner.rb +++ b/spec/support/db_cleaner.rb @@ -78,19 +78,6 @@ def recreate_all_databases! puts "Databases re-creation done in #{Gitlab::Metrics::System.monotonic_time - start}" end - def recreate_databases_and_seed_if_needed - # Postgres maximum number of columns in a table is 1600 (https://github.com/postgres/postgres/blob/de41869b64d57160f58852eab20a27f248188135/src/include/access/htup_details.h#L23-L47). - # We drop and recreate the database if any table has more than 1200 columns, just to be safe. - return false unless any_connection_class_with_more_than_allowed_columns? - - recreate_all_databases! - - # Seed required data as recreating DBs will delete it - TestEnv.seed_db - - true - end - def force_disconnect_all_connections! cmd = <<~SQL SELECT pg_terminate_backend(pg_stat_activity.pid) diff --git a/spec/support/migration.rb b/spec/support/migration.rb index 24e2fc2ff31f48a5c2afe5d34c6abe3ffa5227b9..3c359af886d004936a93218da839864ca2c21a09 100644 --- a/spec/support/migration.rb +++ b/spec/support/migration.rb @@ -19,9 +19,13 @@ # Each example may call `migrate!`, so we must ensure we are migrated down every time config.before(:each, :migration) do use_fake_application_settings + + schema_migrate_down! end config.after(:context, :migration) do + schema_migrate_up! + Gitlab::CurrentSettings.clear_in_memory_application_settings! end end