Commit 53884146 authored by Furkan Ayhan's avatar Furkan Ayhan Committed by Adam Hegyi

Add foreign key to ci_builds runner_id

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

There will be 3 steps;
1. Add a NOT VALID foreign key constraint to the column to ensure
GitLab doesn't create inconsistent records.
2. Add a data migration, to fix or clean up existing records.
3. Validate the whole table by making the foreign key VALID.

This is the 1st step.

Changelog: other
parent 8a79b81d
# frozen_string_literal: true
class AddNotValidForeignKeyToCiBuildsRunnerId < Gitlab::Database::Migration[1.0]
disable_ddl_transaction!
def up
add_concurrent_foreign_key :ci_builds, :ci_runners, column: :runner_id, on_delete: :nullify, validate: false
end
def down
with_lock_retries do
remove_foreign_key_if_exists :ci_builds, column: :runner_id
end
end
end
e00dd618ca393596f3ff05b44b1a9a36183729a864a5cf4b8f1a262dfcdb932b
\ No newline at end of file
...@@ -30140,6 +30140,9 @@ ALTER TABLE ONLY ci_builds_metadata ...@@ -30140,6 +30140,9 @@ ALTER TABLE ONLY ci_builds_metadata
ALTER TABLE ONLY gitlab_subscriptions ALTER TABLE ONLY gitlab_subscriptions
ADD CONSTRAINT fk_e2595d00a1 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE; ADD CONSTRAINT fk_e2595d00a1 FOREIGN KEY (namespace_id) REFERENCES namespaces(id) ON DELETE CASCADE;
ALTER TABLE ONLY ci_builds
ADD CONSTRAINT fk_e4ef9c2f27 FOREIGN KEY (runner_id) REFERENCES ci_runners(id) ON DELETE SET NULL NOT VALID;
ALTER TABLE ONLY merge_requests ALTER TABLE ONLY merge_requests
ADD CONSTRAINT fk_e719a85f8a FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE SET NULL; ADD CONSTRAINT fk_e719a85f8a FOREIGN KEY (author_id) REFERENCES users(id) ON DELETE SET NULL;
...@@ -26,7 +26,7 @@ RSpec.describe 'Database schema' do ...@@ -26,7 +26,7 @@ RSpec.describe 'Database schema' do
boards: %w[milestone_id iteration_id], boards: %w[milestone_id iteration_id],
chat_names: %w[chat_id team_id user_id], chat_names: %w[chat_id team_id user_id],
chat_teams: %w[team_id], chat_teams: %w[team_id],
ci_builds: %w[erased_by_id runner_id trigger_request_id], ci_builds: %w[erased_by_id trigger_request_id],
ci_namespace_monthly_usages: %w[namespace_id], ci_namespace_monthly_usages: %w[namespace_id],
ci_runner_projects: %w[runner_id], ci_runner_projects: %w[runner_id],
ci_trigger_requests: %w[commit_id], ci_trigger_requests: %w[commit_id],
......
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