Validate environment_id foreign key on deployments table
Summary
The foreign key for environment_id
on the deployments
table is currently NOT VALID
(https://gitlab.com/gitlab-org/gitlab/-/blob/master/db/structure.sql#L32250):
ALTER TABLE ONLY deployments
ADD CONSTRAINT fk_009fd21147 FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE NOT VALID;
This foreign key was introduced last year, and we could not validate it right away due to orphaned deployments. Orphaned rows have been cleaned up, so we should be okay to validate the foreign key now.
Implementation
validate_foreign_key :deployments, :environment_id
The validation may need to be run in a background migration as it will probably take long to run. See https://docs.gitlab.com/ee/development/database/add_foreign_key_to_existing_column.html#validate-the-foreign-key for docs on foreign key validation.
Edited by Alishan Ladhani