1"""
2per-queue NEW comments and permissions
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"""
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23################################################################################
25import psycopg2
27from daklib.dak_exceptions import DBUpdateError
29statements = [
30 """
31ALTER TABLE new_comments
32ADD COLUMN policy_queue_id INTEGER REFERENCES policy_queue(id)
33""",
34 """
35UPDATE new_comments
36SET policy_queue_id = (SELECT id FROM policy_queue WHERE queue_name = 'new')
37""",
38 """
39ALTER TABLE new_comments ALTER COLUMN policy_queue_id SET NOT NULL
40""",
41 """
42CREATE OR REPLACE FUNCTION trigger_check_policy_queue_permission() RETURNS TRIGGER
43SET search_path = public, pg_temp
44LANGUAGE plpgsql
45AS $$
46DECLARE
47 v_row RECORD;
48 v_suite_id suite.id%TYPE;
49 v_policy_queue_name policy_queue.queue_name%TYPE;
50BEGIN
52 CASE TG_OP
53 WHEN 'INSERT', 'UPDATE' THEN
54 v_row := NEW;
55 WHEN 'DELETE' THEN
56 v_row := OLD;
57 ELSE
58 RAISE EXCEPTION 'Unexpected TG_OP (%)', TG_OP;
59 END CASE;
61 IF TG_OP = 'UPDATE' AND OLD.policy_queue_id != NEW.policy_queue_id THEN
62 RAISE EXCEPTION 'Cannot change policy_queue_id';
63 END IF;
65 SELECT suite_id, queue_name INTO STRICT v_suite_id, v_policy_queue_name
66 FROM policy_queue WHERE id = v_row.policy_queue_id;
67 IF NOT has_suite_permission(TG_OP, v_suite_id) THEN
68 RAISE EXCEPTION 'Not allowed to % in %', TG_OP, v_policy_queue_name;
69 END IF;
71 RETURN v_row;
73END;
74$$
75""",
76 """
77CREATE CONSTRAINT TRIGGER trigger_new_comments_permission
78 AFTER INSERT OR UPDATE OR DELETE
79 ON new_comments
80 FOR EACH ROW
81 EXECUTE PROCEDURE trigger_check_policy_queue_permission()
82""",
83]
85################################################################################
88def do_update(self):
89 print(__doc__)
90 try:
91 c = self.db.cursor()
93 for stmt in statements:
94 c.execute(stmt)
96 c.execute("UPDATE config SET value = '91' WHERE name = 'db_revision'")
97 self.db.commit()
99 except psycopg2.ProgrammingError as msg:
100 self.db.rollback()
101 raise DBUpdateError(
102 "Unable to apply sick update 91, rollback issued. Error message: {0}".format(
103 msg
104 )
105 )