Revision 90a2c60a
Von Moritz Bunkus vor mehr als 10 Jahren hinzugefügt
sql/Pg-upgrade2/delete_cvars_on_trans_deletion.sql | ||
---|---|---|
1 |
-- @tag: delete_cvars_on_trans_deletion |
|
2 |
-- @description: Einträge in benutzerdefinierten Variablen löschen, deren Bezugsbelege gelöscht wurde |
|
3 |
-- @depends: release_3_1_0 |
|
4 |
|
|
5 |
-- 1. Alle benutzerdefinierten Variablen löschen, für die es keine |
|
6 |
-- Einträge in den dazugehörigen Tabellen mehr gibt. |
|
7 |
|
|
8 |
-- 1.1. Alle CVars für Artikel selber (sub_module ist leer): |
|
9 |
DELETE FROM custom_variables |
|
10 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC')) |
|
11 |
AND (COALESCE(sub_module, '') = '') |
|
12 |
AND (trans_id NOT IN (SELECT id FROM parts)); |
|
13 |
|
|
14 |
-- 1.2. Alle CVars für Angebote/Aufträge, Lieferscheine, Rechnungen |
|
15 |
-- (sub_module gesetzt): |
|
16 |
DELETE FROM custom_variables |
|
17 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC')) |
|
18 |
AND (sub_module = 'orderitems') |
|
19 |
AND (trans_id NOT IN (SELECT id FROM orderitems)); |
|
20 |
|
|
21 |
DELETE FROM custom_variables |
|
22 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC')) |
|
23 |
AND (sub_module = 'delivery_order_items') |
|
24 |
AND (trans_id NOT IN (SELECT id FROM delivery_order_items)); |
|
25 |
|
|
26 |
DELETE FROM custom_variables |
|
27 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'IC')) |
|
28 |
AND (sub_module = 'invoice') |
|
29 |
AND (trans_id NOT IN (SELECT id FROM invoice)); |
|
30 |
|
|
31 |
-- 1.3. Alle CVars für Kunden/Lieferanten: |
|
32 |
DELETE FROM custom_variables |
|
33 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'CT')) |
|
34 |
AND (trans_id NOT IN (SELECT id FROM customer UNION SELECT id FROM vendor)); |
|
35 |
|
|
36 |
-- 1.4. Alle CVars für Ansprechpersonen: |
|
37 |
DELETE FROM custom_variables |
|
38 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'Contacts')) |
|
39 |
AND (trans_id NOT IN (SELECT cp_id FROM contacts)); |
|
40 |
|
|
41 |
-- 1.5. Alle CVars für Projekte: |
|
42 |
DELETE FROM custom_variables |
|
43 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = 'Projects')) |
|
44 |
AND (trans_id NOT IN (SELECT id FROM project)); |
|
45 |
|
|
46 |
-- 2. Triggerfunktionen erstellen, die die benutzerdefinierten |
|
47 |
-- Variablen löschen. |
|
48 |
|
|
49 |
-- 2.1. Parametrisierte Backend-Funktion zum Löschen: |
|
50 |
CREATE OR REPLACE FUNCTION delete_custom_variables_with_sub_module(config_module TEXT, cvar_sub_module TEXT, old_id INTEGER) |
|
51 |
RETURNS BOOLEAN AS $$ |
|
52 |
BEGIN |
|
53 |
DELETE FROM custom_variables |
|
54 |
WHERE (config_id IN (SELECT id FROM custom_variable_configs WHERE module = config_module)) |
|
55 |
AND (COALESCE(sub_module, '') = cvar_sub_module) |
|
56 |
AND (trans_id = old_id); |
|
57 |
|
|
58 |
RETURN TRUE; |
|
59 |
END; |
|
60 |
$$ LANGUAGE plpgsql; |
|
61 |
|
|
62 |
-- 2.2. Nun die Funktionen, die als Trigger aufgerufen wird und die |
|
63 |
-- entscheidet, wie genau zu löschen ist: |
|
64 |
CREATE OR REPLACE FUNCTION delete_custom_variables_trigger() |
|
65 |
RETURNS TRIGGER AS $$ |
|
66 |
BEGIN |
|
67 |
IF (TG_TABLE_NAME IN ('orderitems', 'delivery_order_items', 'invoice')) THEN |
|
68 |
PERFORM delete_custom_variables_with_sub_module('IC', TG_TABLE_NAME, old.id); |
|
69 |
END IF; |
|
70 |
|
|
71 |
IF (TG_TABLE_NAME = 'parts') THEN |
|
72 |
PERFORM delete_custom_variables_with_sub_module('IC', '', old.id); |
|
73 |
END IF; |
|
74 |
|
|
75 |
IF (TG_TABLE_NAME IN ('customer', 'vendor')) THEN |
|
76 |
PERFORM delete_custom_variables_with_sub_module('CT', '', old.id); |
|
77 |
END IF; |
|
78 |
|
|
79 |
IF (TG_TABLE_NAME = 'contacts') THEN |
|
80 |
PERFORM delete_custom_variables_with_sub_module('Contacts', '', old.id); |
|
81 |
END IF; |
|
82 |
|
|
83 |
IF (TG_TABLE_NAME = 'project') THEN |
|
84 |
PERFORM delete_custom_variables_with_sub_module('Projects', '', old.id); |
|
85 |
END IF; |
|
86 |
|
|
87 |
RETURN old; |
|
88 |
END; |
|
89 |
$$ LANGUAGE plpgsql; |
|
90 |
|
|
91 |
-- 3. Die eigentlichen Trigger erstellen: |
|
92 |
|
|
93 |
-- 3.1. orderitems |
|
94 |
DROP TRIGGER IF EXISTS orderitems_delete_custom_variables_after_deletion ON orderitems; |
|
95 |
|
|
96 |
CREATE TRIGGER orderitems_delete_custom_variables_after_deletion |
|
97 |
AFTER DELETE ON orderitems |
|
98 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
99 |
|
|
100 |
-- 3.2. delivery_order_items |
|
101 |
DROP TRIGGER IF EXISTS delivery_order_items_delete_custom_variables_after_deletion ON delivery_order_items; |
|
102 |
|
|
103 |
CREATE TRIGGER delivery_order_items_delete_custom_variables_after_deletion |
|
104 |
AFTER DELETE ON delivery_order_items |
|
105 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
106 |
|
|
107 |
-- 3.3. invoice |
|
108 |
DROP TRIGGER IF EXISTS invoice_delete_custom_variables_after_deletion ON invoice; |
|
109 |
|
|
110 |
CREATE TRIGGER invoice_delete_custom_variables_after_deletion |
|
111 |
AFTER DELETE ON invoice |
|
112 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
113 |
|
|
114 |
-- 3.4. parts |
|
115 |
DROP TRIGGER IF EXISTS parts_delete_custom_variables_after_deletion ON parts; |
|
116 |
|
|
117 |
CREATE TRIGGER parts_delete_custom_variables_after_deletion |
|
118 |
AFTER DELETE ON parts |
|
119 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
120 |
|
|
121 |
-- 3.5. customer |
|
122 |
DROP TRIGGER IF EXISTS customer_delete_custom_variables_after_deletion ON customer; |
|
123 |
|
|
124 |
CREATE TRIGGER customer_delete_custom_variables_after_deletion |
|
125 |
AFTER DELETE ON customer |
|
126 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
127 |
|
|
128 |
-- 3.6. vendor |
|
129 |
DROP TRIGGER IF EXISTS vendor_delete_custom_variables_after_deletion ON vendor; |
|
130 |
|
|
131 |
CREATE TRIGGER vendor_delete_custom_variables_after_deletion |
|
132 |
AFTER DELETE ON vendor |
|
133 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
134 |
|
|
135 |
-- 3.7. contacts |
|
136 |
DROP TRIGGER IF EXISTS contacts_delete_custom_variables_after_deletion ON contacts; |
|
137 |
|
|
138 |
CREATE TRIGGER contacts_delete_custom_variables_after_deletion |
|
139 |
AFTER DELETE ON contacts |
|
140 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
|
141 |
|
|
142 |
-- 3.8. project |
|
143 |
DROP TRIGGER IF EXISTS project_delete_custom_variables_after_deletion ON project; |
|
144 |
|
|
145 |
CREATE TRIGGER project_delete_custom_variables_after_deletion |
|
146 |
AFTER DELETE ON project |
|
147 |
FOR EACH ROW EXECUTE PROCEDURE delete_custom_variables_trigger(); |
Auch abrufbar als: Unified diff
CVars: Einträge beim Löschen dazugehöriger Belege automatisch löschen