summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-12-05 12:42:24 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-01-06 14:09:59 +0100
commitcd6d0dbd5fe9548ab7c0cd7b09e640a7b425daf1 (patch)
tree839568b145dd96c7437cef4713ca542c678b5efc
parent2ab69f0d0c825f8546f189a61189246d6c90b7ff (diff)
downloadgitlab-ce-cd6d0dbd5fe9548ab7c0cd7b09e640a7b425daf1.tar.gz
Migrate a build stage completely in a background migration
-rw-r--r--lib/gitlab/background_migration/migrate_build_stage.rb67
-rw-r--r--spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb48
2 files changed, 115 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/migrate_build_stage.rb b/lib/gitlab/background_migration/migrate_build_stage.rb
new file mode 100644
index 00000000000..05edaab29d2
--- /dev/null
+++ b/lib/gitlab/background_migration/migrate_build_stage.rb
@@ -0,0 +1,67 @@
+# frozen_string_literal: true
+# rubocop:disable Metrics/AbcSize
+# rubocop:disable Style/Documentation
+
+module Gitlab
+ module BackgroundMigration
+ class MigrateBuildStage
+ def perform(id)
+ Ci::Build.find_by(id: id).try do |build|
+ Stage.new(build).tap do |stage|
+ return if stage.exists?
+
+ stage.ensure!
+ stage.migrate_reference!
+ stage.migrate_status!
+ end
+ end
+ end
+
+ private
+
+ class Ci::Stage < ActiveRecord::Base
+ self.table_name = 'ci_stages'
+ end
+
+ class Ci::Build < ActiveRecord::Base
+ self.table_name = 'ci_builds'
+ end
+
+ class Stage
+ def initialize(build)
+ @build = build
+ end
+
+ def exists?
+ @build.reload.stage_id.present?
+ end
+
+ def ensure!
+ find || create!
+ end
+
+ def find
+ Ci::Stage.find_by(name: @build.stage,
+ pipeline_id: @build.commit_id,
+ project_id: @build.project_id)
+ end
+
+ def create!
+ Ci::Stage.create!(name: @build.stage,
+ pipeline_id: @build.commit_id,
+ project_id: @build.project_id)
+ 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
diff --git a/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb b/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb
new file mode 100644
index 00000000000..baa9c532c6d
--- /dev/null
+++ b/spec/lib/gitlab/background_migration/migrate_build_stage_spec.rb
@@ -0,0 +1,48 @@
+require 'spec_helper'
+
+describe Gitlab::BackgroundMigration::MigrateBuildStage, :migration, schema: 20171205101928 do
+ let(:projects) { table(:projects) }
+ let(:pipelines) { table(:ci_pipelines) }
+ let(:stages) { table(:ci_stages) }
+ let(:jobs) { table(:ci_builds) }
+
+ STATUSES = { created: 0, pending: 1, running: 2, success: 3,
+ failed: 4, canceled: 5, skipped: 6, manual: 7 }.freeze
+
+ before do
+ ##
+ # Dependencies
+ #
+ projects.create!(id: 123, name: 'gitlab', path: 'gitlab-ce')
+ pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
+
+ ##
+ # CI/CD jobs
+ #
+ jobs.create!(id: 1, commit_id: 1, project_id: 123,
+ stage_idx: 2, stage: 'build', status: :success)
+ jobs.create!(id: 2, commit_id: 1, project_id: 123,
+ stage_idx: 2, stage: 'build', status: :success)
+ jobs.create!(id: 3, commit_id: 1, project_id: 123,
+ stage_idx: 1, stage: 'test', status: :failed)
+ jobs.create!(id: 4, commit_id: 1, project_id: 123,
+ stage_idx: 1, stage: 'test', status: :success)
+ jobs.create!(id: 5, commit_id: 1, project_id: 123,
+ stage_idx: 3, stage: 'deploy', status: :pending)
+ end
+
+ it 'correctly migrates builds stages' do
+ expect(stages.count).to be_zero
+
+ jobs.all.find_each do |job|
+ described_class.new.perform(job.id)
+ end
+
+ expect(stages.count).to eq 3
+ expect(stages.all.pluck(:name)).to match_array %w[test build deploy]
+ expect(jobs.where(stage_id: nil)).to be_empty
+ expect(stages.all.pluck(:status)).to match_array [STATUSES[:success],
+ STATUSES[:failed],
+ STATUSES[:pending]]
+ end
+end