Commit 66f24a97 authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch '348274-remove-foreign-key-security_scans-build_id' into 'master'

Remove foreign key security_scans.build_id

See merge request gitlab-org/gitlab!77919
parents f75b04be fc438951
# frozen_string_literal: true
class RemoveSecurityScansBuildIdFk < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
CONSTRAINT_NAME = 'fk_rails_4ef1e6b4c6'
def up
with_lock_retries do
execute('LOCK ci_builds, security_scans IN ACCESS EXCLUSIVE MODE')
remove_foreign_key_if_exists(:security_scans, :ci_builds, name: CONSTRAINT_NAME)
end
end
def down
add_concurrent_foreign_key(:security_scans, :ci_builds, column: :build_id, on_delete: :cascade, name: CONSTRAINT_NAME)
end
end
34759cbf09171f6057b87af791f5e9f3045ac5e06558147436ba32e276f40a19
\ No newline at end of file
......@@ -30504,9 +30504,6 @@ ALTER TABLE ONLY geo_repository_renamed_events
ALTER TABLE ONLY aws_roles
ADD CONSTRAINT fk_rails_4ed56f4720 FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
ALTER TABLE ONLY security_scans
ADD CONSTRAINT fk_rails_4ef1e6b4c6 FOREIGN KEY (build_id) REFERENCES ci_builds(id) ON DELETE CASCADE;
ALTER TABLE ONLY packages_debian_publications
ADD CONSTRAINT fk_rails_4fc8ebd03e FOREIGN KEY (distribution_id) REFERENCES packages_debian_project_distributions(id) ON DELETE CASCADE;
......@@ -3,6 +3,11 @@
require 'spec_helper'
RSpec.describe Security::Scan do
it_behaves_like 'cleanup by a loose foreign key' do
let!(:model) { create(:security_scan) }
let(:parent) { model.build }
end
describe 'associations' do
it { is_expected.to belong_to(:build) }
it { is_expected.to belong_to(:project) }
......
......@@ -55,8 +55,13 @@ RSpec.shared_examples 'cleanup by a loose foreign key' do
end
def find_model
primary_key = model.class.primary_key.to_sym
model.class.find_by(primary_key => model.public_send(primary_key))
query = model.class
# handle composite primary keys
connection = model.class.connection
connection.primary_keys(model.class.table_name).each do |primary_key|
query = query.where(primary_key => model.public_send(primary_key))
end
query.first
end
it 'cleans up (delete or nullify) the model' do
......
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