Improve specs for background stage_id ref migration

This commit is contained in:
Grzegorz Bizon 2017-06-29 12:26:37 +02:00
parent 316d8821cd
commit 825521539d
2 changed files with 18 additions and 12 deletions

View File

@ -12,13 +12,12 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
end
def up
Build.where(stage_id: nil)
.in_batches(of: BATCH_SIZE) do |relation, index|
schedule = index * 5.minutes
jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
Build.where(stage_id: nil).in_batches(of: BATCH_SIZE) do |relation, index|
schedule = index * 5.minutes
jobs = relation.pluck(:id).map { |id| [MIGRATION, [id]] }
BackgroundMigrationWorker.perform_bulk_in(schedule, jobs)
end
BackgroundMigrationWorker.perform_bulk_in(schedule, jobs)
end
end
def down

View File

@ -2,7 +2,7 @@ require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170628080858_migrate_stage_id_reference_in_background')
describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
matcher :have_scheduled_migration do |delay, *expected|
matcher :be_scheduled_migration do |delay, *expected|
match do |migration|
BackgroundMigrationWorker.jobs.any? do |job|
job['args'] == [migration, expected] &&
@ -24,16 +24,22 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
stub_const('MigrateStageIdReferenceInBackground::BATCH_SIZE', 2)
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 345, name: 'gitlab2', path: 'gitlab2')
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
pipelines.create!(id: 2, project_id: 345, ref: 'feature', sha: 'cdf43c3c')
jobs.create!(id: 1, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 2, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 3, commit_id: 1, project_id: 123, stage_idx: 1, stage: 'test')
jobs.create!(id: 4, commit_id: 1, project_id: 123, stage_idx: 3, stage: 'deploy')
jobs.create!(id: 5, commit_id: 2, project_id: 345, stage_idx: 1, stage: 'test')
stages.create(id: 101, pipeline_id: 1, project_id: 123, name: 'test')
stages.create(id: 102, pipeline_id: 1, project_id: 123, name: 'build')
stages.create(id: 103, pipeline_id: 1, project_id: 123, name: 'deploy')
jobs.create!(id: 6, commit_id: 2, project_id: 345, stage_id: 101, stage_idx: 1, stage: 'test')
end
it 'correctly schedules background migrations' do
@ -41,10 +47,11 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
Timecop.freeze do
migrate!
expect(described_class::MIGRATION).to have_scheduled_migration(5.minutes, 1)
expect(described_class::MIGRATION).to have_scheduled_migration(5.minutes, 2)
expect(described_class::MIGRATION).to have_scheduled_migration(10.minutes, 3)
expect(described_class::MIGRATION).to have_scheduled_migration(10.minutes, 4)
expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 1)
expect(described_class::MIGRATION).to be_scheduled_migration(5.minutes, 2)
expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 3)
expect(described_class::MIGRATION).to be_scheduled_migration(10.minutes, 4)
expect(BackgroundMigrationWorker.jobs.size).to eq 5
end
end
end
@ -55,7 +62,7 @@ describe MigrateStageIdReferenceInBackground, :migration, :sidekiq do
migrate!
expect(jobs.where(stage_id: nil)).to be_empty
expect(jobs.where(stage_id: nil)).to be_one
end
end
end