Add initial build stage_id ref background migration

This commit is contained in:
Grzegorz Bizon 2017-06-28 12:01:52 +02:00
parent b21ee2ee36
commit 98992c4e4b
3 changed files with 27 additions and 8 deletions

View File

@ -3,7 +3,17 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
DOWNTIME = false
disable_ddl_transaction!
class Build < ActiveRecord::Base
self.table_name = 'ci_builds'
end
def up
Build.find_each do |build|
BackgroundMigrationWorker
.perform_async('MigrateBuildStageIdReference', [build.id])
end
end
def down

View File

@ -1,15 +1,19 @@
module Gitlab
module BackgroundMigration
class MigrateBuildStageIdReference
class Build < ActiveRecord::Base
self.table_name = 'ci_builds'
end
class Stage < ActiveRecord::Base
self.table_name = 'ci_stages'
end
def perform(id)
raise ArgumentError unless id.is_a?(Integer)
sql = <<-SQL.strip_heredoc
UPDATE "ci_builds" SET "stage_id" = (
SELECT id FROM ci_stages
WHERE ci_stages.pipeline_id = ci_builds.commit_id
AND ci_stages.name = ci_builds.stage
)
WHERE "ci_builds"."id" = #{id} AND "ci_builds"."stage_id" IS NULL
SQL
ActiveRecord::Base.connection.execute(sql)
end
end
end

View File

@ -22,5 +22,10 @@ describe MigrateStageIdReferenceInBackground, :migration, :redis do
end
it 'schedules background migrations' do
expect(jobs.where(stage_id: nil)).to be_present
migrate!
expect(jobs.where(stage_id: nil)).to be_empty
end
end