Skip to content

Address usability issue in generate-loose-foreign-key script

What does this MR do and why?

Describe in detail what your merge request does and why.

In https://gitlab.com/gitlab-org/gitlab/-/jobs/3886691360, I came across a couple of issues with the code generated for LFKs. This MR addressed those issues and closes #394860 (closed).

Patch generated by script
diff --git a/config/gitlab_loose_foreign_keys.yml b/config/gitlab_loose_foreign_keys.yml
index ca1129ef288b..2c630c9c1bed 100644
--- a/config/gitlab_loose_foreign_keys.yml
+++ b/config/gitlab_loose_foreign_keys.yml
@@ -244,6 +244,10 @@ p_ci_builds_metadata:
   - table: projects
     column: project_id
     on_delete: async_delete
+p_ci_runner_machine_builds:
+  - table: ci_runner_machines
+    column: runner_machine_id
+    on_delete: async_delete
 packages_build_infos:
   - table: ci_pipelines
     column: pipeline_id
diff --git a/db/structure.sql b/db/structure.sql
index 50cc6403eda9..a66cab5eea3b 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -35737,9 +35737,6 @@ ALTER TABLE ONLY dast_scanner_profiles
 ALTER TABLE ONLY vulnerability_historical_statistics
     ADD CONSTRAINT fk_rails_72b73ed023 FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE;
 
-ALTER TABLE p_ci_runner_machine_builds
-    ADD CONSTRAINT fk_rails_72ffb51d74 FOREIGN KEY (runner_machine_id) REFERENCES ci_runner_machines(id) ON DELETE CASCADE;
-
 ALTER TABLE ONLY custom_emoji
     ADD CONSTRAINT fk_rails_745925b412 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;
 
diff --git a/spec/models/ci/runner_machine_build_spec.rb b/spec/models/ci/runner_machine_build_spec.rb
index b15d232cbb56..cd5005f55548 100644
--- a/spec/models/ci/runner_machine_build_spec.rb
+++ b/spec/models/ci/runner_machine_build_spec.rb
@@ -47,4 +47,11 @@
     it { expect(partitioning_strategy.current_partitions).to include partitioning_strategy.initial_partition }
     it { expect(partitioning_strategy.active_partition).to be_present }
   end
+
+  context 'with loose foreign key on p_ci_runner_machine_builds.runner_machine_id' do
+    it_behaves_like 'cleanup by a loose foreign key' do
+      let!(:parent) { create(:ci_runner_machine) }
+      let!(:model) { create(:p_ci_runner_machine_build, runner_machine: parent) }
+    end
+  end
 end

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

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.

Merge request reports