]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update30.py
Convert exception handling to Python3 syntax.
[dak.git] / dak / dakdb / update30.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Adding content fields
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2010  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 import psycopg2
31 import time
32 from daklib.dak_exceptions import DBUpdateError
33
34 ################################################################################
35
36 def do_update(self):
37     print "fix trigger for bin_contents so that it ignores non deb,udeb"
38
39     try:
40         c = self.db.cursor()
41         c.execute( """CREATE OR REPLACE FUNCTION update_contents_for_bin_a() RETURNS trigger AS  $$
42     event = TD["event"]
43     if event == "DELETE" or event == "UPDATE":
44
45         plpy.execute(plpy.prepare("DELETE FROM deb_contents WHERE binary_id=$1 and suite=$2",
46                                   ["int","int"]),
47                                   [TD["old"]["bin"], TD["old"]["suite"]])
48
49     if event == "INSERT" or event == "UPDATE":
50
51        content_data = plpy.execute(plpy.prepare(
52             \"\"\"SELECT s.section, b.package, b.architecture, ot.type
53             FROM override o
54             JOIN override_type ot on o.type=ot.id
55             JOIN binaries b on b.package=o.package
56             JOIN files f on b.file=f.id
57             JOIN location l on l.id=f.location
58             JOIN section s on s.id=o.section
59             WHERE b.id=$1
60             AND o.suite=$2
61             AND ot.type in ('deb','udeb')
62             \"\"\",
63             ["int", "int"]),
64             [TD["new"]["bin"], TD["new"]["suite"]])[0]
65
66        tablename="%s_contents" % content_data['type']
67
68        plpy.execute(plpy.prepare(\"\"\"DELETE FROM %s
69                    WHERE package=$1 and arch=$2 and suite=$3\"\"\" % tablename,
70                    ['text','int','int']),
71                    [content_data['package'],
72                    content_data['architecture'],
73                    TD["new"]["suite"]])
74
75        filenames = plpy.execute(plpy.prepare(
76            "SELECT bc.file FROM bin_contents bc where bc.binary_id=$1",
77            ["int"]),
78            [TD["new"]["bin"]])
79
80        for filename in filenames:
81            plpy.execute(plpy.prepare(
82                \"\"\"INSERT INTO %s
83                    (filename,section,package,binary_id,arch,suite)
84                    VALUES($1,$2,$3,$4,$5,$6)\"\"\" % tablename,
85                ["text","text","text","int","int","int"]),
86                [filename["file"],
87                 content_data["section"],
88                 content_data["package"],
89                 TD["new"]["bin"],
90                 content_data["architecture"],
91                 TD["new"]["suite"]] )
92 $$ LANGUAGE plpythonu VOLATILE SECURITY DEFINER;
93 """)
94
95         c.execute("UPDATE config SET value = '30' WHERE name = 'db_revision'")
96         self.db.commit()
97
98     except psycopg2.ProgrammingError as msg:
99         self.db.rollback()
100         raise DBUpdateError, "Unable to appy debversion updates, rollback issued. Error message : %s" % (str(msg))