Merge branch 'fix/gb/add-pipeline-builds-foreign-key' into 'master'
Add database foreign key between pipelines and builds Closes #46035 See merge request gitlab-org/gitlab-ce!18822
This commit is contained in:
commit
b215db37f8
4 changed files with 74 additions and 0 deletions
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
title: Add database foreign key constraint between pipelines and build
|
||||
merge_request: 18822
|
||||
author:
|
||||
type: fixed
|
27
db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
Normal file
27
db/migrate/20180420010016_add_pipeline_build_foreign_key.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
class AddPipelineBuildForeignKey < ActiveRecord::Migration
|
||||
include Gitlab::Database::MigrationHelpers
|
||||
|
||||
DOWNTIME = false
|
||||
|
||||
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 stage_id IS NULL
|
||||
SQL
|
||||
|
||||
add_concurrent_foreign_key(:ci_builds, :ci_pipelines, column: :commit_id)
|
||||
end
|
||||
|
||||
def down
|
||||
return unless foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)
|
||||
|
||||
remove_foreign_key(:ci_builds, column: :commit_id)
|
||||
end
|
||||
end
|
32
spec/migrations/add_pipeline_build_foreign_key_spec.rb
Normal file
32
spec/migrations/add_pipeline_build_foreign_key_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'spec_helper'
|
||||
require Rails.root.join('db', 'migrate', '20180420010016_add_pipeline_build_foreign_key.rb')
|
||||
|
||||
describe AddPipelineBuildForeignKey, :migration do
|
||||
let(:namespaces) { table(:namespaces) }
|
||||
let(:projects) { table(:projects) }
|
||||
let(:pipelines) { table(:ci_pipelines) }
|
||||
let(:builds) { table(:ci_builds) }
|
||||
|
||||
before do
|
||||
namespaces.create(id: 10, name: 'gitlab-org', path: 'gitlab-org')
|
||||
projects.create!(id: 11, namespace_id: 10, name: 'gitlab', path: 'gitlab')
|
||||
pipelines.create!(id: 12, project_id: 11, ref: 'master', sha: 'adf43c3a')
|
||||
|
||||
builds.create!(id: 101, commit_id: 12, project_id: 11)
|
||||
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 6
|
||||
expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_falsey
|
||||
|
||||
migrate!
|
||||
|
||||
expect(builds.all.pluck(:id)).to eq [101, 104]
|
||||
expect(foreign_key_exists?(:ci_builds, :ci_pipelines, column: :commit_id)).to be_truthy
|
||||
end
|
||||
end
|
|
@ -24,6 +24,16 @@ module MigrationsHelpers
|
|||
end
|
||||
end
|
||||
|
||||
def foreign_key_exists?(source, target = nil, column: nil)
|
||||
ActiveRecord::Base.connection.foreign_keys(source).any? do |key|
|
||||
if column
|
||||
key.options[:column].to_s == column.to_s
|
||||
else
|
||||
key.to_table.to_s == target.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def reset_column_in_all_models
|
||||
clear_schema_cache!
|
||||
|
||||
|
|
Loading…
Reference in a new issue