Remove all foreign key constraints that connect between non `ci_*` and `ci_*` tables
Problem
Any foreign key constraint in a ci_* table must only reference another ci_* table. And any foreign key constraints not in a ci_* table must not reference a ci_* table. This is because they will live in separate databases.
Two examples that we need to fix to illustrate the point:
-
ci_builds.project_id(inci_*) referencesprojects(not inci_*) -
merge_requests.head_pipeline_id(not inci_*) referencesci_pipelines(inci_*)
Solution
We will need to go through and remove all the foreign key constraints but this forces us to also address the lack of cascading deletes/nullify.
For each of these we need to replace them with the rails version of cascading deletes which is dependent: :delete_all.
We should note that dependent: :delete_all is possibly less performant and also does not offer the same data consistency guarantees and we should evaluate in each the problems that might arise from orphaned data if the dependent: :delete_all times out but the parent is still deleted.
We may also want to evaluate if this dependent: :delete_all should be pushed into sidekiq workers to improve performance for deletes from the UI as well as improve reliability by relying on Sidekiq retries for failures.