4 """ Database Update Script - debversion """
5 # Copyright © 2008 Michael Casadevall <mcasadevall@debian.org>
6 # Copyright © 2008 Roger Leigh <rleigh@debian.org>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ################################################################################
26 from daklib.dak_exceptions import DBUpdateError
28 ################################################################################
32 print "Note: to be able to enable the the PL/Perl (plperl) procedural language, we do"
33 print "need postgresql-plperl-$postgres-version installed. Make sure that this is the"
34 print "case before you continue. Interrupt if it isn't, sleeping 5 seconds now."
35 print "(We need to be database superuser for this to work!)"
42 print "Enabling PL/Perl language"
43 c.execute("CREATE LANGUAGE plperl;")
44 c.execute("CREATE LANGUAGE plpgsql;")
46 print "Adding debversion type to database."
48 # Not present in all databases, maybe PL/Perl version-dependent?
49 # c.execute("SET SESSION plperl.use_strict TO 't';")
51 c.execute("CREATE DOMAIN debversion AS TEXT;")
52 c.execute("COMMENT ON DOMAIN debversion IS 'Debian package version number';")
54 c.execute("""ALTER DOMAIN debversion
55 ADD CONSTRAINT debversion_syntax
56 CHECK (VALUE !~ '[^-+:.0-9a-zA-Z~]');""")
58 # From Dpkg::Version::parseversion
59 c.execute("""CREATE OR REPLACE FUNCTION debversion_split (debversion)
65 $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
73 if ($ver =~ /(.+)-(.*)$/)
75 $verhash{version} = $1;
76 $verhash{revision} = $2;
80 $verhash{version} = $ver;
81 $verhash{revision} = 0;
84 return [$verhash{'epoch'}, $verhash{'version'}, $verhash{'revision'}];
88 c.execute("""COMMENT ON FUNCTION debversion_split (debversion)
89 IS 'Split debian version into epoch, upstream version and revision';""")
91 c.execute("""CREATE OR REPLACE FUNCTION debversion_epoch (version debversion)
96 split := debversion_split(version);
102 COMMENT ON FUNCTION debversion_epoch (debversion)
103 IS 'Get debian version epoch';
105 CREATE OR REPLACE FUNCTION debversion_version (version debversion)
110 split := debversion_split(version);
115 IMMUTABLE STRICT;""")
116 c.execute("""COMMENT ON FUNCTION debversion_version (debversion)
117 IS 'Get debian version upstream version';""")
119 c.execute("""CREATE OR REPLACE FUNCTION debversion_revision (version debversion)
124 split := debversion_split(version);
129 IMMUTABLE STRICT;""")
130 c.execute("""COMMENT ON FUNCTION debversion_revision (debversion)
131 IS 'Get debian version revision';""")
133 # From Dpkg::Version::parseversion
134 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare_single (version1 text, version2 text)
135 RETURNS integer AS $$
138 ##define order(x) ((x) == '~' ? -1 \
139 # : cisdigit((x)) ? 0 \
141 # : cisalpha((x)) ? (x) \
143 # This comparison is out of dpkg's order to avoid
144 # comparing things to undef and triggering warnings.
145 if (not defined $x or not length $x) {
151 elsif ($x =~ /^\d$/) {
154 elsif ($x =~ /^[A-Z]$/i) {
158 return ord($x) + 256;
164 return @{$a} ? shift @{$a} : undef;
166 my ($val, $ref) = @_;
167 $val = "" if not defined $val;
168 $ref = "" if not defined $ref;
169 my @val = split //,$val;
170 my @ref = split //,$ref;
171 my $vc = next_elem @val;
172 my $rc = next_elem @ref;
173 while (defined $vc or defined $rc) {
175 while ((defined $vc and $vc !~ /^\d$/) or
176 (defined $rc and $rc !~ /^\d$/)) {
177 my $vo = order($vc); my $ro = order($rc);
178 # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
179 return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
180 $vc = next_elem @val; $rc = next_elem @ref;
182 while (defined $vc and $vc eq '0') {
183 $vc = next_elem @val;
185 while (defined $rc and $rc eq '0') {
186 $rc = next_elem @ref;
188 while (defined $vc and $vc =~ /^\d$/ and
189 defined $rc and $rc =~ /^\d$/) {
190 $first_diff = ord($vc) - ord($rc) if !$first_diff;
191 $vc = next_elem @val; $rc = next_elem @ref;
193 return 1 if defined $vc and $vc =~ /^\d$/;
194 return -1 if defined $rc and $rc =~ /^\d$/;
195 return (($first_diff > 0) ? 1 : -1) if $first_diff;
200 IMMUTABLE STRICT;""")
201 c.execute("""COMMENT ON FUNCTION debversion_compare_single (text, text)
202 IS 'Compare upstream or revision parts of Debian versions';""")
204 # Logic only derived from Dpkg::Version::parseversion
205 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare (version1 debversion, version2 debversion)
206 RETURNS integer AS $$
213 split1 := debversion_split(version1);
214 split2 := debversion_split(version2);
216 -- RAISE NOTICE 'Version 1: %', version1;
217 -- RAISE NOTICE 'Version 2: %', version2;
218 -- RAISE NOTICE 'Split 1: %', split1;
219 -- RAISE NOTICE 'Split 2: %', split2;
221 IF split1[1] > split2[1] THEN
223 ELSIF split1[1] < split2[1] THEN
226 result := debversion_compare_single(split1[2], split2[2]);
228 result := debversion_compare_single(split1[3], split2[3]);
236 IMMUTABLE STRICT;""")
237 c.execute("""COMMENT ON FUNCTION debversion_compare (debversion, debversion)
238 IS 'Compare Debian versions';""")
240 c.execute("""CREATE OR REPLACE FUNCTION debversion_eq (version1 debversion, version2 debversion)
241 RETURNS boolean AS $$
246 comp := debversion_compare(version1, version2);
252 IMMUTABLE STRICT;""")
253 c.execute("""COMMENT ON FUNCTION debversion_eq (debversion, debversion)
254 IS 'debversion equal';""")
256 c.execute("""CREATE OR REPLACE FUNCTION debversion_ne (version1 debversion, version2 debversion)
257 RETURNS boolean AS $$
262 comp := debversion_compare(version1, version2);
268 IMMUTABLE STRICT;""")
269 c.execute("""COMMENT ON FUNCTION debversion_ne (debversion, debversion)
270 IS 'debversion not equal';""")
272 c.execute("""CREATE OR REPLACE FUNCTION debversion_lt (version1 debversion, version2 debversion)
273 RETURNS boolean AS $$
278 comp := debversion_compare(version1, version2);
284 IMMUTABLE STRICT;""")
285 c.execute("""COMMENT ON FUNCTION debversion_lt (debversion, debversion)
286 IS 'debversion less-than';""")
288 c.execute("""CREATE OR REPLACE FUNCTION debversion_gt (version1 debversion, version2 debversion) RETURNS boolean AS $$
293 comp := debversion_compare(version1, version2);
299 IMMUTABLE STRICT;""")
300 c.execute("""COMMENT ON FUNCTION debversion_gt (debversion, debversion)
301 IS 'debversion greater-than';""")
303 c.execute("""CREATE OR REPLACE FUNCTION debversion_le (version1 debversion, version2 debversion)
304 RETURNS boolean AS $$
309 comp := debversion_compare(version1, version2);
315 IMMUTABLE STRICT;""")
316 c.execute("""COMMENT ON FUNCTION debversion_le (debversion, debversion)
317 IS 'debversion less-than-or-equal';""")
319 c.execute("""CREATE OR REPLACE FUNCTION debversion_ge (version1 debversion, version2 debversion)
320 RETURNS boolean AS $$
325 comp := debversion_compare(version1, version2);
331 IMMUTABLE STRICT;""")
332 c.execute("""COMMENT ON FUNCTION debversion_ge (debversion, debversion)
333 IS 'debversion greater-than-or-equal';""")
335 c.execute("""CREATE OPERATOR = (
336 PROCEDURE = debversion_eq,
337 LEFTARG = debversion,
338 RIGHTARG = debversion,
341 c.execute("""COMMENT ON OPERATOR = (debversion, debversion)
342 IS 'debversion equal';""")
344 c.execute("""CREATE OPERATOR != (
345 PROCEDURE = debversion_eq,
346 LEFTARG = debversion,
347 RIGHTARG = debversion,
350 c.execute("""COMMENT ON OPERATOR != (debversion, debversion)
351 IS 'debversion not equal';""")
353 c.execute("""CREATE OPERATOR < (
354 PROCEDURE = debversion_lt,
355 LEFTARG = debversion,
356 RIGHTARG = debversion,
359 c.execute("""COMMENT ON OPERATOR < (debversion, debversion)
360 IS 'debversion less-than';""")
362 c.execute("""CREATE OPERATOR > (
363 PROCEDURE = debversion_gt,
364 LEFTARG = debversion,
365 RIGHTARG = debversion,
368 c.execute("""COMMENT ON OPERATOR > (debversion, debversion)
369 IS 'debversion greater-than';""")
371 c.execute("""CREATE OPERATOR <= (
372 PROCEDURE = debversion_le,
373 LEFTARG = debversion,
374 RIGHTARG = debversion,
377 c.execute("""COMMENT ON OPERATOR <= (debversion, debversion)
378 IS 'debversion less-than-or-equal';""")
380 c.execute("""CREATE OPERATOR >= (
381 PROCEDURE = debversion_ge,
382 LEFTARG = debversion,
383 RIGHTARG = debversion,
386 c.execute("""COMMENT ON OPERATOR >= (debversion, debversion)
387 IS 'debversion greater-than-or-equal';""")
389 c.execute("ALTER TABLE source ALTER COLUMN version TYPE debversion;")
390 c.execute("ALTER TABLE binaries ALTER COLUMN version TYPE debversion;")
392 c.execute("UPDATE config SET value = '2' WHERE name = 'db_revision'")
396 except psycopg2.ProgrammingError, msg:
398 raise DBUpdateError, "Unable to appy debversion updates, rollback issued. Error message : %s" % (str(msg))