Find builds that require a migration in batches

This commit is contained in:
Grzegorz Bizon 2017-06-28 12:21:25 +02:00
parent 6209ff671f
commit 02bb40e2ac
2 changed files with 8 additions and 3 deletions

View File

@ -2,6 +2,8 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 10000
MIGRATION = 'MigrateBuildStageIdReference'.freeze
disable_ddl_transaction!
@ -10,9 +12,10 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
end
def up
Build.find_each do |build|
BackgroundMigrationWorker
.perform_async('MigrateBuildStageIdReference', [build.id])
Build.find_in_batches(batch_size: BATCH_SIZE).with_index do |builds, batch|
migrations = builds.map { |build| [MIGRATION, [build.id]] }
BackgroundMigrationWorker.perform_bulk(*migrations)
end
end

View File

@ -8,6 +8,8 @@ describe MigrateStageIdReferenceInBackground, :migration, :redis do
let(:projects) { table(:projects) }
before do
stub_const('MigrateStageIdReferenceInBackground::BATCH_SIZE', 1)
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')