4 # Debian Archive Kit Database Update Script
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 ################################################################################
29 print "Note: to be able to enable the the PL/Perl (plperl) procedural language, we do"
30 print "need postgresql-plperl-$postgres-version installed. Make sure that this is the"
31 print "case before you continue. Interrupt if it isn't, sleeping 5 seconds now."
32 print "(We need to be database superuser for this to work!)"
38 print "Enabling PL/Perl language"
39 c.execute("CREATE LANGUAGE plperl;")
41 print "Adding debversion type to database."
43 # Not present in all databases, maybe PL/Perl version-dependent?
44 # c.execute("SET SESSION plperl.use_strict TO 't';")
46 c.execute("CREATE DOMAIN debversion AS TEXT;")
47 c.execute("COMMENT ON DOMAIN debversion IS 'Debian package version number';")
49 c.execute("""ALTER DOMAIN debversion
50 ADD CONSTRAINT debversion_syntax
51 CHECK (VALUE !~ '[^-+:.0-9a-zA-Z~]');""")
53 # From Dpkg::Version::parseversion
54 c.execute("""CREATE OR REPLACE FUNCTION debversion_split (debversion)
60 $ver =~ /^(\d+):(.+)/ or die "bad version number '$ver'";
68 if ($ver =~ /(.+)-(.*)$/)
70 $verhash{version} = $1;
71 $verhash{revision} = $2;
75 $verhash{version} = $ver;
76 $verhash{revision} = 0;
79 return [$verhash{'epoch'}, $verhash{'version'}, $verhash{'revision'}];
83 c.execute("""COMMENT ON FUNCTION debversion_split (debversion)
84 IS 'Split debian version into epoch, upstream version and revision';""")
86 c.execute("""CREATE OR REPLACE FUNCTION debversion_epoch (version debversion)
91 split := debversion_split(version);
97 COMMENT ON FUNCTION debversion_epoch (debversion)
98 IS 'Get debian version epoch';
100 CREATE OR REPLACE FUNCTION debversion_version (version debversion)
105 split := debversion_split(version);
110 IMMUTABLE STRICT;""")
111 c.execute("""COMMENT ON FUNCTION debversion_version (debversion)
112 IS 'Get debian version upstream version';""")
114 c.execute("""CREATE OR REPLACE FUNCTION debversion_revision (version debversion)
119 split := debversion_split(version);
124 IMMUTABLE STRICT;""")
125 c.execute("""COMMENT ON FUNCTION debversion_revision (debversion)
126 IS 'Get debian version revision';""")
128 # From Dpkg::Version::parseversion
129 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare_single (version1 text, version2 text)
130 RETURNS integer AS $$
133 ##define order(x) ((x) == '~' ? -1 \
134 # : cisdigit((x)) ? 0 \
136 # : cisalpha((x)) ? (x) \
138 # This comparison is out of dpkg's order to avoid
139 # comparing things to undef and triggering warnings.
140 if (not defined $x or not length $x) {
146 elsif ($x =~ /^\d$/) {
149 elsif ($x =~ /^[A-Z]$/i) {
153 return ord($x) + 256;
159 return @{$a} ? shift @{$a} : undef;
161 my ($val, $ref) = @_;
162 $val = "" if not defined $val;
163 $ref = "" if not defined $ref;
164 my @val = split //,$val;
165 my @ref = split //,$ref;
166 my $vc = next_elem @val;
167 my $rc = next_elem @ref;
168 while (defined $vc or defined $rc) {
170 while ((defined $vc and $vc !~ /^\d$/) or
171 (defined $rc and $rc !~ /^\d$/)) {
172 my $vo = order($vc); my $ro = order($rc);
173 # Unlike dpkg's verrevcmp, we only return 1 or -1 here.
174 return (($vo - $ro > 0) ? 1 : -1) if $vo != $ro;
175 $vc = next_elem @val; $rc = next_elem @ref;
177 while (defined $vc and $vc eq '0') {
178 $vc = next_elem @val;
180 while (defined $rc and $rc eq '0') {
181 $rc = next_elem @ref;
183 while (defined $vc and $vc =~ /^\d$/ and
184 defined $rc and $rc =~ /^\d$/) {
185 $first_diff = ord($vc) - ord($rc) if !$first_diff;
186 $vc = next_elem @val; $rc = next_elem @ref;
188 return 1 if defined $vc and $vc =~ /^\d$/;
189 return -1 if defined $rc and $rc =~ /^\d$/;
190 return (($first_diff > 0) ? 1 : -1) if $first_diff;
195 IMMUTABLE STRICT;""")
196 c.execute("""COMMENT ON FUNCTION debversion_compare_single (text, text)
197 IS 'Compare upstream or revision parts of Debian versions';""")
199 # Logic only derived from Dpkg::Version::parseversion
200 c.execute("""CREATE OR REPLACE FUNCTION debversion_compare (version1 debversion, version2 debversion)
201 RETURNS integer AS $$
208 split1 := debversion_split(version1);
209 split2 := debversion_split(version2);
211 -- RAISE NOTICE 'Version 1: %', version1;
212 -- RAISE NOTICE 'Version 2: %', version2;
213 -- RAISE NOTICE 'Split 1: %', split1;
214 -- RAISE NOTICE 'Split 2: %', split2;
216 IF split1[1] > split2[1] THEN
218 ELSIF split1[1] < split2[1] THEN
221 result := debversion_compare_single(split1[2], split2[2]);
223 result := debversion_compare_single(split1[3], split2[3]);
231 IMMUTABLE STRICT;""")
232 c.execute("""COMMENT ON FUNCTION debversion_compare (debversion, debversion)
233 IS 'Compare Debian versions';""")
235 c.execute("""CREATE OR REPLACE FUNCTION debversion_eq (version1 debversion, version2 debversion)
236 RETURNS boolean AS $$
241 comp := debversion_compare(version1, version2);
247 IMMUTABLE STRICT;""")
248 c.execute("""COMMENT ON FUNCTION debversion_eq (debversion, debversion)
249 IS 'debversion equal';""")
251 c.execute("""CREATE OR REPLACE FUNCTION debversion_ne (version1 debversion, version2 debversion)
252 RETURNS boolean AS $$
257 comp := debversion_compare(version1, version2);
263 IMMUTABLE STRICT;""")
264 c.execute("""COMMENT ON FUNCTION debversion_ne (debversion, debversion)
265 IS 'debversion not equal';""")
267 c.execute("""CREATE OR REPLACE FUNCTION debversion_lt (version1 debversion, version2 debversion)
268 RETURNS boolean AS $$
273 comp := debversion_compare(version1, version2);
279 IMMUTABLE STRICT;""")
280 c.execute("""COMMENT ON FUNCTION debversion_lt (debversion, debversion)
281 IS 'debversion less-than';""")
283 c.execute("""CREATE OR REPLACE FUNCTION debversion_gt (version1 debversion, version2 debversion) RETURNS boolean AS $$
288 comp := debversion_compare(version1, version2);
294 IMMUTABLE STRICT;""")
295 c.execute("""COMMENT ON FUNCTION debversion_gt (debversion, debversion)
296 IS 'debversion greater-than';""")
298 c.execute("""CREATE OR REPLACE FUNCTION debversion_le (version1 debversion, version2 debversion)
299 RETURNS boolean AS $$
304 comp := debversion_compare(version1, version2);
310 IMMUTABLE STRICT;""")
311 c.execute("""COMMENT ON FUNCTION debversion_le (debversion, debversion)
312 IS 'debversion less-than-or-equal';""")
314 c.execute("""CREATE OR REPLACE FUNCTION debversion_ge (version1 debversion, version2 debversion)
315 RETURNS boolean AS $$
320 comp := debversion_compare(version1, version2);
326 IMMUTABLE STRICT;""")
327 c.execute("""COMMENT ON FUNCTION debversion_ge (debversion, debversion)
328 IS 'debversion greater-than-or-equal';""")
330 c.execute("""CREATE OPERATOR = (
331 PROCEDURE = debversion_eq,
332 LEFTARG = debversion,
333 RIGHTARG = debversion,
336 c.execute("""COMMENT ON OPERATOR = (debversion, debversion)
337 IS 'debversion equal';""")
339 c.execute("""CREATE OPERATOR != (
340 PROCEDURE = debversion_eq,
341 LEFTARG = debversion,
342 RIGHTARG = debversion,
345 c.execute("""COMMENT ON OPERATOR != (debversion, debversion)
346 IS 'debversion not equal';""")
348 c.execute("""CREATE OPERATOR < (
349 PROCEDURE = debversion_lt,
350 LEFTARG = debversion,
351 RIGHTARG = debversion,
354 c.execute("""COMMENT ON OPERATOR < (debversion, debversion)
355 IS 'debversion less-than';""")
357 c.execute("""CREATE OPERATOR > (
358 PROCEDURE = debversion_gt,
359 LEFTARG = debversion,
360 RIGHTARG = debversion,
363 c.execute("""COMMENT ON OPERATOR > (debversion, debversion)
364 IS 'debversion greater-than';""")
366 c.execute("""CREATE OPERATOR <= (
367 PROCEDURE = debversion_le,
368 LEFTARG = debversion,
369 RIGHTARG = debversion,
372 c.execute("""COMMENT ON OPERATOR <= (debversion, debversion)
373 IS 'debversion less-than-or-equal';""")
375 c.execute("""CREATE OPERATOR >= (
376 PROCEDURE = debversion_ge,
377 LEFTARG = debversion,
378 RIGHTARG = debversion,
381 c.execute("""COMMENT ON OPERATOR >= (debversion, debversion)
382 IS 'debversion greater-than-or-equal';""")
384 c.execute("ALTER TABLE source ALTER COLUMN version TYPE debversion;")
385 c.execute("ALTER TABLE binaries ALTER COLUMN version TYPE debversion;")
387 c.execute("UPDATE config SET value = '2' WHERE name = 'db_revision'")
391 except psycopg2.ProgrammingError, msg:
393 print "FATAL: Unable to apply debversion table update 2!"
394 print "Error Message: " + str(msg)
395 print "Database changes have been rolled back."