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
|
|
|
|
def perform(id)
|
2018-01-06 13:44:43 +00:00
|
|
|
DatabaseBuild.find_by(id: id).try do |build|
|
|
|
|
MigratableStage.new(build).tap do |stage|
|
|
|
|
break if stage.exists? || stage.legacy?
|
2017-12-05 11:42:24 +00:00
|
|
|
|
|
|
|
stage.ensure!
|
|
|
|
stage.migrate_reference!
|
|
|
|
stage.migrate_status!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-06 13:44:43 +00:00
|
|
|
class DatabaseStage < ActiveRecord::Base
|
2017-12-05 11:42:24 +00:00
|
|
|
self.table_name = 'ci_stages'
|
|
|
|
end
|
|
|
|
|
2018-01-06 13:44:43 +00:00
|
|
|
class DatabaseBuild < ActiveRecord::Base
|
2017-12-05 11:42:24 +00:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
end
|
|
|
|
|
2018-01-06 13:44:43 +00:00
|
|
|
class MigratableStage
|
2017-12-05 11:42:24 +00:00
|
|
|
def initialize(build)
|
|
|
|
@build = build
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
@build.reload.stage_id.present?
|
|
|
|
end
|
|
|
|
|
2018-01-06 13:44:43 +00:00
|
|
|
##
|
|
|
|
# We can have some very old stages that do not have `ci_builds.stage` set.
|
|
|
|
#
|
|
|
|
# In that case we just don't migrate such stage.
|
|
|
|
#
|
|
|
|
def legacy?
|
2018-01-06 15:07:55 +00:00
|
|
|
@build.stage.nil?
|
2018-01-06 13:44:43 +00:00
|
|
|
end
|
|
|
|
|
2017-12-05 11:42:24 +00:00
|
|
|
def ensure!
|
|
|
|
find || create!
|
|
|
|
end
|
|
|
|
|
|
|
|
def find
|
2018-01-06 13:44:43 +00:00
|
|
|
DatabaseStage.find_by(name: @build.stage,
|
|
|
|
pipeline_id: @build.commit_id,
|
|
|
|
project_id: @build.project_id)
|
2017-12-05 11:42:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create!
|
2018-01-06 13:44:43 +00:00
|
|
|
DatabaseStage.create!(name: @build.stage,
|
|
|
|
pipeline_id: @build.commit_id,
|
|
|
|
project_id: @build.project_id)
|
2017-12-05 11:42:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_reference!
|
|
|
|
MigrateBuildStageIdReference.new.perform(@build.id, @build.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def migrate_status!
|
|
|
|
raise ArgumentError unless exists?
|
|
|
|
|
|
|
|
MigrateStageStatus.new.perform(@build.stage_id, @build.stage_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|