1 """
2 per-queue NEW comments and permissions
3
4 @contact: Debian FTP Master <ftpmaster@debian.org>
5 @copyright: 2012 Ansgar Burchardt <ansgar@debian.org>
6 @license: GNU General Public License version 2 or later
7 """
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import psycopg2
26 from daklib.dak_exceptions import DBUpdateError
27 from daklib.config import Config
28
29 statements = [
30 """
31 ALTER TABLE new_comments
32 ADD COLUMN policy_queue_id INTEGER REFERENCES policy_queue(id)
33 """,
34
35 """
36 UPDATE new_comments
37 SET policy_queue_id = (SELECT id FROM policy_queue WHERE queue_name = 'new')
38 """,
39
40 """
41 ALTER TABLE new_comments ALTER COLUMN policy_queue_id SET NOT NULL
42 """,
43
44 """
45 CREATE OR REPLACE FUNCTION trigger_check_policy_queue_permission() RETURNS TRIGGER
46 SET search_path = public, pg_temp
47 LANGUAGE plpgsql
48 AS $$
49 DECLARE
50 v_row RECORD;
51 v_suite_id suite.id%TYPE;
52 v_policy_queue_name policy_queue.queue_name%TYPE;
53 BEGIN
54
55 CASE TG_OP
56 WHEN 'INSERT', 'UPDATE' THEN
57 v_row := NEW;
58 WHEN 'DELETE' THEN
59 v_row := OLD;
60 ELSE
61 RAISE EXCEPTION 'Unexpected TG_OP (%)', TG_OP;
62 END CASE;
63
64 IF TG_OP = 'UPDATE' AND OLD.policy_queue_id != NEW.policy_queue_id THEN
65 RAISE EXCEPTION 'Cannot change policy_queue_id';
66 END IF;
67
68 SELECT suite_id, queue_name INTO STRICT v_suite_id, v_policy_queue_name
69 FROM policy_queue WHERE id = v_row.policy_queue_id;
70 IF NOT has_suite_permission(TG_OP, v_suite_id) THEN
71 RAISE EXCEPTION 'Not allowed to % in %', TG_OP, v_policy_queue_name;
72 END IF;
73
74 RETURN v_row;
75
76 END;
77 $$
78 """,
79
80 """
81 CREATE CONSTRAINT TRIGGER trigger_new_comments_permission
82 AFTER INSERT OR UPDATE OR DELETE
83 ON new_comments
84 FOR EACH ROW
85 EXECUTE PROCEDURE trigger_check_policy_queue_permission()
86 """,
87 ]
88
89
90
91
93 print(__doc__)
94 try:
95 cnf = Config()
96
97 c = self.db.cursor()
98
99 for stmt in statements:
100 c.execute(stmt)
101
102 c.execute("UPDATE config SET value = '91' WHERE name = 'db_revision'")
103 self.db.commit()
104
105 except psycopg2.ProgrammingError as msg:
106 self.db.rollback()
107 raise DBUpdateError('Unable to apply sick update 91, rollback issued. Error message: {0}'.format(msg))
108