2017-12-05 11:42:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
|
|
# rubocop:disable Style/Documentation
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module BackgroundMigration
|
|
|
|
class MigrateBuildStage
|
2018-02-14 12:59:16 +00:00
|
|
|
module Migratable
|
|
|
|
class Stage < ActiveRecord::Base
|
|
|
|
self.table_name = 'ci_stages'
|
2017-12-05 11:42:24 +00:00
|
|
|
end
|
|
|
|
|
2018-02-14 12:59:16 +00:00
|
|
|
class Build < ActiveRecord::Base
|
|
|
|
self.table_name = 'ci_builds'
|
2018-01-06 13:44:43 +00:00
|
|
|
|
2018-02-15 09:13:20 +00:00
|
|
|
def ensure_stage!(attempts: 2)
|
|
|
|
find_stage || create_stage!
|
2018-02-14 12:59:16 +00:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2018-02-15 09:13:20 +00:00
|
|
|
retry if (attempts -= 1) > 0
|
|
|
|
raise
|
2018-02-14 12:59:16 +00:00
|
|
|
end
|
2017-12-05 11:42:24 +00:00
|
|
|
|
2018-02-15 09:13:20 +00:00
|
|
|
def find_stage
|
|
|
|
Stage.find_by(name: self.stage || 'test',
|
2018-02-14 12:59:16 +00:00
|
|
|
pipeline_id: self.commit_id,
|
|
|
|
project_id: self.project_id)
|
|
|
|
end
|
2017-12-05 11:42:24 +00:00
|
|
|
|
2018-02-15 09:13:20 +00:00
|
|
|
def create_stage!
|
2018-02-14 12:59:16 +00:00
|
|
|
Stage.create!(name: self.stage || 'test',
|
|
|
|
pipeline_id: self.commit_id,
|
|
|
|
project_id: self.project_id)
|
|
|
|
end
|
2017-12-05 11:42:24 +00:00
|
|
|
end
|
2018-02-14 12:59:16 +00:00
|
|
|
end
|
2017-12-05 11:42:24 +00:00
|
|
|
|
2018-02-14 12:59:16 +00:00
|
|
|
def perform(start_id, stop_id)
|
|
|
|
stages = Migratable::Build.where('stage_id IS NULL')
|
2018-02-15 09:13:20 +00:00
|
|
|
.where('id BETWEEN ? AND ?', start_id, stop_id)
|
2018-02-14 12:59:16 +00:00
|
|
|
.map { |build| build.ensure_stage! }
|
|
|
|
.compact.map(&:id)
|
2017-12-05 11:42:24 +00:00
|
|
|
|
2018-02-14 12:59:16 +00:00
|
|
|
MigrateBuildStageIdReference.new.perform(start_id, stop_id)
|
|
|
|
MigrateStageStatus.new.perform(stages.min, stages.max)
|
2017-12-05 11:42:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|