Commit b55aad4c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Migrate stages only with correct foreign references

parent 559521ce
......@@ -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
......
......@@ -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')
......
......@@ -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
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