Migrate stages only with correct foreign references

This commit is contained in:
Grzegorz Bizon 2017-06-06 14:32:24 +02:00
parent 559521ce48
commit b55aad4cc3
3 changed files with 23 additions and 4 deletions

View File

@ -11,7 +11,10 @@ class MigratePipelineStages < ActiveRecord::Migration
execute <<-SQL.strip_heredoc
INSERT INTO ci_stages (project_id, pipeline_id, name)
SELECT project_id, commit_id, stage FROM ci_builds
WHERE stage IS NOT NULL AND stage_id IS NULL
WHERE stage IS NOT NULL
AND stage_id IS NULL
AND EXISTS (SELECT 1 FROM projects WHERE projects.id = ci_builds.project_id)
AND EXISTS (SELECT 1 FROM ci_pipelines WHERE ci_pipelines.id = ci_builds.commit_id)
GROUP BY project_id, commit_id, stage
ORDER BY MAX(stage_idx)
SQL

View File

@ -9,8 +9,15 @@ describe MigrateBuildStageReference, :migration do
let(:jobs) { table(:ci_builds) }
let(:stages) { table(:ci_stages) }
let(:pipelines) { table(:ci_pipelines) }
let(:projects) { table(:projects) }
before do
# Create projects
#
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 456, name: 'gitlab2', path: 'gitlab2')
projects.create!(id: 798, name: 'gitlab3', path: 'gitlab3')
# Create CI/CD pipelines
#
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')

View File

@ -9,8 +9,14 @@ describe MigratePipelineStages, :migration do
let(:jobs) { table(:ci_builds) }
let(:stages) { table(:ci_stages) }
let(:pipelines) { table(:ci_pipelines) }
let(:projects) { table(:projects) }
before do
# Create projects
#
projects.create!(id: 123, name: 'gitlab1', path: 'gitlab1')
projects.create!(id: 456, name: 'gitlab2', path: 'gitlab2')
# Create CI/CD pipelines
#
pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
@ -28,7 +34,8 @@ describe MigratePipelineStages, :migration do
jobs.create!(id: 8, commit_id: 2, project_id: 456, stage_idx: 1, stage: 'test:1')
jobs.create!(id: 9, commit_id: 2, project_id: 456, stage_idx: 1, stage: 'test:1')
jobs.create!(id: 10, commit_id: 2, project_id: 456, stage_idx: 2, stage: 'test:2')
jobs.create!(id: 11, commit_id: 3, project_id: 789, stage_idx: 3, stage: 'deploy')
jobs.create!(id: 11, commit_id: 3, project_id: 456, stage_idx: 3, stage: 'deploy')
jobs.create!(id: 12, commit_id: 2, project_id: 789, stage_idx: 3, stage: 'deploy')
end
it 'correctly migrates pipeline stages' do
@ -36,12 +43,14 @@ describe MigratePipelineStages, :migration do
migrate!
expect(stages.count).to eq 7
expect(stages.count).to eq 6
expect(stages.all.pluck(:name))
.to match_array %w[test build deploy test:1 test:2 deploy deploy]
.to match_array %w[test build deploy test:1 test:2 deploy]
expect(stages.where(pipeline_id: 1).order(:id).pluck(:name))
.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(stages.where(pipeline_id: 3).count).to be_zero
expect(stages.where(project_id: 789).count).to be_zero
end
end