]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update30.py
2cba65897372fb174f2f75bedb166dd0bca07200
[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             \"\"\",
62             ["int", "int"]),
63             [TD["new"]["bin"], TD["new"]["suite"]])[0]
64
65        if content_data['type'] not in ['deb', 'udeb']:
66            return
67
68        tablename="%s_contents" % content_data['type']
69
70        plpy.execute(plpy.prepare(\"\"\"DELETE FROM %s
71                    WHERE package=$1 and arch=$2 and suite=$3\"\"\" % tablename,
72                    ['text','int','int']),
73                    [content_data['package'],
74                    content_data['architecture'],
75                    TD["new"]["suite"]])
76
77        filenames = plpy.execute(plpy.prepare(
78            "SELECT bc.file FROM bin_contents bc where bc.binary_id=$1",
79            ["int"]),
80            [TD["new"]["bin"]])
81
82        for filename in filenames:
83            plpy.execute(plpy.prepare(
84                \"\"\"INSERT INTO %s
85                    (filename,section,package,binary_id,arch,suite)
86                    VALUES($1,$2,$3,$4,$5,$6)\"\"\" % tablename,
87                ["text","text","text","int","int","int"]),
88                [filename["file"],
89                 content_data["section"],
90                 content_data["package"],
91                 TD["new"]["bin"],
92                 content_data["architecture"],
93                 TD["new"]["suite"]] )
94 $$ LANGUAGE plpythonu VOLATILE SECURITY DEFINER;
95 """)
96
97         c.execute("UPDATE config SET value = '30' WHERE name = 'db_revision'")
98         self.db.commit()
99
100     except psycopg2.ProgrammingError, msg:
101         self.db.rollback()
102         raise DBUpdateError, "Unable to appy debversion updates, rollback issued. Error message : %s" % (str(msg))