]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update31.py
Convert exception handling to Python3 syntax.
[dak.git] / dak / dakdb / update31.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 keep contents of binary packages in tables so we can generate contents.gz files from dak
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2009  Mike O'Connor <stew@debian.org>
9 @license: GNU General Public License version 2 or later
10 """
11
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
26 ################################################################################
27
28
29 ################################################################################
30
31 import psycopg2
32 from daklib.dak_exceptions import DBUpdateError
33
34 ################################################################################
35 def do_update(self):
36     """
37     add trigger to verify that bin_associations aren't added for an
38     illegal suite,arch combination.  Fix override trigger, re-add all
39     3 triggers
40     """
41     print __doc__
42     try:
43         c = self.db.cursor()
44
45         c.execute("""CREATE OR REPLACE FUNCTION check_illegal_suite_arch()
46                       RETURNS trigger AS  $$
47     event = TD["event"]
48     if event == "UPDATE" or event == "INSERT":
49         row = TD["new"]
50         r = plpy.execute(plpy.prepare( \"\"\"SELECT 1 from suite_architectures sa
51                   JOIN binaries b ON b.architecture = sa.architecture
52                   WHERE b.id = $1 and sa.suite = $2\"\"\",
53                 ["int", "int"]),
54                 [row["bin"], row["suite"]])
55         if not len(r):
56             plpy.error("Illegal architecture for this suite")
57
58 $$ LANGUAGE plpythonu VOLATILE;""")
59
60         c.execute( """CREATE OR REPLACE FUNCTION update_contents_for_override() RETURNS trigger AS  $$
61     event = TD["event"]
62     if event == "UPDATE":
63
64         otype = plpy.execute(plpy.prepare("SELECT type from override_type where id=$1",["int"]),[TD["new"]["type"]] )[0];
65         if otype["type"].endswith("deb"):
66             section = plpy.execute(plpy.prepare("SELECT section from section where id=$1",["int"]),[TD["new"]["section"]] )[0];
67
68             table_name = "%s_contents" % otype["type"]
69             plpy.execute(plpy.prepare("UPDATE %s set section=$1 where package=$2 and suite=$3" % table_name,
70                                       ["text","text","int"]),
71                                       [section["section"],
72                                       TD["new"]["package"],
73                                       TD["new"]["suite"]])
74
75 $$ LANGUAGE plpythonu VOLATILE SECURITY DEFINER;
76 """)
77         c.execute( "DROP TRIGGER IF EXISTS illegal_suite_arch_bin_associations_trigger on bin_associations;" )
78
79         c.execute( "DROP TRIGGER IF EXISTS bin_associations_contents_trigger ON bin_associations;" )
80         c.execute( "DROP TRIGGER IF EXISTS override_contents_trigger ON override;" )
81
82         c.execute( """CREATE TRIGGER bin_associations_contents_trigger
83                       AFTER INSERT OR UPDATE OR DELETE ON bin_associations
84                       FOR EACH ROW EXECUTE PROCEDURE update_contents_for_bin_a();""")
85
86         c.execute("""CREATE TRIGGER override_contents_trigger
87                       AFTER UPDATE ON override
88                       FOR EACH ROW EXECUTE PROCEDURE update_contents_for_override();""")
89
90         c.execute( """CREATE TRIGGER illegal_suite_arch_bin_associations_trigger
91                       BEFORE INSERT OR UPDATE ON bin_associations
92                       FOR EACH ROW EXECUTE PROCEDURE check_illegal_suite_arch();""")
93
94         c.execute("UPDATE config SET value = '31' WHERE name = 'db_revision'")
95         self.db.commit()
96
97     except psycopg2.ProgrammingError as msg:
98         self.db.rollback()
99         raise DBUpdateError, "Unable to apply process-new update 31, rollback issued. Error message : %s" % (str(msg))
100