Revision 024f31b4
Von Jan Büren vor mehr als 5 Jahren hinzugefügt
sql/Pg-upgrade2/record_links_post_delete_triggers_gl.sql | ||
---|---|---|
1 |
-- @tag: record_links_post_delete_triggers_gl2 |
|
2 |
-- @description: Datenbankkonsistenz record_links nach Löschen von Dialogbuchungen und Briefen |
|
3 |
-- @depends: release_3_5_3 |
|
4 |
|
|
5 |
-- When deleting records record_links weren't cleaned up until now |
|
6 |
-- This wasn't really a problem apart from the fact that record_links slowly grew |
|
7 |
-- but deleting records was seldom enough to not matter |
|
8 |
-- Unfortunately delivery_plan decides if an order need to be displayed by the |
|
9 |
-- number of record_links, which generates false negatives. |
|
10 |
-- so, first clean up the database, and after that create triggers to |
|
11 |
-- clean up automatically |
|
12 |
|
|
13 |
DELETE FROM record_links WHERE from_table = 'letter' AND from_id NOT IN (SELECT id FROM letter); |
|
14 |
DELETE FROM record_links WHERE to_table = 'letter' AND to_id NOT IN (SELECT id FROM letter); |
|
15 |
|
|
16 |
DELETE FROM record_links WHERE from_table = 'gl' AND from_id NOT IN (SELECT id FROM gl); |
|
17 |
DELETE FROM record_links WHERE to_table = 'gl' AND to_id NOT IN (SELECT id FROM gl); |
|
18 |
|
|
19 |
CREATE OR REPLACE FUNCTION clean_up_record_links_before_letter_delete() RETURNS trigger AS $$ |
|
20 |
BEGIN |
|
21 |
DELETE FROM record_links |
|
22 |
WHERE (from_table = 'letter' AND from_id = OLD.id) |
|
23 |
OR (to_table = 'letter' AND to_id = OLD.id); |
|
24 |
RETURN OLD; |
|
25 |
END; |
|
26 |
$$ LANGUAGE plpgsql; |
|
27 |
|
|
28 |
CREATE OR REPLACE FUNCTION clean_up_record_links_before_gl_delete() RETURNS trigger AS $$ |
|
29 |
BEGIN |
|
30 |
DELETE FROM record_links |
|
31 |
WHERE (from_table = 'gl' AND from_id = OLD.id) |
|
32 |
OR (to_table = 'gl' AND to_id = OLD.id); |
|
33 |
RETURN OLD; |
|
34 |
END; |
|
35 |
$$ LANGUAGE plpgsql; |
|
36 |
|
|
37 |
|
|
38 |
CREATE TRIGGER before_delete_gl_trigger |
|
39 |
BEFORE DELETE ON gl FOR EACH ROW EXECUTE |
|
40 |
PROCEDURE clean_up_record_links_before_gl_delete(); |
|
41 |
|
|
42 |
CREATE TRIGGER before_delete_letter_trigger |
|
43 |
BEFORE DELETE ON letter FOR EACH ROW EXECUTE |
|
44 |
PROCEDURE clean_up_record_links_before_letter_delete(); |
Auch abrufbar als: Unified diff
Trigger zum Löschen von RecordLinks auch für verknüpfte Briefe und Dialogbuchugen