diff options
Diffstat (limited to 'spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb')
-rw-r--r-- | spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb b/spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb index 78bd1afd8d2..9c33100a0b3 100644 --- a/spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb +++ b/spec/lib/gitlab/background_migration/copy_column_using_background_migration_job_spec.rb @@ -16,6 +16,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo ActiveRecord::Migration.new.extend(Gitlab::Database::MigrationHelpers) end + let(:job_arguments) { %w(name name_convert_to_text) } let(:copy_job) do described_class.new(start_id: 12, end_id: 20, @@ -23,6 +24,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo batch_column: 'id', sub_batch_size: sub_batch_size, pause_ms: pause_ms, + job_arguments: job_arguments, connection: connection) end @@ -53,32 +55,42 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo SQL end - it 'copies all primary keys in range' do - temporary_column = helpers.convert_to_bigint_column(:id) + context 'primary keys' do + let(:temporary_column) { helpers.convert_to_bigint_column(:id) } + let(:job_arguments) { ['id', temporary_column] } - copy_job.perform('id', temporary_column) + it 'copies all in range' do + copy_job.perform - expect(test_table.count).to eq(4) - expect(test_table.where("id = #{temporary_column}").pluck(:id)).to contain_exactly(12, 15, 19) - expect(test_table.where(temporary_column => 0).pluck(:id)).to contain_exactly(11) + expect(test_table.count).to eq(4) + expect(test_table.where("id = #{temporary_column}").pluck(:id)).to contain_exactly(12, 15, 19) + expect(test_table.where(temporary_column => 0).pluck(:id)).to contain_exactly(11) + end end - it 'copies all foreign keys in range' do - temporary_column = helpers.convert_to_bigint_column(:fk) + context 'foreign keys' do + let(:temporary_column) { helpers.convert_to_bigint_column(:fk) } + let(:job_arguments) { ['fk', temporary_column] } - copy_job.perform('fk', temporary_column) + it 'copies all in range' do + copy_job.perform - expect(test_table.count).to eq(4) - expect(test_table.where("fk = #{temporary_column}").pluck(:id)).to contain_exactly(12, 15, 19) - expect(test_table.where(temporary_column => 0).pluck(:id)).to contain_exactly(11) + expect(test_table.count).to eq(4) + expect(test_table.where("fk = #{temporary_column}").pluck(:id)).to contain_exactly(12, 15, 19) + expect(test_table.where(temporary_column => 0).pluck(:id)).to contain_exactly(11) + end end - it 'copies columns with NULLs' do - expect { copy_job.perform('name', 'name_convert_to_text') } - .to change { test_table.where("name_convert_to_text = 'no name'").count }.from(4).to(1) + context 'columns with NULLs' do + let(:job_arguments) { %w(name name_convert_to_text) } - expect(test_table.where('name = name_convert_to_text').pluck(:id)).to contain_exactly(12, 19) - expect(test_table.where('name is NULL and name_convert_to_text is NULL').pluck(:id)).to contain_exactly(15) + it 'copies all in range' do + expect { copy_job.perform } + .to change { test_table.where("name_convert_to_text = 'no name'").count }.from(4).to(1) + + expect(test_table.where('name = name_convert_to_text').pluck(:id)).to contain_exactly(12, 19) + expect(test_table.where('name is NULL and name_convert_to_text is NULL').pluck(:id)).to contain_exactly(15) + end end context 'when multiple columns are given' do @@ -87,8 +99,10 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo let(:columns_to_copy_from) { %w[id fk] } let(:columns_to_copy_to) { [id_tmp_column, fk_tmp_column] } + let(:job_arguments) { [columns_to_copy_from, columns_to_copy_to] } + it 'copies all values in the range' do - copy_job.perform(columns_to_copy_from, columns_to_copy_to) + copy_job.perform expect(test_table.count).to eq(4) expect(test_table.where("id = #{id_tmp_column} AND fk = #{fk_tmp_column}").pluck(:id)).to contain_exactly(12, 15, 19) @@ -100,7 +114,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo it 'raises an error' do expect do - copy_job.perform(columns_to_copy_from, columns_to_copy_to) + copy_job.perform end.to raise_error(ArgumentError, 'number of source and destination columns must match') end end @@ -109,7 +123,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo it 'tracks timings of queries' do expect(copy_job.batch_metrics.timings).to be_empty - copy_job.perform('name', 'name_convert_to_text') + copy_job.perform expect(copy_job.batch_metrics.timings[:update_all]).not_to be_empty end @@ -120,7 +134,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo it 'sleeps for the specified time between sub-batches' do expect(copy_job).to receive(:sleep).with(0.005) - copy_job.perform('name', 'name_convert_to_text') + copy_job.perform end context 'when pause_ms value is negative' do @@ -129,7 +143,7 @@ RSpec.describe Gitlab::BackgroundMigration::CopyColumnUsingBackgroundMigrationJo it 'treats it as a 0' do expect(copy_job).to receive(:sleep).with(0) - copy_job.perform('name', 'name_convert_to_text') + copy_job.perform end end end |