Schedule full build stage migration in the background

For builds that are still missing `stage_id`.
This commit is contained in:
Grzegorz Bizon 2017-12-05 12:02:07 +01:00
parent 6d972724d4
commit 2ab69f0d0c
2 changed files with 63 additions and 0 deletions

View file

@ -0,0 +1,20 @@
class ScheduleBuildStageMigration < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
MIGRATION = 'MigrateBuildStage'.freeze
BATCH = 10_000
class Build < ActiveRecord::Base
include EachBatch
self.table_name = 'ci_builds'
end
def change
Build.where('stage_id IS NULL').each_batch(of: BATCH) do |builds, index|
builds.pluck(:id).map { |id| [MIGRATION, [id]] }.tap do |migrations|
BackgroundMigrationWorker.perform_bulk_in(index.minutes, migrations)
end
end
end
end

View file

@ -0,0 +1,43 @@
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20171205101928_schedule_build_stage_migration')
describe ScheduleBuildStageMigration, :migration do
let(:projects) { table(:projects) }
let(:pipelines) { table(:ci_pipelines) }
let(:stages) { table(:ci_stages) }
let(:jobs) { table(:ci_builds) }
before do
##
# Dependencies
#
projects.create!(id: 123, name: 'gitlab', path: 'gitlab-ce')
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
stages.create!(id: 1, project_id: 123, pipeline_id: 1, name: 'test')
##
# CI/CD jobs
#
jobs.create!(id: 10, commit_id: 1, project_id: 123, stage_id: nil)
jobs.create!(id: 20, commit_id: 1, project_id: 123, stage_id: nil)
jobs.create!(id: 30, commit_id: 1, project_id: 123, stage_id: nil)
jobs.create!(id: 40, commit_id: 1, project_id: 123, stage_id: 1)
end
before do
stub_const("#{described_class}::BATCH", 1)
end
it 'schedules background migrations in batches in bulk' do
Sidekiq::Testing.fake! do
Timecop.freeze do
migrate!
expect(described_class::MIGRATION).to be_scheduled_migration(1.minutes, 10)
expect(described_class::MIGRATION).to be_scheduled_migration(2.minutes, 20)
expect(described_class::MIGRATION).to be_scheduled_migration(3.minutes, 30)
expect(BackgroundMigrationWorker.jobs.size).to eq 3
end
end
end
end