]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update22.py
Try to sanitise changes and queue handling
[dak.git] / dak / dakdb / update22.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Clean up queue SQL
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2009  Mark Hymers <mhy@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 import os
34 import datetime
35 import traceback
36
37 from daklib.dak_exceptions import DBUpdateError
38 from daklib.config import Config
39
40 ################################################################################
41
42 def do_update(self):
43     print "Splitting up queues and fixing general design mistakes"
44
45     try:
46         c = self.db.cursor()
47
48         cnf = Config()
49
50         print "Adding build_queue table"
51         c.execute("""CREATE TABLE build_queue (
52                             id          SERIAL PRIMARY KEY,
53                             queue_name  TEXT NOT NULL UNIQUE,
54                             path        TEXT NOT NULL,
55                             copy_files  BOOL DEFAULT FALSE NOT NULL)""")
56
57         print "Adding policy_queue table"
58         c.execute("""CREATE TABLE policy_queue (
59                             id          SERIAL PRIMARY KEY,
60                             queue_name  TEXT NOT NULL UNIQUE,
61                             path        TEXT NOT NULL)""")
62
63         print "Copying queues"
64         queues = {}
65         c.execute("""SELECT queue.id, queue.queue_name, queue.path, queue.copy_pool_files FROM queue""")
66
67         for q in c.fetchall():
68             queues[q[0]] = q[1]
69             if q[1] in ['accepted', 'buildd']:
70                 # Move to build_queue_table
71                 c.execute("""INSERT INTO build_queue (queue_name, path, copy_files)
72                                    VALUES ('%s', '%s', '%s')""" % (q[1], q[2], q[3]))
73
74             else:
75                 # Move to policy_queue_table
76                 c.execute("""INSERT INTO policy_queue (queue_name, path)
77                                    VALUES ('%s', '%s')""" % (q[1], q[2]))
78
79
80         print "Fixing up build_queue_files"
81         c.execute("""ALTER TABLE queue_files DROP CONSTRAINT queue_files_queueid_fkey""")
82         c.execute("""ALTER TABLE queue_files RENAME TO build_queue_files""")
83         c.execute("""ALTER TABLE build_queue_files RENAME COLUMN queueid TO build_queue_id""")
84
85         c.execute("""UPDATE build_queue_files
86                         SET build_queue_id = (SELECT build_queue.id FROM build_queue
87                                                WHERE build_queue.queue_name =
88                                                 (SELECT queue.queue_name FROM queue
89                                                   WHERE queue.id = build_queue_files.build_queue_id))""")
90
91         c.execute("""ALTER TABLE build_queue_files
92                        ADD CONSTRAINT build_queue_files_build_queue_id_fkey
93                        FOREIGN KEY (build_queue_id)
94                        REFERENCES build_queue(id)
95                        ON DELETE CASCADE""")
96
97
98         c.execute("""ALTER TABLE suite DROP CONSTRAINT suite_policy_queue_fkey""")
99
100         c.execute("""UPDATE suite
101     SET policy_queue_id = (SELECT policy_queue.id FROM policy_queue
102                              WHERE policy_queue.queue_name =
103                               (SELECT queue.queue_name FROM queue
104                                WHERE queue.id = suite.policy_queue_id))""")
105
106         c.execute("""ALTER TABLE suite
107                        ADD CONSTRAINT suite_policy_queue_fkey
108                        FOREIGN KEY (policy_queue_id)
109                        REFERENCES policy_queue (id)
110                        ON DELETE RESTRICT""")
111
112         c.execute("""ALTER TABLE known_changes DROP CONSTRAINT known_changes_approved_for_fkey""")
113         c.execute("""ALTER TABLE known_changes DROP CONSTRAINT known_changes_in_queue_fkey""")
114
115         c.execute("""UPDATE known_changes
116     SET in_queue = (SELECT policy_queue.id FROM policy_queue
117                              WHERE policy_queue.queue_name =
118                               (SELECT queue.queue_name FROM queue
119                                WHERE queue.id = known_changes.in_queue))""")
120
121         c.execute("""ALTER TABLE known_changes
122                        ADD CONSTRAINT known_changes_in_queue_fkey
123                        FOREIGN KEY (in_queue)
124                        REFERENCES policy_queue (id)
125                        ON DELETE RESTRICT""")
126
127
128
129         c.execute("""UPDATE known_changes
130     SET approved_for = (SELECT policy_queue.id FROM policy_queue
131                                WHERE policy_queue.queue_name =
132                                 (SELECT queue.queue_name FROM queue
133                                   WHERE queue.id = known_changes.approved_for))""")
134
135         c.execute("""ALTER TABLE known_changes
136                        ADD CONSTRAINT known_changes_approved_for_fkey
137                        FOREIGN KEY (in_queue)
138                        REFERENCES policy_queue (id)
139                        ON DELETE RESTRICT""")
140
141         c.execute("""ALTER TABLE suite_queue_copy RENAME TO suite_build_queue_copy""")
142
143         c.execute("""ALTER TABLE suite_build_queue_copy DROP CONSTRAINT suite_queue_copy_queue_fkey""")
144
145         c.execute("""ALTER TABLE suite_build_queue_copy RENAME COLUMN queue TO build_queue_id""")
146
147         c.execute("""UPDATE suite_build_queue_copy
148     SET build_queue_id = (SELECT build_queue.id FROM build_queue
149                                  WHERE build_queue.queue_name =
150                                 (SELECT queue.queue_name FROM queue
151                                   WHERE queue.id = suite_build_queue_copy.build_queue_id))""")
152
153         c.execute("""ALTER TABLE suite_build_queue_copy
154                        ADD CONSTRAINT suite_build_queue_copy_build_queue_id_fkey
155                        FOREIGN KEY (build_queue_id)
156                        REFERENCES build_queue (id)
157                        ON DELETE RESTRICT""")
158
159         c.execute("""DROP TABLE changes_pending_files""")
160
161         c.execute("""CREATE TABLE changes_pending_files (
162                             id             SERIAL PRIMARY KEY,
163                             filename       TEXT NOT NULL UNIQUE,
164                             size           BIGINT NOT NULL,
165                             md5sum         TEXT NOT NULL,
166                             sha1sum        TEXT NOT NULL,
167                             sha256sum      TEXT NOT NULL )""")
168
169         c.execute("""CREATE TABLE changes_pending_source (
170                             id             SERIAL PRIMARY KEY,
171                             change_id      INT4 NOT NULL REFERENCES known_changes (id),
172                             source         TEXT NOT NULL,
173                             version        DEBVERSION NOT NULL,
174                             maintainer_id  INT4 NOT NULL REFERENCES maintainer (id),
175                             changedby_id   INT4 NOT NULL REFERENCES maintainer (id),
176                             sig_fpr        INT4 NOT NULL REFERENCES fingerprint (id),
177                             dm_upload_allowed BOOL NOT NULL DEFAULT FALSE )""")
178
179         c.execute("""CREATE TABLE changes_pending_source_files (
180                             pending_source_id INT4 REFERENCES changes_pending_source (id) NOT NULL,
181                             pending_file_id   INT4 REFERENCES changes_pending_files (id) NOT NULL,
182
183                             PRIMARY KEY (pending_source_id, pending_file_id) )""")
184
185         c.execute("""CREATE TABLE changes_pending_binaries (
186                             id                 SERIAL PRIMARY KEY,
187                             change_id          INT4 NOT NULL REFERENCES known_changes (id),
188                             package            TEXT NOT NULL,
189                             version            DEBVERSION NOT NULL,
190                             architecture_id    INT4 REFERENCES architecture (id) NOT NULL,
191                             source_id          INT4 REFERENCES source (id),
192                             pending_source_id  INT4 REFERENCES changes_pending_source (id),
193                             pending_file_id    INT4 REFERENCES changes_pending_files (id),
194
195                             UNIQUE (package, version, architecture_id),
196                             CHECK (source_id IS NOT NULL or pending_source_id IS NOT NULL ) )""")
197
198         print "Getting rid of old queue table"
199         c.execute("""DROP TABLE queue""")
200
201         print "Sorting out permissions"
202
203         for t in ['build_queue', 'policy_queue', 'build_queue_files',
204                   'changes_pending_binaries', 'changes_pending_source_files',
205                   'changes_pending_source', 'changes_pending_files',
206                   'changes_pool_files', 'suite_build_queue_copy']:
207             c.execute("GRANT SELECT ON %s TO public" % t)
208             c.execute("GRANT ALL ON %s TO ftpmaster" % t)
209
210         for s in ['queue_files_id_seq', 'build_queue_id_seq',
211                   'changes_pending_source_id_seq',
212                   'changes_pending_binaries_id_seq',
213                   'changes_pending_files_id_seq',
214                   'changes_pending_source_id_seq',
215                   'known_changes_id_seq',
216                   'policy_queue_id_seq']:
217             c.execute("GRANT USAGE ON %s TO ftpmaster" % s)
218
219         print "Committing"
220         c.execute("UPDATE config SET value = '22' WHERE name = 'db_revision'")
221         self.db.commit()
222
223     except psycopg2.InternalError, msg:
224         self.db.rollback()
225         raise DBUpdateError, "Unable to apply queue_build 21, rollback issued. Error message : %s" % (str(msg))