]> git.decadent.org.uk Git - dak.git/blob - dak/dakdb/update71.py
079c195c254e4f88957557e90126c6ff75dfd84a
[dak.git] / dak / dakdb / update71.py
1 #!/usr/bin/env python
2 # coding=utf8
3
4 """
5 Add missing checksums to source_metadata
6
7 @contact: Debian FTP Master <ftpmaster@debian.org>
8 @copyright: 2011 Ansgar Burchardt <ansgar@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 import psycopg2
29 from daklib.dak_exceptions import DBUpdateError
30 from daklib.config import Config
31
32 ################################################################################
33 def do_update(self):
34     """
35     Add missing checksums to source_metadata
36     """
37     print __doc__
38     try:
39         c = self.db.cursor()
40         c.execute(R"""CREATE OR REPLACE FUNCTION metadata_keys_get(key_ text)
41   RETURNS integer
42   LANGUAGE plpgsql
43   STRICT
44 AS $function$
45 DECLARE
46   v_key_id metadata_keys.key_id%TYPE;
47 BEGIN
48   SELECT key_id INTO v_key_id FROM metadata_keys WHERE key = key_;
49   IF NOT FOUND THEN
50     INSERT INTO metadata_keys (key) VALUES (key_) RETURNING key_id INTO v_key_id;
51   END IF;
52   RETURN v_key_id;
53 END;
54 $function$
55 """)
56
57         c.execute("""COMMENT ON FUNCTION metadata_keys_get(text)
58 IS 'return key_id for the given key. If key is not present, create a new entry.'
59 """)
60
61         c.execute(R"""CREATE OR REPLACE FUNCTION source_metadata_add_missing_checksum(type text)
62   RETURNS INTEGER
63   LANGUAGE plpgsql
64   STRICT
65 AS $function$
66 DECLARE
67   v_checksum_key metadata_keys.key_id%TYPE;
68   rows INTEGER;
69 BEGIN
70   IF type NOT IN ('Files', 'Checksums-Sha1', 'Checksums-Sha256') THEN
71     RAISE EXCEPTION 'Unknown checksum field %', type;
72   END IF;
73   v_checksum_key := metadata_keys_get(type);
74
75   INSERT INTO source_metadata
76     (src_id, key_id, value)
77   SELECT
78     s.id,
79     v_checksum_key,
80     E'\n' ||
81       (SELECT STRING_AGG(' ' || tmp.checksum || ' ' || tmp.size || ' ' || tmp.basename, E'\n' ORDER BY tmp.basename)
82        FROM
83          (SELECT
84               CASE type
85                 WHEN 'Files' THEN f.md5sum
86                 WHEN 'Checksums-Sha1' THEN f.sha1sum
87                 WHEN 'Checksums-Sha256' THEN f.sha256sum
88               END AS checksum,
89               f.size,
90               SUBSTRING(f.filename FROM E'/([^/]*)\\Z') AS basename
91             FROM files f JOIN dsc_files ON f.id = dsc_files.file
92             WHERE dsc_files.source = s.id AND f.id != s.file
93          ) AS tmp
94       )
95
96     FROM
97       source s
98     WHERE NOT EXISTS (SELECT 1 FROM source_metadata md WHERE md.src_id=s.id AND md.key_id = v_checksum_key);
99
100   GET DIAGNOSTICS rows = ROW_COUNT;
101   RETURN rows;
102 END;
103 $function$
104 """)
105
106         c.execute("""COMMENT ON FUNCTION source_metadata_add_missing_checksum(TEXT)
107 IS 'add missing checksum fields to source_metadata. type can be Files (md5sum), Checksums-Sha1, Checksums-Sha256'
108 """)
109
110         c.execute("UPDATE config SET value = '71' WHERE name = 'db_revision'")
111         self.db.commit()
112
113     except psycopg2.ProgrammingError as msg:
114         self.db.rollback()
115         raise DBUpdateError('Unable to apply sick update 71, rollback issued. Error message : %s' % (str(msg)))