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 ################################################################################
31 print "Note: to be able to enable the the PL/Perl (plperl) procedural language, we do"
32 print "need postgresql-plperl-$postgres-version installed. Make sure that this is the"
33 print "case before you continue. Interrupt if it isn't, sleeping 5 seconds now."
34 print "(We need to be database superuser for this to work!)"
40 print "Enabling PL/Perl language"
41 c.execute("CREATE LANGUAGE plperl;")
42 c.execute("CREATE LANGUAGE plpgsql;")
44 print "Adding debversion type to database."
46 # Not present in all databases, maybe PL/Perl version-dependent?
47 # c.execute("SET SESSION plperl.use_strict TO 't';")
49 c.execute("CREATE DOMAIN debversion AS TEXT;")
50 c.execute("COMMENT ON DOMAIN debversion IS 'Debian package version number';")
52 c.execute("""ALTER DOMAIN debversion
53 ADD CONSTRAINT debversion_syntax
54 CHECK (VALUE !~ '[^-+:.0-9a-zA-Z~]');""")
56 # From Dpkg::Version::parseversion
57 c.execute("""CREATE OR REPLACE FUNCTION debversion_split (debversion)
63 $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
71 if ($ver =~ /(.+)-(.*)$/)
73 $verhash{version} = $1;
74 $verhash{revision} = $2;
78 $verhash{version} = $ver;
79 $verhash{revision} = 0;
82 return [$verhash{'epoch'}, $verhash{'version'}, $verhash{'revision'}];
86 c.execute("""COMMENT ON FUNCTION debversion_split (debversion)
87 IS 'Split debian version into epoch, upstream version and revision';""")
89 c.execute("""CREATE OR REPLACE FUNCTION debversion_epoch (version debversion)
94 split := debversion_split(version);
100 COMMENT ON FUNCTION debversion_epoch (debversion)
101 IS 'Get debian version epoch';
103 CREATE OR REPLACE FUNCTION debversion_version (version debversion)
108 split := debversion_split(version);
113 IMMUTABLE STRICT;""")
114 c.execute("""COMMENT ON FUNCTION debversion_version (debversion)
115 IS 'Get debian version upstream version';""")
117 c.execute("""CREATE OR REPLACE FUNCTION debversion_revision (version debversion)
122 split := debversion_split(version);
127 IMMUTABLE STRICT;""")
128 c.execute("""COMMENT ON FUNCTION debversion_revision (debversion)
129 IS 'Get debian version revision';""")
131 # From Dpkg::Version::parseversion
132 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare_single (version1 text, version2 text)
133 RETURNS integer AS $$
136 ##define order(x) ((x) == '~' ? -1 \
137 # : cisdigit((x)) ? 0 \
139 # : cisalpha((x)) ? (x) \
141 # This comparison is out of dpkg's order to avoid
142 # comparing things to undef and triggering warnings.
143 if (not defined $x or not length $x) {
149 elsif ($x =~ /^\d$/) {
152 elsif ($x =~ /^[A-Z]$/i) {
156 return ord($x) + 256;
162 return @{$a} ? shift @{$a} : undef;
164 my ($val, $ref) = @_;
165 $val = "" if not defined $val;
166 $ref = "" if not defined $ref;
167 my @val = split //,$val;
168 my @ref = split //,$ref;
169 my $vc = next_elem @val;
170 my $rc = next_elem @ref;
171 while (defined $vc or defined $rc) {
173 while ((defined $vc and $vc !~ /^\d$/) or
174 (defined $rc and $rc !~ /^\d$/)) {
175 my $vo = order($vc); my $ro = order($rc);
176 # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
177 return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
178 $vc = next_elem @val; $rc = next_elem @ref;
180 while (defined $vc and $vc eq '0') {
181 $vc = next_elem @val;
183 while (defined $rc and $rc eq '0') {
184 $rc = next_elem @ref;
186 while (defined $vc and $vc =~ /^\d$/ and
187 defined $rc and $rc =~ /^\d$/) {
188 $first_diff = ord($vc) - ord($rc) if !$first_diff;
189 $vc = next_elem @val; $rc = next_elem @ref;
191 return 1 if defined $vc and $vc =~ /^\d$/;
192 return -1 if defined $rc and $rc =~ /^\d$/;
193 return (($first_diff > 0) ? 1 : -1) if $first_diff;
198 IMMUTABLE STRICT;""")
199 c.execute("""COMMENT ON FUNCTION debversion_compare_single (text, text)
200 IS 'Compare upstream or revision parts of Debian versions';""")
202 # Logic only derived from Dpkg::Version::parseversion
203 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare (version1 debversion, version2 debversion)
204 RETURNS integer AS $$
211 split1 := debversion_split(version1);
212 split2 := debversion_split(version2);
214 -- RAISE NOTICE 'Version 1: %', version1;
215 -- RAISE NOTICE 'Version 2: %', version2;
216 -- RAISE NOTICE 'Split 1: %', split1;
217 -- RAISE NOTICE 'Split 2: %', split2;
219 IF split1[1] > split2[1] THEN
221 ELSIF split1[1] < split2[1] THEN
224 result := debversion_compare_single(split1[2], split2[2]);
226 result := debversion_compare_single(split1[3], split2[3]);
234 IMMUTABLE STRICT;""")
235 c.execute("""COMMENT ON FUNCTION debversion_compare (debversion, debversion)
236 IS 'Compare Debian versions';""")
238 c.execute("""CREATE OR REPLACE FUNCTION debversion_eq (version1 debversion, version2 debversion)
239 RETURNS boolean AS $$
244 comp := debversion_compare(version1, version2);
250 IMMUTABLE STRICT;""")
251 c.execute("""COMMENT ON FUNCTION debversion_eq (debversion, debversion)
252 IS 'debversion equal';""")
254 c.execute("""CREATE OR REPLACE FUNCTION debversion_ne (version1 debversion, version2 debversion)
255 RETURNS boolean AS $$
260 comp := debversion_compare(version1, version2);
266 IMMUTABLE STRICT;""")
267 c.execute("""COMMENT ON FUNCTION debversion_ne (debversion, debversion)
268 IS 'debversion not equal';""")
270 c.execute("""CREATE OR REPLACE FUNCTION debversion_lt (version1 debversion, version2 debversion)
271 RETURNS boolean AS $$
276 comp := debversion_compare(version1, version2);
282 IMMUTABLE STRICT;""")
283 c.execute("""COMMENT ON FUNCTION debversion_lt (debversion, debversion)
284 IS 'debversion less-than';""")
286 c.execute("""CREATE OR REPLACE FUNCTION debversion_gt (version1 debversion, version2 debversion) RETURNS boolean AS $$
291 comp := debversion_compare(version1, version2);
297 IMMUTABLE STRICT;""")
298 c.execute("""COMMENT ON FUNCTION debversion_gt (debversion, debversion)
299 IS 'debversion greater-than';""")
301 c.execute("""CREATE OR REPLACE FUNCTION debversion_le (version1 debversion, version2 debversion)
302 RETURNS boolean AS $$
307 comp := debversion_compare(version1, version2);
313 IMMUTABLE STRICT;""")
314 c.execute("""COMMENT ON FUNCTION debversion_le (debversion, debversion)
315 IS 'debversion less-than-or-equal';""")
317 c.execute("""CREATE OR REPLACE FUNCTION debversion_ge (version1 debversion, version2 debversion)
318 RETURNS boolean AS $$
323 comp := debversion_compare(version1, version2);
329 IMMUTABLE STRICT;""")
330 c.execute("""COMMENT ON FUNCTION debversion_ge (debversion, debversion)
331 IS 'debversion greater-than-or-equal';""")
333 c.execute("""CREATE OPERATOR = (
334 PROCEDURE = debversion_eq,
335 LEFTARG = debversion,
336 RIGHTARG = debversion,
339 c.execute("""COMMENT ON OPERATOR = (debversion, debversion)
340 IS 'debversion equal';""")
342 c.execute("""CREATE OPERATOR != (
343 PROCEDURE = debversion_eq,
344 LEFTARG = debversion,
345 RIGHTARG = debversion,
348 c.execute("""COMMENT ON OPERATOR != (debversion, debversion)
349 IS 'debversion not equal';""")
351 c.execute("""CREATE OPERATOR < (
352 PROCEDURE = debversion_lt,
353 LEFTARG = debversion,
354 RIGHTARG = debversion,
357 c.execute("""COMMENT ON OPERATOR < (debversion, debversion)
358 IS 'debversion less-than';""")
360 c.execute("""CREATE OPERATOR > (
361 PROCEDURE = debversion_gt,
362 LEFTARG = debversion,
363 RIGHTARG = debversion,
366 c.execute("""COMMENT ON OPERATOR > (debversion, debversion)
367 IS 'debversion greater-than';""")
369 c.execute("""CREATE OPERATOR <= (
370 PROCEDURE = debversion_le,
371 LEFTARG = debversion,
372 RIGHTARG = debversion,
375 c.execute("""COMMENT ON OPERATOR <= (debversion, debversion)
376 IS 'debversion less-than-or-equal';""")
378 c.execute("""CREATE OPERATOR >= (
379 PROCEDURE = debversion_ge,
380 LEFTARG = debversion,
381 RIGHTARG = debversion,
384 c.execute("""COMMENT ON OPERATOR >= (debversion, debversion)
385 IS 'debversion greater-than-or-equal';""")
387 c.execute("ALTER TABLE source ALTER COLUMN version TYPE debversion;")
388 c.execute("ALTER TABLE binaries ALTER COLUMN version TYPE debversion;")
390 c.execute("UPDATE config SET value = '2' WHERE name = 'db_revision'")
394 except psycopg2.ProgrammingError, msg:
396 raise DBUpdateError, "Unable to appy debversion updates, rollback issued. Error message : %s" % (str(msg))