From: Bernd Bleßmann Date: Wed, 24 Mar 2021 13:07:07 +0000 (+0100) Subject: Mahnungen: DB-Trigger zum Löschen von Verknüpfungen beim Löschen einer Mahnung X-Git-Tag: kivitendo-mebil_0.1-0~9^2~400 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=07c31856a83b520e82d21a40a749f3a1a7c6df6d;p=kivitendo-erp.git Mahnungen: DB-Trigger zum Löschen von Verknüpfungen beim Löschen einer Mahnung --- diff --git a/sql/Pg-upgrade2/record_links_dunning_post_delete_trigger.sql b/sql/Pg-upgrade2/record_links_dunning_post_delete_trigger.sql new file mode 100644 index 000000000..f7a097e7d --- /dev/null +++ b/sql/Pg-upgrade2/record_links_dunning_post_delete_trigger.sql @@ -0,0 +1,21 @@ +-- @tag: record_links_dunning_post_delete_trigger +-- @description: Verknüpfte Belege für Mahnungen beim Löschen entfernen +-- @depends: release_3_5_6_1 + +-- clean up old dangling links +DELETE FROM record_links WHERE from_table = 'dunning' AND NOT EXISTS (SELECT id FROM dunning WHERE id = from_id); +DELETE FROM record_links WHERE to_table = 'dunning' AND NOT EXISTS (SELECT id FROM dunning WHERE id = to_id); + +-- install a trigger to delete links on delete +CREATE OR REPLACE FUNCTION clean_up_record_links_before_dunning_delete() RETURNS trigger AS $$ + BEGIN + DELETE FROM record_links + WHERE (from_table = 'dunning' AND from_id = OLD.id) + OR (to_table = 'dunning' AND to_id = OLD.id); + RETURN OLD; + END; +$$ LANGUAGE plpgsql; + +CREATE TRIGGER before_delete_dunning_trigger +BEFORE DELETE ON dunning FOR EACH ROW EXECUTE +PROCEDURE clean_up_record_links_before_dunning_delete();