]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update23.py
merge from ftp-master
[dak.git] / dak / dakdb / update23.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Adding a trainee field to the process-new notes
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 import time
33 from daklib.dak_exceptions import DBUpdateError
34
35 ################################################################################
36
37 def suites():
38     """
39     return a list of suites to operate on
40     """
41     if Config().has_key( "%s::%s" %(options_prefix,"Suite")):
42         suites = utils.split_args(Config()[ "%s::%s" %(options_prefix,"Suite")])
43     else:
44         suites = [ 'unstable', 'testing' ]
45 #            suites = Config().SubTree("Suite").List()
46
47     return suites
48
49 def arches(cursor, suite):
50     """
51     return a list of archs to operate on
52     """
53     arch_list = []
54     cursor.execute("""SELECT s.architecture, a.arch_string
55     FROM suite_architectures s
56     JOIN architecture a ON (s.architecture=a.id)
57     WHERE suite = :suite""", {'suite' : suite })
58
59     while True:
60         r = cursor.fetchone()
61         if not r:
62             break
63
64         if r[1] != "source" and r[1] != "all":
65             arch_list.append((r[0], r[1]))
66
67     return arch_list
68
69 def do_update(self):
70     """
71     Adding contents table as first step to maybe, finally getting rid
72     of apt-ftparchive
73     """
74
75     print __doc__
76
77     try:
78         c = self.db.cursor()
79
80         c.execute("""CREATE TABLE pending_bin_contents (
81         id serial NOT NULL,
82         package text NOT NULL,
83         version debversion NOT NULL,
84         arch int NOT NULL,
85         filename text NOT NULL,
86         type int NOT NULL,
87         PRIMARY KEY(id))""" );
88
89         c.execute("""CREATE TABLE deb_contents (
90         filename text,
91         section text,
92         package text,
93         binary_id integer,
94         arch integer,
95         suite integer,
96         component text)""" )
97
98         c.execute("""CREATE TABLE udeb_contents (
99         filename text,
100         section text,
101         package text,
102         binary_id integer,
103         suite integer,
104         arch integer,
105         component text )""" )
106
107         c.execute("""ALTER TABLE ONLY deb_contents
108         ADD CONSTRAINT deb_contents_arch_fkey
109         FOREIGN KEY (arch) REFERENCES architecture(id)
110         ON DELETE CASCADE;""")
111
112         c.execute("""ALTER TABLE ONLY udeb_contents
113         ADD CONSTRAINT udeb_contents_arch_fkey
114         FOREIGN KEY (arch) REFERENCES architecture(id)
115         ON DELETE CASCADE;""")
116
117         c.execute("""ALTER TABLE ONLY deb_contents
118         ADD CONSTRAINT deb_contents_pkey
119         PRIMARY KEY (filename,package,arch,suite);""")
120
121         c.execute("""ALTER TABLE ONLY udeb_contents
122         ADD CONSTRAINT udeb_contents_pkey
123         PRIMARY KEY (filename,package,arch,suite);""")
124
125         c.execute("""ALTER TABLE ONLY deb_contents
126         ADD CONSTRAINT deb_contents_suite_fkey
127         FOREIGN KEY (suite) REFERENCES suite(id)
128         ON DELETE CASCADE;""")
129
130         c.execute("""ALTER TABLE ONLY udeb_contents
131         ADD CONSTRAINT udeb_contents_suite_fkey
132         FOREIGN KEY (suite) REFERENCES suite(id)
133         ON DELETE CASCADE;""")
134
135         c.execute("""ALTER TABLE ONLY deb_contents
136         ADD CONSTRAINT deb_contents_binary_fkey
137         FOREIGN KEY (binary_id) REFERENCES binaries(id)
138         ON DELETE CASCADE;""")
139
140         c.execute("""ALTER TABLE ONLY udeb_contents
141         ADD CONSTRAINT udeb_contents_binary_fkey
142         FOREIGN KEY (binary_id) REFERENCES binaries(id)
143         ON DELETE CASCADE;""")
144
145         c.execute("""CREATE INDEX ind_deb_contents_binary ON deb_contents(binary_id);""" )
146
147
148         suites = self.suites()
149
150         for suite in [i.lower() for i in suites]:
151             suite_id = DBConn().get_suite_id(suite)
152             arch_list = arches(c, suite_id)
153             arch_list = arches(c, suite_id)
154
155             for (arch_id,arch_str) in arch_list:
156                 c.execute( "CREATE INDEX ind_deb_contents_%s_%s ON deb_contents (arch,suite) WHERE (arch=2 OR arch=%d) AND suite=$d"%(arch_str,suite,arch_id,suite_id) )
157
158             for section, sname in [("debian-installer","main"),
159                                   ("non-free/debian-installer", "nonfree")]:
160                 c.execute( "CREATE INDEX ind_udeb_contents_%s_%s ON udeb_contents (section,suite) WHERE section=%s AND suite=$d"%(sname,suite,section,suite_id) )
161
162
163         c.execute( """CREATE OR REPLACE FUNCTION update_contents_for_bin_a() RETURNS trigger AS  $$
164     event = TD["event"]
165     if event == "DELETE" or event == "UPDATE":
166
167         plpy.execute(plpy.prepare("DELETE FROM deb_contents WHERE binary_id=$1 and suite=$2",
168                                   ["int","int"]),
169                                   [TD["old"]["bin"], TD["old"]["suite"]])
170
171     if event == "INSERT" or event == "UPDATE":
172
173        content_data = plpy.execute(plpy.prepare(
174             """SELECT s.section, b.package, b.architecture, c.name, ot.type
175             FROM override o
176             JOIN override_type ot on o.type=ot.id
177             JOIN binaries b on b.package=o.package
178             JOIN files f on b.file=f.id
179             JOIN location l on l.id=f.location
180             JOIN section s on s.id=o.section
181             JOIN component c on c.id=l.component
182             WHERE b.id=$1
183             AND o.suite=$2
184             """,
185             ["int", "int"]),
186             [TD["new"]["bin"], TD["new"]["suite"]])[0]
187
188        component_str = "";
189        if not content_data["name"] === "main":
190            component_str=content_data["name"]+"/"
191
192        filenames = plpy.execute(plpy.prepare(
193            "SELECT bc.file FROM bin_contents bc where bc.binary_id=$1",
194            ["int"]),
195            [TD["new"]["bin"]])
196
197        for filename in filenames:
198            plpy.execute(plpy.prepare(
199                """INSERT INTO deb_contents
200                    (file,section,package,binary_id,arch,suite,component)
201                    VALUES($1,$2,$3,$4,$5,$6,$7)""",
202                ["text","text","text","int","int","int","text"]),
203                [filename["filename"],
204                 content_data["section"],
205                 content_data["package"],
206                 TD["new"]["bin"],
207                 content_data["architecture"],
208                 TD["new"]["suite"],
209                 component_str])
210 $$ LANGUAGE plpythonu VOLATILE SECURITY DEFINER;
211 """)
212
213
214         c.execute( """CREATE OR REPLACE FUNCTION update_contents_for_override() RETURNS trigger AS  $$
215     event = TD["event"]
216     if event == "UPDATE":
217
218         otype = plpy.execute(plpy.prepare("SELECT type from override_type where id=$1",["int"]),TD["new"]["type"] )[0];
219         if otype["type"].endswith("deb"):
220             table_name = "%s_contents" % otype["type"]
221             plpy.execute(plpy.prepare("UPDATE %s set sections=$1" % table_name
222                                       ["text"]),
223                                       [TD["new"]["section"]])
224
225 $$ LANGUAGE plpythonu VOLATILE SECURITY DEFINER;
226 """)
227         c.execute( """CREATE TRIGGER bin_associations_contents_trigger
228                       AFTER INSERT OR UPDATE OR DELETE ON bin_associations
229                       FOR EACH ROW EXECUTE PROCEDURE update_contents_for_bin_a();""")
230         c.execute("""CREATE TRIGGER override_contents_trigger
231                       AFTER UPDATE ON override
232                       FOR EACH ROW EXECUTE PROCEDURE update_contents_for_override();""")
233
234         self.db.commit()
235
236     except psycopg2.ProgrammingError, msg:
237         self.db.rollback()
238         raise DBUpdateError, "Unable to apply process-new update 14, rollback issued. Error message : %s" % (str(msg))
239