Commit b7d67232 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add initial build stage_id ref background migration

parent 16ae7b7a
...@@ -3,7 +3,17 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration ...@@ -3,7 +3,17 @@ class MigrateStageIdReferenceInBackground < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
disable_ddl_transaction!
class Build < ActiveRecord::Base
self.table_name = 'ci_builds'
end
def up def up
Build.find_each do |build|
BackgroundMigrationWorker
.perform_async('MigrateBuildStageIdReference', [build.id])
end
end end
def down def down
......
module Gitlab module Gitlab
module BackgroundMigration module BackgroundMigration
class MigrateBuildStageIdReference class MigrateBuildStageIdReference
class Build < ActiveRecord::Base def perform(id)
self.table_name = 'ci_builds' raise ArgumentError unless id.is_a?(Integer)
end
class Stage < ActiveRecord::Base sql = <<-SQL.strip_heredoc
self.table_name = 'ci_stages' UPDATE "ci_builds" SET "stage_id" = (
end 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
def perform(id) ActiveRecord::Base.connection.execute(sql)
end end
end end
end end
......
...@@ -22,5 +22,10 @@ describe MigrateStageIdReferenceInBackground, :migration, :redis do ...@@ -22,5 +22,10 @@ describe MigrateStageIdReferenceInBackground, :migration, :redis do
end end
it 'schedules background migrations' do 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
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment