From 911855a9e07b81f018d11a29cc9c328b3c75b39c Mon Sep 17 00:00:00 2001 From: Michelangelo Mori Date: Thu, 28 Nov 2024 18:11:02 +0100 Subject: [PATCH] Fix on delete cascade in `rule_type_data_sources`. (#5085) This migration recreates the foreign key constraint to delete rows from `rule_type_data_sources` when a record is deleted from `rule_type`. Note that it is safe to just drop and recreate the constraint as the previous version prevented the deletion of rule types if they referenced a data source, so it was not possible to have dangling records. --- ...ype_data_sources_cascading_delete.down.sql | 13 ++++++++ ...rtype_data_sources_cascading_delete.up.sql | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 database/migrations/000110_rule_rtype_data_sources_cascading_delete.down.sql create mode 100644 database/migrations/000110_rule_rtype_data_sources_cascading_delete.up.sql diff --git a/database/migrations/000110_rule_rtype_data_sources_cascading_delete.down.sql b/database/migrations/000110_rule_rtype_data_sources_cascading_delete.down.sql new file mode 100644 index 0000000000..b4e6aee6a2 --- /dev/null +++ b/database/migrations/000110_rule_rtype_data_sources_cascading_delete.down.sql @@ -0,0 +1,13 @@ +-- SPDX-FileCopyrightText: Copyright 2024 The Minder Authors +-- SPDX-License-Identifier: Apache-2.0 + +BEGIN; + +ALTER TABLE rule_type_data_sources + DROP CONSTRAINT rule_type_data_sources_rule_type_id_fkey; + +ALTER TABLE rule_type_data_sources + ADD CONSTRAINT rule_type_data_sources_rule_type_id_fkey + FOREIGN KEY (rule_type_id) REFERENCES rule_type(id); + +COMMIT; diff --git a/database/migrations/000110_rule_rtype_data_sources_cascading_delete.up.sql b/database/migrations/000110_rule_rtype_data_sources_cascading_delete.up.sql new file mode 100644 index 0000000000..6c009bf188 --- /dev/null +++ b/database/migrations/000110_rule_rtype_data_sources_cascading_delete.up.sql @@ -0,0 +1,30 @@ +-- SPDX-FileCopyrightText: Copyright 2024 The Minder Authors +-- SPDX-License-Identifier: Apache-2.0 + +BEGIN; + +-- Generally speaking, given two projects R (root) and A within the +-- same hierarchy, a rule type defined in project A can reference a +-- data source defined in project R, and in such cases we want to +-- prevent admins of project R from deleting the data source without +-- fixing said rule type in project A (or having someone else fix it +-- for them), but we still want admins from project A to be able to +-- delete their rule types without hindrance. +-- +-- This migration recreates the foreign key constraint to delete rows +-- from `rule_type_data_sources` when a record is deleted from +-- `rule_type`. +-- +-- Note that it is safe to just drop and recreate the constraint as +-- the previous version prevented the deletion of rule types if they +-- referenced a data source, so it was not possible to have dangling +-- records. + +ALTER TABLE rule_type_data_sources + DROP CONSTRAINT rule_type_data_sources_rule_type_id_fkey; + +ALTER TABLE rule_type_data_sources + ADD CONSTRAINT rule_type_data_sources_rule_type_id_fkey + FOREIGN KEY (rule_type_id) REFERENCES rule_type(id) ON DELETE CASCADE; + +COMMIT;