diff options
Diffstat (limited to 'spec/lib/gitlab/database/migrations')
6 files changed, 110 insertions, 48 deletions
diff --git a/spec/lib/gitlab/database/migrations/background_migration_helpers_spec.rb b/spec/lib/gitlab/database/migrations/background_migration_helpers_spec.rb index c423340a572..f21f1ac5e52 100644 --- a/spec/lib/gitlab/database/migrations/background_migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migrations/background_migration_helpers_spec.rb @@ -37,12 +37,6 @@ RSpec.describe Gitlab::Database::Migrations::BackgroundMigrationHelpers do freeze_time { example.run } end - before do - User.class_eval do - include EachBatch - end - end - it 'returns the final expected delay' do Sidekiq::Testing.fake! do final_delay = model.queue_background_migration_jobs_by_range_at_intervals(User, 'FooJob', 10.minutes, batch_size: 2) diff --git a/spec/lib/gitlab/database/migrations/batched_background_migration_helpers_spec.rb b/spec/lib/gitlab/database/migrations/batched_background_migration_helpers_spec.rb index 5bfb2516ba1..a2f6e6b43ed 100644 --- a/spec/lib/gitlab/database/migrations/batched_background_migration_helpers_spec.rb +++ b/spec/lib/gitlab/database/migrations/batched_background_migration_helpers_spec.rb @@ -15,12 +15,25 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d describe '#queue_batched_background_migration' do let(:pgclass_info) { instance_double('Gitlab::Database::PgClass', cardinality_estimate: 42) } + let(:job_class) do + Class.new(Gitlab::BackgroundMigration::BatchedMigrationJob) do + def self.name + 'MyJobClass' + end + end + end before do allow(Gitlab::Database::PgClass).to receive(:for_table).and_call_original expect(Gitlab::Database::QueryAnalyzers::RestrictAllowedSchemas).to receive(:require_dml_mode!) allow(migration).to receive(:transaction_open?).and_return(false) + + stub_const("Gitlab::Database::BackgroundMigration::BatchedMigration::JOB_CLASS_MODULE", '') + allow_next_instance_of(Gitlab::Database::BackgroundMigration::BatchedMigration) do |batched_migration| + allow(batched_migration).to receive(:job_class) + .and_return(job_class) + end end context 'when such migration already exists' do @@ -42,7 +55,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d expect do migration.queue_batched_background_migration( - 'MyJobClass', + job_class.name, :projects, :id, [:id], [:id_convert_to_bigint], @@ -62,7 +75,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d expect do migration.queue_batched_background_migration( - 'MyJobClass', + job_class.name, :projects, :id, job_interval: 5.minutes, @@ -97,7 +110,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d it 'sets the job interval to the minimum value' do expect do - migration.queue_batched_background_migration('MyJobClass', :events, :id, job_interval: minimum_delay - 1.minute) + migration.queue_batched_background_migration(job_class.name, :events, :id, job_interval: minimum_delay - 1.minute) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) created_migration = Gitlab::Database::BackgroundMigration::BatchedMigration.last @@ -107,26 +120,76 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d end context 'when additional arguments are passed to the method' do - it 'saves the arguments on the database record' do - expect do - migration.queue_batched_background_migration( - 'MyJobClass', - :projects, - :id, - 'my', - 'arguments', - job_interval: 5.minutes, - batch_max_value: 1000) - end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) + context 'when the job class provides job_arguments_count' do + context 'when defined job arguments for the job class does not match provided arguments' do + it 'raises an error' do + expect do + migration.queue_batched_background_migration( + job_class.name, + :projects, + :id, + 'my', + 'arguments', + job_interval: 2.minutes) + end.to raise_error(RuntimeError, /Wrong number of job arguments for MyJobClass \(given 2, expected 0\)/) + end + end - expect(Gitlab::Database::BackgroundMigration::BatchedMigration.last).to have_attributes( - job_class_name: 'MyJobClass', - table_name: 'projects', - column_name: 'id', - interval: 300, - min_value: 1, - max_value: 1000, - job_arguments: %w[my arguments]) + context 'when defined job arguments for the job class match provided arguments' do + let(:job_class) do + Class.new(Gitlab::BackgroundMigration::BatchedMigrationJob) do + def self.name + 'MyJobClass' + end + + job_arguments :foo, :bar + end + end + + it 'saves the arguments on the database record' do + expect do + migration.queue_batched_background_migration( + job_class.name, + :projects, + :id, + 'my', + 'arguments', + job_interval: 5.minutes, + batch_max_value: 1000) + end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) + + expect(Gitlab::Database::BackgroundMigration::BatchedMigration.last).to have_attributes( + job_class_name: 'MyJobClass', + table_name: 'projects', + column_name: 'id', + interval: 300, + min_value: 1, + max_value: 1000, + job_arguments: %w[my arguments]) + end + end + end + + context 'when the job class does not provide job_arguments_count' do + let(:job_class) do + Class.new do + def self.name + 'MyJobClass' + end + end + end + + it 'does not raise an error' do + expect do + migration.queue_batched_background_migration( + job_class.name, + :projects, + :id, + 'my', + 'arguments', + job_interval: 2.minutes) + end.not_to raise_error + end end end @@ -138,7 +201,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d it 'creates the record with the current max value' do expect do - migration.queue_batched_background_migration('MyJobClass', :events, :id, job_interval: 5.minutes) + migration.queue_batched_background_migration(job_class.name, :events, :id, job_interval: 5.minutes) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) created_migration = Gitlab::Database::BackgroundMigration::BatchedMigration.last @@ -148,7 +211,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d it 'creates the record with an active status' do expect do - migration.queue_batched_background_migration('MyJobClass', :events, :id, job_interval: 5.minutes) + migration.queue_batched_background_migration(job_class.name, :events, :id, job_interval: 5.minutes) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) expect(Gitlab::Database::BackgroundMigration::BatchedMigration.last).to be_active @@ -158,7 +221,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d context 'when the database is empty' do it 'sets the max value to the min value' do expect do - migration.queue_batched_background_migration('MyJobClass', :events, :id, job_interval: 5.minutes) + migration.queue_batched_background_migration(job_class.name, :events, :id, job_interval: 5.minutes) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) created_migration = Gitlab::Database::BackgroundMigration::BatchedMigration.last @@ -168,7 +231,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d it 'creates the record with a finished status' do expect do - migration.queue_batched_background_migration('MyJobClass', :projects, :id, job_interval: 5.minutes) + migration.queue_batched_background_migration(job_class.name, :projects, :id, job_interval: 5.minutes) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) expect(Gitlab::Database::BackgroundMigration::BatchedMigration.last).to be_finished @@ -181,7 +244,7 @@ RSpec.describe Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers d expect(migration).to receive(:gitlab_schema_from_context).and_return(:gitlab_ci) expect do - migration.queue_batched_background_migration('MyJobClass', :events, :id, job_interval: 5.minutes) + migration.queue_batched_background_migration(job_class.name, :events, :id, job_interval: 5.minutes) end.to change { Gitlab::Database::BackgroundMigration::BatchedMigration.count }.by(1) created_migration = Gitlab::Database::BackgroundMigration::BatchedMigration.last diff --git a/spec/lib/gitlab/database/migrations/instrumentation_spec.rb b/spec/lib/gitlab/database/migrations/instrumentation_spec.rb index c31244060ec..3540a120b8f 100644 --- a/spec/lib/gitlab/database/migrations/instrumentation_spec.rb +++ b/spec/lib/gitlab/database/migrations/instrumentation_spec.rb @@ -122,7 +122,11 @@ RSpec.describe Gitlab::Database::Migrations::Instrumentation do it 'records observations for all migrations' do subject.observe(version: migration_version, name: migration_name, connection: connection) {} - subject.observe(version: migration_version_2, name: migration_name_2, connection: connection) { raise 'something went wrong' } rescue nil + begin + subject.observe(version: migration_version_2, name: migration_name_2, connection: connection) { raise 'something went wrong' } + rescue StandardError + nil + end expect { load_observation(result_dir, migration_name) }.not_to raise_error expect { load_observation(result_dir, migration_name_2) }.not_to raise_error diff --git a/spec/lib/gitlab/database/migrations/lock_retry_mixin_spec.rb b/spec/lib/gitlab/database/migrations/lock_retry_mixin_spec.rb index 50ad77caaf1..6092d985ce8 100644 --- a/spec/lib/gitlab/database/migrations/lock_retry_mixin_spec.rb +++ b/spec/lib/gitlab/database/migrations/lock_retry_mixin_spec.rb @@ -83,10 +83,10 @@ RSpec.describe Gitlab::Database::Migrations::LockRetryMixin do context 'with transactions disabled' do let(:migration) { double('migration', enable_lock_retries?: false) } - let(:receiver) { double('receiver', use_transaction?: false)} + let(:receiver) { double('receiver', use_transaction?: false) } it 'calls super method' do - p = proc { } + p = proc {} expect(receiver).to receive(:ddl_transaction).with(migration, &p) @@ -95,11 +95,11 @@ RSpec.describe Gitlab::Database::Migrations::LockRetryMixin do end context 'with transactions enabled, but lock retries disabled' do - let(:receiver) { double('receiver', use_transaction?: true)} + let(:receiver) { double('receiver', use_transaction?: true) } let(:migration) { double('migration', enable_lock_retries?: false) } it 'calls super method' do - p = proc { } + p = proc {} expect(receiver).to receive(:ddl_transaction).with(migration, &p) @@ -108,12 +108,12 @@ RSpec.describe Gitlab::Database::Migrations::LockRetryMixin do end context 'with transactions enabled and lock retries enabled' do - let(:receiver) { double('receiver', use_transaction?: true)} + let(:receiver) { double('receiver', use_transaction?: true) } let(:migration) { double('migration', migration_connection: connection, enable_lock_retries?: true) } let(:connection) { ActiveRecord::Base.connection } it 'calls super method' do - p = proc { } + p = proc {} expect(receiver).not_to receive(:ddl_transaction) expect_next_instance_of(Gitlab::Database::WithLockRetries) do |retries| diff --git a/spec/lib/gitlab/database/migrations/runner_spec.rb b/spec/lib/gitlab/database/migrations/runner_spec.rb index e7f68e3e4a8..a37247ba0c6 100644 --- a/spec/lib/gitlab/database/migrations/runner_spec.rb +++ b/spec/lib/gitlab/database/migrations/runner_spec.rb @@ -41,7 +41,7 @@ RSpec.describe Gitlab::Database::Migrations::Runner do allow(described_class).to receive(:migration_context).and_return(ctx) - names_this_branch = (applied_migrations_this_branch + pending_migrations).map { |m| "db/migrate/#{m.version}_#{m.name}.rb"} + names_this_branch = (applied_migrations_this_branch + pending_migrations).map { |m| "db/migrate/#{m.version}_#{m.name}.rb" } allow(described_class).to receive(:migration_file_names_this_branch).and_return(names_this_branch) end diff --git a/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb b/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb index f1f72d71e1a..9451a6bd34a 100644 --- a/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb +++ b/spec/lib/gitlab/database/migrations/test_batched_background_runner_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez let(:connection) { ApplicationRecord.connection } - let(:table_name) { "_test_column_copying"} + let(:table_name) { "_test_column_copying" } before do connection.execute(<<~SQL) @@ -50,18 +50,16 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez context 'with jobs to run' do let(:migration_name) { 'TestBackgroundMigration' } - before do - migration.queue_batched_background_migration( - migration_name, table_name, :id, job_interval: 5.minutes, batch_size: 100 - ) - end - it 'samples jobs' do calls = [] define_background_migration(migration_name) do |*args| calls << args end + migration.queue_batched_background_migration(migration_name, table_name, :id, + job_interval: 5.minutes, + batch_size: 100) + described_class.new(result_dir: result_dir, connection: connection).run_jobs(for_duration: 3.minutes) expect(calls.count).to eq(10) # 1000 rows / batch size 100 = 10 @@ -70,6 +68,9 @@ RSpec.describe Gitlab::Database::Migrations::TestBatchedBackgroundRunner, :freez context 'with multiple jobs to run' do it 'runs all jobs created within the last 3 hours' do old_migration = define_background_migration(migration_name) + migration.queue_batched_background_migration(migration_name, table_name, :id, + job_interval: 5.minutes, + batch_size: 100) travel 4.hours |