]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update19.py
Convert exception handling to Python3 syntax.
[dak.git] / dak / dakdb / update19.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Move to using the C version of debversion
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 "Converting database to use new C based debversion type"
44
45     try:
46         c = self.db.cursor()
47
48         print "Temporarily converting columns to TEXT"
49         c.execute("ALTER TABLE binaries ALTER COLUMN version TYPE TEXT")
50         c.execute("ALTER TABLE source ALTER COLUMN version TYPE TEXT")
51         c.execute("ALTER TABLE upload_blocks ALTER COLUMN version TYPE TEXT")
52         c.execute("ALTER TABLE pending_content_associations ALTER COLUMN version TYPE TEXT")
53
54         print "Dropping old debversion type"
55         c.execute("DROP OPERATOR >(debversion, debversion)")
56         c.execute("DROP OPERATOR <(debversion, debversion)")
57         c.execute("DROP OPERATOR <=(debversion, debversion)")
58         c.execute("DROP OPERATOR >=(debversion, debversion)")
59         c.execute("DROP OPERATOR =(debversion, debversion)")
60         c.execute("DROP OPERATOR <>(debversion, debversion)")
61         c.execute("DROP FUNCTION debversion_eq(debversion,debversion)")
62         c.execute("DROP FUNCTION debversion_ge(debversion,debversion)")
63         c.execute("DROP FUNCTION debversion_gt(debversion,debversion)")
64         c.execute("DROP FUNCTION debversion_le(debversion,debversion)")
65         c.execute("DROP FUNCTION debversion_lt(debversion,debversion)")
66         c.execute("DROP FUNCTION debversion_ne(debversion,debversion)")
67         c.execute("DROP FUNCTION debversion_compare(debversion,debversion)")
68         c.execute("DROP FUNCTION debversion_revision(debversion)")
69         c.execute("DROP FUNCTION debversion_version(debversion)")
70         c.execute("DROP FUNCTION debversion_epoch(debversion)")
71         c.execute("DROP FUNCTION debversion_split(debversion)")
72         c.execute("DROP TYPE debversion")
73
74         # URGH - kill me now
75         print "Importing new debversion type"
76         f = open('/usr/share/postgresql/8.4/contrib/debversion.sql', 'r')
77         cmds = []
78         curcmd = ''
79         for j in f.readlines():
80             j = j.replace('\t', '').replace('\n', '').split('--')[0]
81             if not j.startswith('--'):
82                 jj = j.split(';')
83                 curcmd += " " + jj[0]
84                 if len(jj) > 1:
85                     for jjj in jj[1:]:
86                         if jjj.strip() == '':
87                             cmds.append(curcmd)
88                             curcmd = ''
89                         else:
90                             curcmd += " " + jjj
91
92         for cm in cmds:
93             c.execute(cm)
94
95         print "Converting columns to new debversion type"
96         c.execute("ALTER TABLE binaries ALTER COLUMN version TYPE debversion")
97         c.execute("ALTER TABLE source ALTER COLUMN version TYPE debversion")
98         c.execute("ALTER TABLE upload_blocks ALTER COLUMN version TYPE debversion")
99         c.execute("ALTER TABLE pending_content_associations ALTER COLUMN version TYPE debversion")
100
101         print "Committing"
102         c.execute("UPDATE config SET value = '19' WHERE name = 'db_revision'")
103         self.db.commit()
104
105     except psycopg2.InternalError as msg:
106         self.db.rollback()
107         raise DBUpdateError, "Unable to apply debversion update 19, rollback issued. Error message : %s" % (str(msg))