]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update21.py
Convert exception handling to Python3 syntax.
[dak.git] / dak / dakdb / update21.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Modify queue autobuild support
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 "Updating queue_build table"
44
45     try:
46         c = self.db.cursor()
47
48         cnf = Config()
49
50         print "Adding copy_files field to queue table"
51         c.execute("ALTER TABLE queue ADD copy_pool_files BOOL NOT NULL DEFAULT FALSE")
52
53         print "Adding queue_files table"
54
55         c.execute("""CREATE TABLE queue_files (
56     id            SERIAL PRIMARY KEY,
57     queueid       INT4 NOT NULL REFERENCES queue(id) ON DELETE RESTRICT,
58     insertdate    TIMESTAMP NOT NULL DEFAULT now(),
59     lastused      TIMESTAMP DEFAULT NULL,
60     filename      TEXT NOT NULL,
61     fileid        INT4 REFERENCES files(id) ON DELETE CASCADE)""")
62
63         c.execute("""SELECT queue_build.filename, queue_build.last_used, queue_build.queue
64                        FROM queue_build""")
65
66         for r in c.fetchall():
67             print r[0]
68             filename = r[0]
69             last_used = r[1]
70             queue = r[2]
71             try:
72                 endlink = os.readlink(filename)
73                 c.execute("SELECT files.id FROM files WHERE filename LIKE '%%%s'" % endlink[endlink.rindex('/')+1:])
74                 f = c.fetchone()
75                 c.execute("""INSERT INTO queue_files (queueid, lastused, filename, fileid) VALUES
76                                                      (%s, now(), %s, %s)""", (queue, filename[filename.rindex('/')+1:], f[0]))
77             except OSError as e:
78                 print "Can't find file %s (%s)" % (filename, e)
79
80         print "Dropping old queue_build table"
81         c.execute("DROP TABLE queue_build")
82
83         print "Adding changes_pending_files table"
84         c.execute("""CREATE TABLE changes_pending_files (
85                         id           SERIAL PRIMARY KEY,
86                         changeid     INT4 NOT NULL REFERENCES known_changes(id) ON DELETE CASCADE,
87                         filename     TEXT NOT NULL,
88                         source       BOOL NOT NULL DEFAULT FALSE,
89                         filesize     BIGINT NOT NULL,
90                         md5sum       TEXT NOT NULL,
91                         sha1sum      TEXT NOT NULL,
92                         sha256sum    TEXT NOT NULL)""")
93
94
95         print "Adding changes_pool_files table"
96         c.execute("""CREATE TABLE changes_pool_files (
97                         changeid     INT4 NOT NULL REFERENCES known_changes(id) ON DELETE CASCADE,
98                         fileid       INT4 NOT NULL REFERENCES files(id) ON DELETE RESTRICT,
99
100                         PRIMARY KEY (changeid, fileid))""")
101
102         print "Adding suite_queue_copy table"
103         c.execute("""CREATE TABLE suite_queue_copy (
104                         suite        INT4 NOT NULL REFERENCES suite(id),
105                         queue        INT4 NOT NULL REFERENCES queue(id),
106
107                         PRIMARY KEY (suite, queue))""")
108
109         # Link all suites from accepted
110         c.execute("""SELECT suite.id FROM suite""")
111         for s in c.fetchall():
112             c.execute("""INSERT INTO suite_queue_copy (suite, queue) VALUES (%s, (SELECT id FROM queue WHERE queue_name = 'accepted'))""", s)
113
114         # Parse the config and add any buildd stuff
115         cnf = Config()
116         c.execute("""INSERT INTO queue (queue_name, path) VALUES ('buildd', '%s')""" % cnf["Dir::QueueBuild"].rstrip('/'))
117
118         for s in cnf.ValueList("Dinstall::QueueBuildSuites"):
119             c.execute("""INSERT INTO suite_queue_copy (suite, queue)
120                               VALUES ( (SELECT id FROM suite WHERE suite_name = '%s'),
121                                        (SELECT id FROM queue WHERE queue_name = 'buildd'))""" % s.lower())
122
123         print "Committing"
124         c.execute("UPDATE config SET value = '21' WHERE name = 'db_revision'")
125         self.db.commit()
126
127     except psycopg2.InternalError as msg:
128         self.db.rollback()
129         raise DBUpdateError, "Unable to apply queue_build 21, rollback issued. Error message : %s" % (str(msg))