diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-19 06:10:36 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-10-19 06:10:36 +0300 |
commit | 53181fac036918912bd271178ec624ee396c21a6 (patch) | |
tree | 94dcf4c53d3fd04d55143fbedcdb522c8817bd48 /spec/support | |
parent | 18e11db5dc1916003766af8a3e1675d2f4713a7a (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/database/multiple_databases.rb | 38 | ||||
-rw-r--r-- | spec/support/database_cleaner.rb | 23 | ||||
-rw-r--r-- | spec/support/db_cleaner.rb | 13 | ||||
-rw-r--r-- | spec/support/migration.rb | 4 |
4 files changed, 49 insertions, 29 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 diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index f8ddf3e66a5..7bd1f0c5dfa 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -13,19 +13,6 @@ RSpec.configure do |config| 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 @@ -35,14 +22,4 @@ RSpec.configure do |config| 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 2a1302f971a..24cdbe04fc2 100644 --- a/spec/support/db_cleaner.rb +++ b/spec/support/db_cleaner.rb @@ -78,6 +78,19 @@ module DbCleaner 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 3c359af886d..24e2fc2ff31 100644 --- a/spec/support/migration.rb +++ b/spec/support/migration.rb @@ -19,13 +19,9 @@ RSpec.configure do |config| # 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 |