diff --git a/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb b/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb index 1a492b1dd684df8abf98a8dba81555a38bc62cc9..b47a62e59cee27fc7d665fff325f80a4d4b0bfb1 100644 --- a/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb +++ b/spec/support/shared_examples/workers/batched_background_migration_worker_shared_examples.rb @@ -259,7 +259,12 @@ def perform let(:migration_records) { batch_size * number_of_batches } let(:connection) { Gitlab::Database.database_base_models[tracking_database].connection } - let(:example_data) { define_batchable_model(new_table_name, connection: connection) } + + let(:example_data) do + Gitlab::Database::SharedModel.using_connection(connection) do + define_batchable_model(new_table_name, connection: connection) + end + end around do |example| Gitlab::Database::SharedModel.using_connection(connection) do @@ -275,20 +280,24 @@ def perform # Test data should have two records that won't be updated: # - one record beyond the migration's range # - one record that doesn't match the migration job's batch condition - connection.execute(<<~SQL) - CREATE TABLE #{new_table_name} ( - id integer primary key, - some_column integer, - status smallint); - - INSERT INTO #{new_table_name} (id, some_column, status) - SELECT generate_series, generate_series, 1 - FROM generate_series(1, #{migration_records + 1}); - - UPDATE #{new_table_name} - SET status = 0 - WHERE some_column = #{migration_records - 5}; - SQL + Gitlab::Database::SharedModel.using_connection(connection) do + connection.execute("DROP TABLE IF EXISTS #{new_table_name}") + + connection.execute(<<~SQL) + CREATE TABLE #{new_table_name} ( + id integer primary key, + some_column integer, + status smallint); + + INSERT INTO #{new_table_name} (id, some_column, status) + SELECT generate_series, generate_series, 1 + FROM generate_series(1, #{migration_records + 1}); + + UPDATE #{new_table_name} + SET status = 0 + WHERE some_column = #{migration_records - 5}; + SQL + end stub_const('Gitlab::BackgroundMigration::ExampleDataMigration', migration_class) end