Change project_id FK constraint on terraform module metadata
Context
The database table packages_terraform_module_metadata has a foreign key constrain on the project_id column with a NOT NULL constraint. At the same time, the FK constraint has ON DELETE SET NULL.
The NOT NULL & ON DELETE SET NULL constraints are contradictory. Thus, when trying to delete a project that has referenced records in packages_terraform_module_metadata, this error is raised: ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR: null value in column "project_id" of relation "packages_terraform_module_metadata" violates not-null constraint.
Now we can think of removing the NOT NULL constraint as a solution, but since the project_id is the sharding key in the table, it cannot be nullified.
Another solution could be to change ON DELETE SET NULL to be ON DELETE CASCADE. However, This would be redundant (and could cause issues?) as we have a chain of cascading deletion from projects -> packages_packages -> packages_terraform_module_metadata.
The solution this MR is implementing is changing ON DELETE SET NULL to be ON DELETE CASCADE. Although we already have a cascading deletion from projects -> packages_packages -> packages_terraform_module_metadata, It's not harmful to have a redundant cascading delete.
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.
| Before | After |
|---|---|
How to set up and validate locally
In rails console:
# stub file upload
def fixture_file_upload(*args, **kwargs)
Rack::Test::UploadedFile.new(*args, **kwargs)
end
# Create a new entry in the `packages_terraform_module_metadata` table
metadatum = FactoryBot.create(:terraform_module_metadatum)
# destroy its project, it should be successful
metadatum.project.destroy!
Related to #470188 (closed)