diff options
-rw-r--r-- | db/post_migrate/20171205101928_schedule_build_stage_migration.rb | 20 | ||||
-rw-r--r-- | spec/migrations/schedule_build_stage_migration_spec.rb | 43 |
2 files changed, 63 insertions, 0 deletions
diff --git a/db/post_migrate/20171205101928_schedule_build_stage_migration.rb b/db/post_migrate/20171205101928_schedule_build_stage_migration.rb new file mode 100644 index 00000000000..60293c60e8e --- /dev/null +++ b/db/post_migrate/20171205101928_schedule_build_stage_migration.rb @@ -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 diff --git a/spec/migrations/schedule_build_stage_migration_spec.rb b/spec/migrations/schedule_build_stage_migration_spec.rb new file mode 100644 index 00000000000..f80c0a94b32 --- /dev/null +++ b/spec/migrations/schedule_build_stage_migration_spec.rb @@ -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 |