Skip to content

LFK: Remove foreign key security_scans.build_id

What does this MR do and why?

In !77908 (merged) we already added a "loose foreign key" 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.

Migration output

Up

== 20220111002708 TrackSecurityScanChanges: migrating =========================
-- execute("CREATE TRIGGER security_scans_loose_fk_trigger\nAFTER DELETE ON security_scans REFERENCING OLD TABLE AS old_table\nFOR EACH STATEMENT\nEXECUTE FUNCTION insert_into_loose_foreign_keys_deleted_records();\n")
   -> 0.0030s
== 20220111002708 TrackSecurityScanChanges: migrated (0.0030s) ================

== 20220111002756 RemoveSecurityScansBuildIdFk: migrating =====================
-- transaction_open?()
   -> 0.0000s
-- foreign_keys(:security_scans)
   -> 0.0053s
-- remove_foreign_key(:security_scans, :ci_builds, {:name=>"fk_rails_4ef1e6b4c6"})
   -> 0.0052s
== 20220111002756 RemoveSecurityScansBuildIdFk: migrated (0.0140s) ============

Down

== 20220111002756 RemoveSecurityScansBuildIdFk: reverting =====================
-- transaction_open?()
   -> 0.0000s
-- foreign_keys(:security_scans)
   -> 0.0047s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE security_scans\nADD CONSTRAINT fk_rails_4ef1e6b4c6\nFOREIGN KEY (build_id)\nREFERENCES ci_builds (id)\nON DELETE CASCADE\nNOT VALID;\n")
   -> 0.0040s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- execute("ALTER TABLE security_scans VALIDATE CONSTRAINT fk_rails_4ef1e6b4c6;")
   -> 0.0217s
-- execute("RESET statement_timeout")
   -> 0.0009s
== 20220111002756 RemoveSecurityScansBuildIdFk: reverted (0.0418s) ============

== 20220111002708 TrackSecurityScanChanges: reverting =========================
-- execute("DROP TRIGGER IF EXISTS security_scans_loose_fk_trigger ON security_scans")
   -> 0.0010s
== 20220111002708 TrackSecurityScanChanges: reverted (0.0010s) ================

Screenshots or screen recordings

These are strongly recommended to assist reviewers and reduce the time to merge your change.

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related issues

Related to #348274 (closed)

Edited by Kamil Trzciński

Merge request reports