Commit 8624a4ed authored by Grzegorz Bizon's avatar Grzegorz Bizon

Remove orphaned builds without project / pipeline reference

parent d8d0067b
......@@ -6,13 +6,16 @@ class AddPipelineBuildForeignKey < ActiveRecord::Migration
disable_ddl_transaction!
def up
execute <<~SQL
DELETE FROM ci_builds WHERE project_id IS NULL OR commit_id IS NULL
SQL
execute <<~SQL
DELETE FROM ci_builds WHERE NOT EXISTS
(SELECT true FROM ci_pipelines WHERE ci_pipelines.id = ci_builds.commit_id)
AND commit_id IS NOT NULL
SQL
return if foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)
add_concurrent_foreign_key(:ci_builds, :ci_pipelines, column: :commit_id)
end
......
......@@ -16,10 +16,12 @@ describe AddPipelineBuildForeignKey, :migration do
builds.create!(id: 102, commit_id: 222, project_id: 11)
builds.create!(id: 103, commit_id: 333, project_id: 11)
builds.create!(id: 104, commit_id: 12, project_id: 11)
builds.create!(id: 106, commit_id: nil, project_id: 11)
builds.create!(id: 107, commit_id: 12, project_id: nil)
end
it 'adds foreign key after removing orphans' do
expect(builds.all.count).to eq 4
expect(builds.all.count).to eq 6
expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_falsey
migrate!
......
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