Remove FK security_scans.build_id as we have LFK

In https://gitlab.com/gitlab-org/gitlab/-/merge_requests/77908 we
already added a ["loose foreign key"](
https://docs.gitlab.com/ee/development/database/loose_foreign_keys.html)
to replace this foreign key. Now that this has been running fine in
production we should be OK to remove the foreign key altogether.

This was added in a post-deployment migration as we want to ensure that
they have the correct code running with the loose foreign key in place
before we remove the foreign key to ensure that there isn't any lost
deletes.

Changelog: other
parent 7efb9d49
# 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
......@@ -30496,9 +30496,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