]> wagnertech.de Git - mfinanz.git/blob - sql/Pg-upgrade2/requirement_spec_items_update_trigger_fix2.sql
Pflichtenheftzeitschätzung: DB-Update-Trigger-Fix 2
[mfinanz.git] / sql / Pg-upgrade2 / requirement_spec_items_update_trigger_fix2.sql
1 -- @tag: requirement_spec_items_update_trigger_fix2
2 -- @description: Fixes für Update-Trigger bei Pflichtenheften
3 -- @depends: requirement_spec_delete_trigger_fix
4
5 -- Trigger for updating time_estimation of function blocks from their
6 -- children. item_id is the ID of the item that needs to be updated
7 -- (or NULL if the requirement spec itself must be updated/a section
8 -- was changed).
9 CREATE OR REPLACE FUNCTION update_requirement_spec_item_time_estimation(item_id INTEGER, item_requirement_spec_id INTEGER) RETURNS BOOLEAN AS $$
10   DECLARE
11     current_row RECORD;
12     new_row     RECORD;
13   BEGIN
14     IF EXISTS(
15       SELECT *
16       FROM trigger_information
17       WHERE ((key = 'deleting_requirement_spec_item') AND (value = CAST(item_id                  AS TEXT)))
18          OR ((key = 'deleting_requirement_spec')      AND (value = CAST(item_requirement_spec_id AS TEXT)))
19       LIMIT 1
20     ) THEN
21       RAISE DEBUG 'updateRSIE: item_id % or requirement_spec_id % is about to be deleted; do not update', item_id, requirement_spec_id;
22       RETURN FALSE;
23     END IF;
24
25     -- item_id IS NULL means that a section has been updated. The
26     -- requirement spec itself must therefore be updated.
27     IF item_id IS NULL THEN
28       SELECT COALESCE(time_estimation, 0) AS time_estimation
29       INTO current_row
30       FROM requirement_specs
31       WHERE id = item_requirement_spec_id;
32
33       SELECT COALESCE(SUM(time_estimation), 0) AS time_estimation
34       INTO new_row
35       FROM requirement_spec_items
36       WHERE (parent_id IS NULL)
37         AND (requirement_spec_id = item_requirement_spec_id);
38
39       IF current_row.time_estimation <> new_row.time_estimation THEN
40         RAISE DEBUG 'updateRSIE: updating requirement_spec % itself: old estimation % new %.', item_requirement_spec_id, current_row.time_estimation, new_row.time_estimation;
41
42         UPDATE requirement_specs
43         SET time_estimation = new_row.time_estimation
44         WHERE id = item_requirement_spec_id;
45       END IF;
46
47       RETURN TRUE;
48     END IF;
49
50     -- If we're here it means that either a sub-function-block or a
51     -- function-block has been updated. item_id is the parent's ID of
52     -- the updated item -- meaning the ID of the item that needs to be
53     -- updated now.
54
55     SELECT COALESCE(time_estimation, 0) AS time_estimation
56     INTO current_row
57     FROM requirement_spec_items
58     WHERE id = item_id;
59
60     SELECT COALESCE(SUM(time_estimation), 0) AS time_estimation
61     INTO new_row
62     FROM requirement_spec_items
63     WHERE (parent_id = item_id);
64
65     IF current_row.time_estimation = new_row.time_estimation THEN
66       RAISE DEBUG 'updateRSIE: item %: nothing to do', item_id;
67       RETURN TRUE;
68     END IF;
69
70     RAISE DEBUG 'updateRSIE: updating item %: old estimation % new %.', item_id, current_row.time_estimation, new_row.time_estimation;
71
72     UPDATE requirement_spec_items
73     SET time_estimation = new_row.time_estimation
74     WHERE id = item_id;
75
76     RETURN TRUE;
77   END;
78 $$ LANGUAGE plpgsql;
79
80 CREATE OR REPLACE FUNCTION recalculate_spec_item_time_estimation(the_requirement_spec_id INTEGER) RETURNS BOOLEAN AS $$
81   DECLARE
82     item RECORD;
83   BEGIN
84     FOR item IN
85       SELECT DISTINCT parent_id
86       FROM requirement_spec_items
87       WHERE (requirement_spec_id = the_requirement_spec_id)
88         AND (item_type           = 'sub-function-block')
89     LOOP
90       RAISE DEBUG 'hmm function-block with sub: %', item.parent_id;
91       PERFORM update_requirement_spec_item_time_estimation(item.parent_id, the_requirement_spec_id);
92     END LOOP;
93
94     FOR item IN
95       SELECT DISTINCT parent_id
96       FROM requirement_spec_items
97       WHERE (requirement_spec_id = the_requirement_spec_id)
98         AND (item_type           = 'function-block')
99         AND (id NOT IN (
100           SELECT parent_id
101           FROM requirement_spec_items
102           WHERE (requirement_spec_id = the_requirement_spec_id)
103             AND (item_type           = 'sub-function-block')
104         ))
105     LOOP
106       RAISE DEBUG 'hmm section with function-block: %', item.parent_id;
107       PERFORM update_requirement_spec_item_time_estimation(item.parent_id, the_requirement_spec_id);
108     END LOOP;
109
110     PERFORM update_requirement_spec_item_time_estimation(NULL, the_requirement_spec_id);
111
112     RETURN TRUE;
113   END;
114 $$ LANGUAGE plpgsql;
115
116 CREATE OR REPLACE FUNCTION recalculate_all_spec_item_time_estimations() RETURNS BOOLEAN AS $$
117   DECLARE
118     rspec RECORD;
119   BEGIN
120     FOR rspec IN SELECT id FROM requirement_specs LOOP
121       PERFORM recalculate_spec_item_time_estimation(rspec.id);
122     END LOOP;
123
124     RETURN TRUE;
125   END;
126 $$ LANGUAGE plpgsql;
127
128 SELECT recalculate_all_spec_item_time_estimations();