BigW Consortium Gitlab

Commit 33a0fd99 by Grzegorz Bizon

Migrate build stage reference in a separate migration

parent b8c39649
......@@ -15,14 +15,6 @@ class MigratePipelineStages < ActiveRecord::Migration
GROUP BY project_id, commit_id, stage, stage_idx
ORDER BY stage_idx
SQL
stage_id = Arel.sql('(SELECT id FROM ci_stages ' \
'WHERE ci_stages.pipeline_id = ci_builds.commit_id ' \
'AND ci_stages.name = ci_builds.stage)')
update_column_in_batches(:ci_builds, :stage_id, stage_id)
# add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
# add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
end
def down
......
class MigrateBuildStageReference < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
disable_statement_timeout
stage_id = Arel.sql('(SELECT id FROM ci_stages ' \
'WHERE ci_stages.pipeline_id = ci_builds.commit_id ' \
'AND ci_stages.name = ci_builds.stage)')
update_column_in_batches(:ci_builds, :stage_id, stage_id)
end
def down
disable_statement_timeout
update_column_in_batches(:ci_builds, :stage_id, nil)
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170526101042) do
ActiveRecord::Schema.define(version: 20170526185921) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20170526185921_migrate_build_stage_reference.rb')
describe MigrateBuildStageReference, :migration, schema: 20170526185602 do
##
# Create test data - pipeline and CI/CD jobs.
#
let(:jobs) { table(:ci_builds) }
let(:stages) { table(:ci_stages) }
let(:pipelines) { table(:ci_pipelines) }
before do
# Create CI/CD pipelines
#
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
pipelines.create!(id: 2, project_id: 456, ref: 'feature', sha: '21a3deb')
# Create CI/CD jobs
#
jobs.create!(id: 1, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 2, commit_id: 1, project_id: 123, stage_idx: 2, stage: 'build')
jobs.create!(id: 3, commit_id: 1, project_id: 123, stage_idx: 1, stage: 'test')
jobs.create!(id: 4, commit_id: 1, project_id: 123, stage_idx: 3, stage: 'deploy')
jobs.create!(id: 5, commit_id: 2, project_id: 456, stage_idx: 2, stage: 'test:2')
jobs.create!(id: 6, commit_id: 2, project_id: 456, stage_idx: 1, stage: 'test:1')
jobs.create!(id: 7, commit_id: 2, project_id: 456, stage_idx: 1, stage: 'test:1')
jobs.create!(id: 8, commit_id: 3, project_id: 789, stage_idx: 3, stage: 'deploy')
# Create CI/CD stages
#
stages.create(id: 101, pipeline_id: 1, project_id: 123, name: 'test')
stages.create(id: 102, pipeline_id: 1, project_id: 123, name: 'build')
stages.create(id: 103, pipeline_id: 1, project_id: 123, name: 'deploy')
stages.create(id: 104, pipeline_id: 2, project_id: 456, name: 'test:1')
stages.create(id: 105, pipeline_id: 2, project_id: 456, name: 'test:2')
stages.create(id: 106, pipeline_id: 2, project_id: 456, name: 'deploy')
end
it 'correctly migrate build stage references' do
expect(jobs.where(stage_id: nil).count).to eq 8
migrate!
expect(jobs.where(stage_id: nil).count).to eq 1
expect(jobs.find(1).stage_id).to eq 102
expect(jobs.find(2).stage_id).to eq 102
expect(jobs.find(3).stage_id).to eq 101
expect(jobs.find(4).stage_id).to eq 103
expect(jobs.find(5).stage_id).to eq 105
expect(jobs.find(6).stage_id).to eq 104
expect(jobs.find(7).stage_id).to eq 104
expect(jobs.find(8).stage_id).to eq nil
end
end
......@@ -43,7 +43,5 @@ describe MigratePipelineStages, :migration, schema: 20170525132202 do
.to eq %w[test build deploy]
expect(stages.where(pipeline_id: 2).order(:id).pluck(:name))
.to eq %w[test:1 test:2 deploy]
expect(jobs.where(stage_id: nil)).to be_empty
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