Skip to content

Add foreign key to ci_builds runner_id

What does this MR do and why?

This MR adds a foreign key to ci_builds runner_id.

When a runner gets deleted, jobs that were run with it still have runner_id. With this MR, they will start to be nullified.

There will be 3 steps (https://docs.gitlab.com/ee/development/database/add_foreign_key_to_existing_column.html);

  1. Add a NOT VALID foreign key constraint to the column to ensure GitLab doesn't create inconsistent records. <-- You're here
  2. Add a data migration, to fix or clean up existing records. <-- soon...
  3. Validate the whole table by making the foreign key VALID. <-- will be in 14.10

This is the 1st step.

Related comment: #30159 (comment 836146520)

Database

Up

== 20220208170445 AddNotValidForeignKeyToCiBuildsRunnerId: migrating ==========
-- transaction_open?()
   -> 0.0000s
-- foreign_keys(:ci_builds)
   -> 0.0071s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE ci_builds\nADD CONSTRAINT fk_e4ef9c2f27\nFOREIGN KEY (runner_id)\nREFERENCES ci_runners (id)\nON DELETE SET NULL\nNOT VALID;\n")
   -> 0.0169s
== 20220208170445 AddNotValidForeignKeyToCiBuildsRunnerId: migrated (0.0514s) =

Down

== 20220208170445 AddNotValidForeignKeyToCiBuildsRunnerId: reverting ==========
-- transaction_open?()
   -> 0.0000s
-- foreign_keys(:ci_builds)
   -> 0.0036s
-- remove_foreign_key(:ci_builds, {:column=>:runner_id})
   -> 0.0035s
== 20220208170445 AddNotValidForeignKeyToCiBuildsRunnerId: reverted (0.0138s) =

How to set up and validate locally

  1. Register a new runner:
gitlab-runner register --url http://gdk.test:3000 --registration-token XYZ --executor shell --name "will-be-deleted"
  1. Run a build with this runner.
  2. See the build has runner_id an integer.
  3. Unregister the runner:
gitlab-runner unregister -n will-be-deleted
  1. See the build has runner_id nil.

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 to #30159 (closed)

Edited by Furkan Ayhan

Merge request reports