diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2017-11-17 11:10:14 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2017-11-17 11:10:14 +0000 |
commit | 2dff37762f76b195d6b36d73dab544d0ec5e6c83 (patch) | |
tree | ee3ae43cf986ae5ee83d0a16db44ea045bcb3b59 /spec/migrations | |
parent | 883d5da1a0cb505125d80f3705420581720b6124 (diff) | |
parent | 5cecff893db3188ff5ec779e07e9c598d3ed2e21 (diff) | |
download | gitlab-ce-2dff37762f76b195d6b36d73dab544d0ec5e6c83.tar.gz |
Merge branch 'sh-background-migration-move-mr-diff-id' into 'master'
Convert migration to populate latest merge request ID into a background migration
See merge request gitlab-org/gitlab-ce!15440
Diffstat (limited to 'spec/migrations')
-rw-r--r-- | spec/migrations/populate_merge_requests_latest_merge_request_diff_id_spec.rb | 61 | ||||
-rw-r--r-- | spec/migrations/schedule_merge_request_latest_merge_request_diff_id_migrations_spec.rb | 64 |
2 files changed, 64 insertions, 61 deletions
diff --git a/spec/migrations/populate_merge_requests_latest_merge_request_diff_id_spec.rb b/spec/migrations/populate_merge_requests_latest_merge_request_diff_id_spec.rb deleted file mode 100644 index 4ea7f441f7c..00000000000 --- a/spec/migrations/populate_merge_requests_latest_merge_request_diff_id_spec.rb +++ /dev/null @@ -1,61 +0,0 @@ -require 'spec_helper' -require Rails.root.join('db', 'post_migrate', '20171026082505_populate_merge_requests_latest_merge_request_diff_id') - -describe PopulateMergeRequestsLatestMergeRequestDiffId, :migration do - let(:projects_table) { table(:projects) } - let(:merge_requests_table) { table(:merge_requests) } - let(:merge_request_diffs_table) { table(:merge_request_diffs) } - - let(:project) { projects_table.create!(name: 'gitlab', path: 'gitlab-org/gitlab-ce') } - - def create_mr!(name, diffs: 0) - merge_request = - merge_requests_table.create!(target_project_id: project.id, - target_branch: 'master', - source_project_id: project.id, - source_branch: name, - title: name) - - diffs.times do - merge_request_diffs_table.create!(merge_request_id: merge_request.id) - end - - merge_request - end - - def diffs_for(merge_request) - merge_request_diffs_table.where(merge_request_id: merge_request.id) - end - - describe '#up' do - it 'ignores MRs without diffs' do - merge_request_without_diff = create_mr!('without_diff') - - expect(merge_request_without_diff.latest_merge_request_diff_id).to be_nil - - expect { migrate! } - .not_to change { merge_request_without_diff.reload.latest_merge_request_diff_id } - end - - it 'ignores MRs that have a diff ID already set' do - merge_request_with_multiple_diffs = create_mr!('with_multiple_diffs', diffs: 3) - diff_id = diffs_for(merge_request_with_multiple_diffs).minimum(:id) - - merge_request_with_multiple_diffs.update!(latest_merge_request_diff_id: diff_id) - - expect { migrate! } - .not_to change { merge_request_with_multiple_diffs.reload.latest_merge_request_diff_id } - end - - it 'migrates multiple MR diffs to the correct values' do - merge_requests = Array.new(3).map.with_index { |_, i| create_mr!(i, diffs: 3) } - - migrate! - - merge_requests.each do |merge_request| - expect(merge_request.reload.latest_merge_request_diff_id) - .to eq(diffs_for(merge_request).maximum(:id)) - end - end - end -end diff --git a/spec/migrations/schedule_merge_request_latest_merge_request_diff_id_migrations_spec.rb b/spec/migrations/schedule_merge_request_latest_merge_request_diff_id_migrations_spec.rb new file mode 100644 index 00000000000..158d0bc02ed --- /dev/null +++ b/spec/migrations/schedule_merge_request_latest_merge_request_diff_id_migrations_spec.rb @@ -0,0 +1,64 @@ +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20171026082505_schedule_merge_request_latest_merge_request_diff_id_migrations') + +describe ScheduleMergeRequestLatestMergeRequestDiffIdMigrations, :migration, :sidekiq do + let(:projects_table) { table(:projects) } + let(:merge_requests_table) { table(:merge_requests) } + let(:merge_request_diffs_table) { table(:merge_request_diffs) } + + let(:project) { projects_table.create!(name: 'gitlab', path: 'gitlab-org/gitlab-ce') } + + let!(:merge_request_1) { create_mr!('mr_1', diffs: 1) } + let!(:merge_request_2) { create_mr!('mr_2', diffs: 2) } + let!(:merge_request_migrated) { create_mr!('merge_request_migrated', diffs: 3) } + let!(:merge_request_4) { create_mr!('mr_4', diffs: 3) } + + def create_mr!(name, diffs: 0) + merge_request = + merge_requests_table.create!(target_project_id: project.id, + target_branch: 'master', + source_project_id: project.id, + source_branch: name, + title: name) + + diffs.times do + merge_request_diffs_table.create!(merge_request_id: merge_request.id) + end + + merge_request + end + + def diffs_for(merge_request) + merge_request_diffs_table.where(merge_request_id: merge_request.id) + end + + before do + stub_const("#{described_class.name}::BATCH_SIZE", 1) + + diff_id = diffs_for(merge_request_migrated).minimum(:id) + merge_request_migrated.update!(latest_merge_request_diff_id: diff_id) + end + + it 'correctly schedules background migrations' do + Sidekiq::Testing.fake! do + Timecop.freeze do + migrate! + + expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, merge_request_1.id, merge_request_1.id) + expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, merge_request_2.id, merge_request_2.id) + expect(described_class::MIGRATION).to be_scheduled_migration(15.minutes, merge_request_4.id, merge_request_4.id) + expect(BackgroundMigrationWorker.jobs.size).to eq 3 + end + end + end + + it 'schedules background migrations' do + Sidekiq::Testing.inline! do + expect(merge_requests_table.where(latest_merge_request_diff_id: nil).count).to eq 3 + + migrate! + + expect(merge_requests_table.where(latest_merge_request_diff_id: nil).count).to eq 0 + end + end +end |