Add foreign key between ci_runner_projects and ci_runners

What does this MR do and why?

This MR does two things, each its commit:

  1. adds a foreign key between ci_runner_projects and ci_runners, in the same way as the fk_rails_8767676b7a FK constraint that exists in ci_runner_namespaces:

image

  1. adds a batched background migration to remove orphaned ci_runner_projects records. The FK constraint from the previous point will be validated after the required stop in %17.9 (hoping we can merge this in %17.8), but will for now avoid new records being left behind.

Changelog: added

References

Please include cross links to any resources that are relevant to this MR. This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

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

This MR will delete around 30% of the ci_runner_projects records:

gitlabhq_dblab> SELECT COUNT(*) FROM ci_runner_projects WHERE NOT EXISTS (SELECT 1 FROM ci_runners WHERE ci_runners.id = ci_runner_projects.runner_id)
+--------+
| count  |
|--------|
| 546619 |
+--------+
SELECT 1
Time: 17.769s (17 seconds), executed in: 17.768s (17 seconds)
gitlabhq_dblab> SELECT COUNT(*) FROM ci_runner_projects
+---------+
| count   |
|---------|
| 1790283 |
+---------+
SELECT 1
Time: 1.927s (1 second), executed in: 1.925s (1 second)

How to set up and validate locally

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

Edited by Pedro Pombeiro

Merge request reports

Loading