1""" 

2per-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# 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. 

13 

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. 

18 

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 

22 

23################################################################################ 

24 

25import psycopg2 

26from daklib.dak_exceptions import DBUpdateError 

27from daklib.config import Config 

28 

29statements = [ 

30""" 

31ALTER TABLE new_comments 

32ADD COLUMN policy_queue_id INTEGER REFERENCES policy_queue(id) 

33""", 

34 

35""" 

36UPDATE new_comments 

37SET policy_queue_id = (SELECT id FROM policy_queue WHERE queue_name = 'new') 

38""", 

39 

40""" 

41ALTER TABLE new_comments ALTER COLUMN policy_queue_id SET NOT NULL 

42""", 

43 

44""" 

45CREATE OR REPLACE FUNCTION trigger_check_policy_queue_permission() RETURNS TRIGGER 

46SET search_path = public, pg_temp 

47LANGUAGE plpgsql 

48AS $$ 

49DECLARE 

50 v_row RECORD; 

51 v_suite_id suite.id%TYPE; 

52 v_policy_queue_name policy_queue.queue_name%TYPE; 

53BEGIN 

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 

76END; 

77$$ 

78""", 

79 

80""" 

81CREATE 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 

92def do_update(self): 

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))