]> git.donarmstrong.com Git - debbugs.git/blob - sql/debbugs_schema.sql
we don't necessarily need to deparse the email address for pkg links
[debbugs.git] / sql / debbugs_schema.sql
1 -- -*- mode: sql; sql-product: postgres; -*-
2 DROP TABLE bug_status_cache CASCADE;
3 DROP VIEW bug_package CASCADE;
4 DROP VIEW binary_versions CASCADE;
5 DROP VIEW bug_status CASCADE;
6 DROP TABLE bug_tag CASCADE;
7 DROP TABLE tag CASCADE;
8 DROP TABLE bug_user_tag CASCADE;
9 DROP TABLE user_tag CASCADE;
10 DROP TABLE severity CASCADE;
11 DROP TABLE bug CASCADE;
12 DROP TABLE src_pkg CASCADE;
13 DROP TABLE bug_ver CASCADE;
14 DROP TABLE src_ver CASCADE;
15 DROP TABLE arch CASCADE;
16 DROP TABLE bin_ver CASCADE;
17 DROP TABLE bin_pkg CASCADE;
18 DROP TABLE bug_blocks CASCADE;
19 DROP TABLE bug_merged CASCADE;
20 DROP TABLE bug_srcpackage CASCADE;
21 DROP TABLE bug_binpackage CASCADE;
22 DROP TABLE bug_affects_binpackage CASCADE;
23 DROP TABLE bug_affects_srcpackage CASCADE;
24 DROP TABLE suite CASCADE;
25 DROP TABLE bin_associations CASCADE;
26 DROP TABLE src_associations CASCADE;
27 DROP TABLE maintainer CASCADE;
28 DROP TABLE bug_message CASCADE;
29 DROP TABLE message_correspondent CASCADE;
30 DROP TABLE correspondent_full_name CASCADE;
31 DROP TABLE correspondent CASCADE;
32 DROP TABLE message_refs CASCADE;
33 DROP TABLE message CASCADE;
34 DROP TYPE message_correspondent_type CASCADE;
35 DROP TABLE table_comments CASCADE;
36 DROP TABLE column_comments CASCADE;
37 DROP TYPE bug_status_type CASCADE;
38
39 -- the following two tables are used to provide documentation about
40 -- the tables and columns for DBIx::Class::Schema::Loader
41 CREATE TABLE table_comments (
42        table_name TEXT NOT NULL,
43        comment_text TEXT NOT NULL
44 );
45 CREATE UNIQUE INDEX table_comments_table_name_idx ON table_comments(table_name);
46 CREATE TABLE column_comments (
47        table_name TEXT  NOT NULL,
48        column_name TEXT  NOT NULL,
49        comment_text TEXT NOT NULL
50 );
51 CREATE UNIQUE INDEX column_comments_table_name_column_name_idx ON column_comments(table_name,column_name);
52
53
54 CREATE TABLE correspondent (
55        id SERIAL PRIMARY KEY,
56        addr TEXT NOT NULL
57 );
58 CREATE UNIQUE INDEX correspondent_addr_idx ON correspondent(addr);
59 INSERT INTO table_comments VALUES ('correspondent','Individual who has corresponded with the BTS');
60 INSERT INTO column_comments VALUES ('correspondent','id','Correspondent ID');
61 INSERT INTO column_comments VALUES ('correspondent','addr','Correspondent address');
62
63 CREATE TABLE maintainer (
64        id SERIAL PRIMARY KEY,
65        name TEXT NOT NULL,
66        correspondent INT NOT NULL REFERENCES correspondent(id),
67        created TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
68        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
69 );
70 CREATE UNIQUE INDEX maintainer_name_idx ON maintainer(name);
71 CREATE INDEX maintainer_idx_correspondent ON maintainer(correspondent);
72 INSERT INTO table_comments  VALUES ('maintainer','Package maintainer names');
73 INSERT INTO column_comments VALUES ('maintainer','id','Package maintainer id');
74 INSERT INTO column_comments VALUES ('maintainer','name','Name of package maintainer');
75 INSERT INTO column_comments VALUES ('maintainer','correspondent','Correspondent ID');
76 INSERT INTO column_comments VALUES ('maintainer','created','Time maintainer record created');
77 INSERT INTO column_comments VALUES ('maintainer','modified','Time maintainer record modified');
78
79
80 CREATE TABLE severity (
81        id SERIAL PRIMARY KEY,
82        severity TEXT NOT NULL,
83        ordering INT NOT NULL DEFAULT 5,
84        strong BOOLEAN DEFAULT FALSE,
85        obsolete BOOLEAN DEFAULT FALSE
86 );
87 CREATE UNIQUE INDEX severity_severity_idx ON severity(severity);
88 CREATE INDEX severity_ordering_idx ON severity(ordering);
89 INSERT INTO table_comments VALUES ('severity','Bug severity');
90 INSERT INTO column_comments VALUES ('severity','id','Severity id');
91 INSERT INTO column_comments VALUES ('severity','severity','Severity name');
92 INSERT INTO column_comments VALUES ('severity','ordering','Severity ordering (more severe severities have higher numbers)');
93 INSERT INTO column_comments VALUES ('severity','strong','True if severity is a strong severity');
94 INSERT INTO column_comments VALUES ('severity','obsolete','Whether a severity level is obsolete (should not be set on new bugs)');
95
96 -- bugs table
97 CREATE TABLE bug (
98        id INTEGER NOT NULL PRIMARY KEY,
99        creation TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
100        log_modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
101        last_modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
102        archived BOOLEAN NOT NULL DEFAULT FALSE,
103        unarchived TIMESTAMP WITH TIME ZONE,
104        forwarded TEXT NOT NULL DEFAULT '',
105        summary TEXT NOT NULL DEFAULT '',
106        outlook TEXT NOT NULL DEFAULT '',
107        subject TEXT NOT NULL,
108        severity INT NOT NULL REFERENCES severity(id),
109        done INT REFERENCES correspondent(id),
110        done_full TEXT NOT NULL DEFAULT '',
111        owner INT REFERENCES correspondent(id),
112        owner_full TEXT NOT NULL DEFAULT '',
113        -- submitter would ideally be NOT NULL, but there are some ancient bugs which do not have submitters
114        submitter INT REFERENCES correspondent(id),
115        submitter_full TEXT NOT NULL DEFAULT '',
116        unknown_packages TEXT NOT NULL DEFAULT '',
117        unknown_affects TEXT NOT NULL DEFAULT ''
118 );
119 CREATE INDEX bug_idx_owner ON bug(owner);
120 CREATE INDEX bug_idx_submitter ON bug(submitter);
121 CREATE INDEX bug_idx_done ON bug(done);
122 CREATE INDEX bug_idx_forwarded ON bug(forwarded);
123 CREATE INDEX bug_idx_last_modified ON bug(last_modified);
124 CREATE INDEX bug_idx_severity ON bug(severity);
125 CREATE INDEX bug_idx_creation ON bug(creation);
126 CREATE INDEX bug_idx_log_modified ON bug(log_modified);
127
128 INSERT INTO table_comments VALUES ('bug','Bugs');
129 INSERT INTO column_comments VALUES ('bug','id','Bug number');
130 INSERT INTO column_comments VALUES ('bug','creation','Time bug created');
131 INSERT INTO column_comments VALUES ('bug','log_modified','Time bug log was last modified');
132 INSERT INTO column_comments VALUES ('bug','last_modified','Time bug status was last modified');
133 INSERT INTO column_comments VALUES ('bug','archived','True if bug has been archived');
134 INSERT INTO column_comments VALUES ('bug','unarchived','Time bug was last unarchived; null if bug has never been unarchived');
135 INSERT INTO column_comments VALUES ('bug','forwarded','Where bug has been forwarded to; empty if it has not been forwarded');
136 INSERT INTO column_comments VALUES ('bug','summary','Summary of the bug; empty if it has no summary');
137 INSERT INTO column_comments VALUES ('bug','outlook','Outlook of the bug; empty if it has no outlook');
138 INSERT INTO column_comments VALUES ('bug','subject','Subject of the bug');
139 INSERT INTO column_comments VALUES ('bug','done','Individual who did the -done; empty if it has never been -done');
140 INSERT INTO column_comments VALUES ('bug','owner','Individual who owns this bug; empty if no one owns it');
141 INSERT INTO column_comments VALUES ('bug','submitter','Individual who submitted this bug; empty if there is no submitter');
142 INSERT INTO column_comments VALUES ('bug','unknown_packages','Package name if the package is not known');
143 INSERT INTO column_comments VALUES ('bug','unknown_affects','Package name if the affected package is not known');
144
145
146
147 CREATE TABLE bug_blocks (
148        id SERIAL PRIMARY KEY,
149        bug INT NOT NULL REFERENCES bug,
150        blocks INT NOT NULL REFERENCES bug,
151        CONSTRAINT bug_doesnt_block_itself CHECK (bug <> blocks)
152 );
153 CREATE UNIQUE INDEX bug_blocks_bug_id_blocks_idx ON bug_blocks(bug,blocks);
154 CREATE INDEX bug_blocks_bug_id_idx ON bug_blocks(bug);
155 CREATE INDEX bug_blocks_blocks_idx ON bug_blocks(blocks);
156 INSERT INTO table_comments VALUES ('bug_blocks','Bugs which block other bugs');
157 INSERT INTO column_comments VALUES ('bug_blocks','bug','Bug number');
158 INSERT INTO column_comments VALUES ('bug_blocks','blocks','Bug number which is blocked by bug');
159
160
161 CREATE TABLE bug_merged (
162        id SERIAL PRIMARY KEY,
163        bug INT NOT NULL REFERENCES bug,
164        merged INT NOT NULL REFERENCES bug,
165        CONSTRAINT bug_doesnt_merged_itself CHECK (bug <> merged)
166 );
167 CREATE UNIQUE INDEX bug_merged_bug_id_merged_idx ON bug_merged(bug,merged);
168 CREATE INDEX bug_merged_bug_id_idx ON bug_merged(bug);
169 CREATE INDEX bug_merged_merged_idx ON bug_merged(merged);
170 INSERT INTO table_comments  VALUES ('bug_merged','Bugs which are merged with other bugs');
171 INSERT INTO column_comments VALUES ('bug_merged','bug','Bug number');
172 INSERT INTO column_comments VALUES ('bug_merged','merged','Bug number which is merged with bug');
173
174 CREATE TABLE src_pkg (
175        id SERIAL PRIMARY KEY,
176        pkg TEXT NOT NULL,
177        pseduopkg BOOLEAN NOT NULL DEFAULT FALSE,
178        alias_of INT REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE,
179        creation TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
180        disabled TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT 'infinity'::timestamp with time zone,
181        last_modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
182        obsolete BOOLEAN NOT NULL DEFAULT FALSE,
183        CONSTRAINT src_pkg_doesnt_alias_itself CHECK (id <> alias_of),
184        CONSTRAINT src_pkg_is_obsolete_if_disabled CHECK (
185            (obsolete IS FALSE AND disabled='infinity'::timestamp with time zone) OR
186            (obsolete IS TRUE AND disabled < 'infinity'::timestamp with time zone))
187 );
188 CREATE INDEX src_pkg_pkg ON src_pkg(pkg);
189 CREATE UNIQUE INDEX src_pkg_pkg_null ON src_pkg(pkg) WHERE disabled='infinity'::timestamp with time zone;
190 CREATE UNIQUE INDEX src_pkg_pkg_disabled ON src_pkg(pkg,disabled);
191 INSERT INTO table_comments VALUES ('src_pkg','Source packages');
192 INSERT INTO column_comments VALUES ('src_pkg','id','Source package id');
193 INSERT INTO column_comments VALUES ('src_pkg','pkg','Source package name');
194 INSERT INTO column_comments VALUES ('src_pkg','pseudopkg','True if this is a pseudo package');
195 INSERT INTO column_comments VALUES ('src_pkg','alias_of','Source package id which this source package is an alias of');
196
197
198
199 CREATE TABLE src_ver (
200        id SERIAL PRIMARY KEY,
201        src_pkg INT NOT NULL REFERENCES src_pkg
202             ON UPDATE CASCADE ON DELETE CASCADE,
203        ver debversion NOT NULL,
204        maintainer INT REFERENCES maintainer
205             ON UPDATE CASCADE ON DELETE SET NULL,
206        upload_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
207        based_on INT REFERENCES src_ver
208             ON UPDATE CASCADE ON DELETE CASCADE
209 );
210 CREATE UNIQUE INDEX src_ver_src_pkg_id_ver ON src_ver(src_pkg,ver);
211 INSERT INTO table_comments VALUES ('src_ver','Source Package versions');
212 INSERT INTO column_comments VALUES ('src_ver','id','Source package version id');
213 INSERT INTO column_comments VALUES ('src_ver','src_pkg','Source package id (matches src_pkg table)');
214 INSERT INTO column_comments VALUES ('src_ver','ver','Version of the source package');
215 INSERT INTO column_comments VALUES ('src_ver','maintainer','Maintainer id (matches maintainer table)');
216 INSERT INTO column_comments VALUES ('src_ver','upload_date','Date this version of the source package was uploaded');
217 INSERT INTO column_comments VALUES ('src_ver','based_on','Source package version this version is based on');
218
219
220
221 CREATE TABLE bug_ver (
222        id SERIAL PRIMARY KEY,
223        bug INT NOT NULL REFERENCES bug
224          ON UPDATE CASCADE ON DELETE RESTRICT,
225        ver_string TEXT,
226        src_pkg INT REFERENCES src_pkg
227             ON UPDATE CASCADE ON DELETE SET NULL,
228        src_ver INT REFERENCES src_ver
229             ON UPDATE CASCADE ON DELETE SET NULL,
230        found BOOLEAN NOT NULL DEFAULT TRUE,
231        creation TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
232        last_modified TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
233 );
234 CREATE INDEX bug_ver_src_pkg_id_idx ON bug_ver(src_pkg);
235 CREATE INDEX bug_ver_src_pkg_id_src_ver_id_idx ON bug_ver(src_pkg,src_ver);
236 CREATE INDEX bug_ver_src_ver_id_idx ON bug_ver(src_ver);
237 CREATE UNIQUE INDEX bug_ver_bug_ver_string_found_idx ON bug_ver(bug,ver_string,found);
238 INSERT INTO table_comments VALUES ('bug_ver','Bug versions');
239 INSERT INTO column_comments VALUES ('bug_ver','id','Bug version id');
240 INSERT INTO column_comments VALUES ('bug_ver','bug','Bug number');
241 INSERT INTO column_comments VALUES ('bug_ver','ver_string','Version string');
242 INSERT INTO column_comments VALUES ('bug_ver','src_pkg','Source package id (matches src_pkg table)');
243 INSERT INTO column_comments VALUES ('bug_ver','src_ver','Source package version id (matches src_ver table)');
244 INSERT INTO column_comments VALUES ('bug_ver','found','True if this is a found version; false if this is a fixed version');
245 INSERT INTO column_comments VALUES ('bug_ver','creation','Time that this entry was created');
246 INSERT INTO column_comments VALUES ('bug_ver','last_modified','Time that this entry was modified');
247
248
249 CREATE TABLE arch (
250        id SERIAL PRIMARY KEY,
251        arch TEXT NOT NULL
252 );
253 CREATE UNIQUE INDEX arch_arch_key ON arch(arch);
254 INSERT INTO table_comments VALUES ('arch','Architectures');
255 INSERT INTO column_comments VALUES ('arch','id','Architecture id');
256 INSERT INTO column_comments VALUES ('arch','arch','Architecture name');
257
258
259 CREATE TABLE bin_pkg (
260        id SERIAL PRIMARY KEY,
261        pkg TEXT NOT NULL
262 );
263 CREATE UNIQUE INDEX bin_pkg_pkg_key ON bin_pkg(pkg);
264 INSERT INTO table_comments VALUES ('bin_pkg','Binary packages');
265 INSERT INTO column_comments VALUES ('bin_pkg','id','Binary package id');
266 INSERT INTO column_comments VALUES ('bin_pkg','pkg','Binary package name');
267
268
269 CREATE TABLE bin_ver(
270        id SERIAL PRIMARY KEY,
271        bin_pkg INT NOT NULL REFERENCES bin_pkg
272             ON UPDATE CASCADE ON DELETE CASCADE,
273        src_ver INT NOT NULL REFERENCES src_ver
274             ON UPDATE CASCADE ON DELETE CASCADE,
275        arch INT NOT NULL REFERENCES arch
276             ON UPDATE CASCADE ON DELETE CASCADE,
277        ver debversion NOT NULL
278 );
279 CREATE INDEX bin_ver_ver_idx ON bin_ver(ver);
280 CREATE UNIQUE INDEX bin_ver_bin_pkg_id_arch_idx ON bin_ver(bin_pkg,arch,ver);
281 CREATE INDEX bin_ver_src_ver_id_arch_idx ON bin_ver(src_ver,arch);
282 CREATE INDEX bin_ver_bin_pkg_id_idx ON bin_ver(bin_pkg);
283 CREATE INDEX bin_ver_src_ver_id_idx ON bin_ver(src_ver);
284 INSERT INTO table_comments VALUES ('bin_ver','Binary versions');
285 INSERT INTO column_comments VALUES ('bin_ver','id','Binary version id');
286 INSERT INTO column_comments VALUES ('bin_ver','bin_pkg','Binary package id (matches bin_pkg)');
287 INSERT INTO column_comments VALUES ('bin_ver','src_ver','Source version (matchines src_ver)');
288 INSERT INTO column_comments VALUES ('bin_ver','arch','Architecture id (matches arch)');
289 INSERT INTO column_comments VALUES ('bin_ver','ver','Binary version');
290
291 CREATE TABLE bin_pkg_src_pkg (
292        bin_pkg INT NOT NULL REFERENCES bin_pkg ON UPDATE CASCADE ON DELETE CASCADE,
293        src_pkg INT NOT NULL REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE
294 );
295 CREATE UNIQUE INDEX bin_pkg_src_pkg_bin_pkg_src_pkg ON bin_pkg_src_pkg (bin_pkg,src_pkg);
296 CREATE UNIQUE INDEX bin_pkg_src_pkg_src_pkg_bin_pkg ON bin_pkg_src_pkg (src_pkg,bin_pkg);
297
298
299 INSERT INTO table_comments VALUES ('bin_pkg_src_pkg',
300        'Binary package <-> source package mapping sumpmary table');
301 INSERT INTO column_comments VALUES ('bin_pkg_src_pkg','bin_pkg','Binary package id (matches bin_pkg)');
302 INSERT INTO column_comments VALUES ('bin_pkg_src_pkg','src_pkg','Source package id (matches src_pkg)');
303
304 CREATE OR REPLACE FUNCTION bin_ver_to_src_pkg(bin_ver INT) RETURNS INT
305   AS $src_pkg_from_bin_ver$
306   DECLARE
307   src_pkg int;
308   BEGIN
309         SELECT sv.src_pkg INTO STRICT src_pkg
310                FROM bin_ver bv JOIN src_ver sv ON bv.src_ver=sv.id
311                WHERE bv.id=bin_ver;
312         RETURN src_pkg;
313   END
314   $src_pkg_from_bin_ver$ LANGUAGE plpgsql;
315
316 CREATE OR REPLACE FUNCTION src_ver_to_src_pkg(src_ver INT) RETURNS INT
317   AS $src_ver_to_src_pkg$
318   DECLARE
319   src_pkg int;
320   BEGIN
321         SELECT sv.src_pkg INTO STRICT src_pkg
322                FROM src_ver sv WHERE sv.id=src_ver;
323         RETURN src_pkg;
324   END
325   $src_ver_to_src_pkg$ LANGUAGE plpgsql;
326
327 CREATE OR REPLACE FUNCTION update_bin_pkg_src_pkg_bin_ver () RETURNS TRIGGER
328   AS $update_bin_pkg_src_pkg_bin_ver$
329   DECLARE
330   src_ver_rows integer;
331   BEGIN
332   IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' )  THEN
333      -- if there is still a bin_ver with this src_pkg, then do nothing
334      PERFORM * FROM bin_ver bv JOIN src_ver sv ON bv.src_ver = sv.id
335             WHERE sv.id = OLD.src_ver LIMIT 2;
336      GET DIAGNOSTICS src_ver_rows = ROW_COUNT;
337      IF (src_ver_rows <= 1) THEN
338         DELETE FROM bin_pkg_src_pkg
339                WHERE bin_pkg=OLD.bin_pkg AND
340                      src_pkg=src_ver_to_src_pkg(OLD.src_ver);
341      END IF;
342   END IF;
343   IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
344      BEGIN
345      INSERT INTO bin_pkg_src_pkg (bin_pkg,src_pkg)
346         VALUES (NEW.bin_pkg,src_ver_to_src_pkg(NEW.src_ver))
347         ON CONFLICT (bin_pkg,src_pkg) DO NOTHING;
348      END;
349   END IF;
350   RETURN NULL;
351   END
352   $update_bin_pkg_src_pkg_bin_ver$ LANGUAGE plpgsql;
353
354 CREATE TRIGGER bin_ver_update_bin_pkg_src_pkg
355 AFTER INSERT OR UPDATE OR DELETE ON bin_ver
356 FOR EACH ROW EXECUTE PROCEDURE update_bin_pkg_src_pkg_bin_ver();
357
358
359 CREATE TABLE tag (
360        id SERIAL PRIMARY KEY,
361        tag TEXT NOT NULL UNIQUE,
362        obsolete BOOLEAN DEFAULT FALSE
363 );
364 INSERT INTO table_comments VALUES ('tag','Bug tags');
365 INSERT INTO column_comments VALUES ('tag','id','Tag id');
366 INSERT INTO column_comments VALUES ('tag','tag','Tag name');
367 INSERT INTO column_comments VALUES ('tag','obsolete','Whether a tag is obsolete (should not be set on new bugs)');
368
369 CREATE TABLE bug_tag (
370        bug INT NOT NULL REFERENCES bug,
371        tag INT NOT NULL REFERENCES tag
372 );
373 INSERT INTO table_comments VALUES ('bug_tag','Bug <-> tag mapping');
374 INSERT INTO column_comments VALUES ('bug_tag','bug','Bug id (matches bug)');
375 INSERT INTO column_comments VALUES ('bug_tag','tag','Tag id (matches tag)');
376
377 CREATE UNIQUE INDEX bug_tag_bug_tag ON bug_tag (bug,tag);
378 CREATE INDEX bug_tag_tag ON bug_tag (tag);
379
380 CREATE TABLE user_tag (
381        id SERIAL PRIMARY KEY,
382        tag TEXT NOT NULL,
383        correspondent INT NOT NULL REFERENCES correspondent(id)
384 );
385 INSERT INTO table_comments VALUES ('user_tag','User bug tags');
386 INSERT INTO column_comments VALUES ('user_tag','id','User bug tag id');
387 INSERT INTO column_comments VALUES ('user_tag','tag','User bug tag name');
388 INSERT INTO column_comments VALUES ('user_tag','correspondent','User bug tag correspondent');
389
390 CREATE UNIQUE INDEX user_tag_tag_correspondent ON user_tag(tag,correspondent);
391 CREATE INDEX user_tag_correspondent ON user_tag(correspondent);
392
393 CREATE TABLE bug_user_tag (
394        bug INT NOT NULL REFERENCES bug,
395        user_tag INT NOT NULL REFERENCES user_tag
396 );
397 INSERT INTO table_comments VALUES ('bug_user_tag','Bug <-> user tag mapping');
398 INSERT INTO column_comments VALUES ('bug_user_tag','bug','Bug id (matches bug)');
399 INSERT INTO column_comments VALUES ('bug_user_tag','tag','User tag id (matches user_tag)');
400
401 CREATE UNIQUE INDEX bug_user_tag_bug_tag ON bug_user_tag (bug,user_tag);
402 CREATE INDEX bug_user_tag_tag ON bug_user_tag (user_tag);
403
404 CREATE TABLE bug_binpackage (
405        bug INT NOT NULL REFERENCES bug,
406        bin_pkg INT NOT NULL REFERENCES bin_pkg ON UPDATE CASCADE ON DELETE CASCADE
407 );
408 CREATE UNIQUE INDEX bug_binpackage_id_pkg ON bug_binpackage(bug,bin_pkg);
409 INSERT INTO table_comments VALUES ('bug_binpackage','Bug <-> binary package mapping');
410 INSERT INTO column_comments VALUES ('bug_binpackage','bug','Bug id (matches bug)');
411 INSERT INTO column_comments VALUES ('bug_binpackage','bin_pkg','Binary package id (matches bin_pkg)');
412
413 CREATE TABLE bug_srcpackage (
414        bug INT NOT NULL REFERENCES bug,
415        src_pkg INT NOT NULL REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE
416 );
417 CREATE UNIQUE INDEX bug_srcpackage_id_pkg ON bug_srcpackage(bug,src_pkg);
418 CREATE INDEX bug_srcpackage_idx_src_pkg ON bug_srcpackage(src_pkg);
419
420 INSERT INTO table_comments VALUES ('bug_srcpackage','Bug <-> source package mapping');
421 INSERT INTO column_comments VALUES ('bug_srcpackage','bug','Bug id (matches bug)');
422 INSERT INTO column_comments VALUES ('bug_srcpackage','src_pkg','Source package id (matches src_pkg)');
423
424 CREATE TABLE bug_affects_binpackage (
425        bug INT NOT NULL REFERENCES bug,
426        bin_pkg INT NOT NULL REFERENCES bin_pkg ON UPDATE CASCADE ON DELETE CASCADE
427 );
428 CREATE UNIQUE INDEX bug_affects_binpackage_id_pkg ON bug_affects_binpackage(bug,bin_pkg);
429 INSERT INTO table_comments VALUES ('bug_affects_binpackage','Bug <-> binary package mapping');
430 INSERT INTO column_comments VALUES ('bug_affects_binpackage','bug','Bug id (matches bug)');
431 INSERT INTO column_comments VALUES ('bug_affects_binpackage','bin_pkg','Binary package id (matches bin_pkg)');
432
433 CREATE TABLE bug_affects_srcpackage (
434        bug INT NOT NULL REFERENCES bug,
435        src_pkg INT NOT NULL REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE
436 );
437 CREATE UNIQUE INDEX bug_affects_srcpackage_id_pkg ON bug_affects_srcpackage(bug,src_pkg);
438 INSERT INTO table_comments VALUES ('bug_affects_srcpackage','Bug <-> source package mapping');
439 INSERT INTO column_comments VALUES ('bug_affects_srcpackage','bug','Bug id (matches bug)');
440 INSERT INTO column_comments VALUES ('bug_affects_srcpackage','src_pkg','Source package id (matches src_pkg)');
441
442 CREATE VIEW bug_package (bug,pkg_id,pkg_type,package) AS
443        SELECT b.bug,b.bin_pkg,'binary',bp.pkg FROM bug_binpackage b JOIN bin_pkg bp ON bp.id=b.bin_pkg UNION
444               SELECT s.bug,s.src_pkg,'source',sp.pkg FROM bug_srcpackage s JOIN src_pkg sp ON sp.id=s.src_pkg UNION
445        SELECT b.bug,b.bin_pkg,'binary_affects',bp.pkg FROM bug_affects_binpackage b JOIN bin_pkg bp ON bp.id=b.bin_pkg UNION
446               SELECT s.bug,s.src_pkg,'source_affects',sp.pkg FROM bug_affects_srcpackage s JOIN src_pkg sp ON sp.id=s.src_pkg;
447
448 CREATE VIEW binary_versions (src_pkg, src_ver, bin_pkg, arch, bin_ver) AS
449        SELECT sp.pkg AS src_pkg, sv.ver AS src_ver, bp.pkg AS bin_pkg, a.arch AS arch, b.ver AS bin_ver,
450        svb.ver AS src_ver_based_on, spb.pkg AS src_pkg_based_on
451        FROM bin_ver b JOIN arch a ON b.arch = a.id
452                       JOIN bin_pkg bp ON b.bin_pkg  = bp.id
453                       JOIN src_ver sv ON b.src_ver  = sv.id
454                       JOIN src_pkg sp ON sv.src_pkg = sp.id
455                       LEFT OUTER JOIN src_ver svb ON sv.based_on = svb.id
456                       LEFT OUTER JOIN src_pkg spb ON spb.id = svb.src_pkg;
457
458 CREATE TABLE suite (
459        id SERIAL PRIMARY KEY,
460        codename TEXT NOT NULL,
461        suite_name TEXT,
462        version TEXT,
463        active BOOLEAN DEFAULT TRUE);
464 CREATE UNIQUE INDEX suite_idx_codename ON suite(codename);
465 CREATE UNIQUE INDEX suite_suite_name_key ON suite(suite_name);
466 CREATE UNIQUE INDEX suite_idx_version ON suite(version);
467 INSERT INTO table_comments VALUES ('suite','Debian Release Suite (stable, testing, etc.)');
468 INSERT INTO column_comments VALUES ('suite','id','Suite id');
469 INSERT INTO column_comments VALUES ('suite','suite_name','Suite name (testing, stable, etc.)');
470 INSERT INTO column_comments VALUES ('suite','version','Suite version; NULL if there is no appropriate version');
471 INSERT INTO column_comments VALUES ('suite','codename','Suite codename (sid, squeeze, etc.)');
472 INSERT INTO column_comments VALUES ('suite','active','TRUE if the suite is still accepting uploads');
473
474 CREATE TABLE bin_associations (
475        id SERIAL PRIMARY KEY,
476        suite INT NOT NULL REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
477        bin INT NOT NULL REFERENCES bin_ver ON DELETE CASCADE ON UPDATE CASCADE,
478        created TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
479        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
480 );
481 INSERT INTO table_comments VALUES ('bin_associations','Binary <-> suite associations');
482 INSERT INTO column_comments VALUES ('bin_associations','id','Binary <-> suite association id');
483 INSERT INTO column_comments VALUES ('bin_associations','suite','Suite id (matches suite)');
484 INSERT INTO column_comments VALUES ('bin_associations','bin','Binary version id (matches bin_ver)');
485 INSERT INTO column_comments VALUES ('bin_associations','created','Time this binary package entered this suite');
486 INSERT INTO column_comments VALUES ('bin_associations','modified','Time this entry was modified');
487 CREATE UNIQUE INDEX bin_associations_bin_suite ON bin_associations(bin,suite);
488
489 CREATE TABLE src_associations (
490        id SERIAL PRIMARY KEY,
491        suite INT NOT NULL REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
492        source INT NOT NULL REFERENCES src_ver ON DELETE CASCADE ON UPDATE CASCADE,
493        created TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
494        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
495 );
496 INSERT INTO table_comments VALUES ('src_associations','Source <-> suite associations');
497 INSERT INTO column_comments VALUES ('src_associations','id','Source <-> suite association id');
498 INSERT INTO column_comments VALUES ('src_associations','suite','Suite id (matches suite)');
499 INSERT INTO column_comments VALUES ('src_associations','source','Source version id (matches src_ver)');
500 INSERT INTO column_comments VALUES ('src_associations','created','Time this source package entered this suite');
501 INSERT INTO column_comments VALUES ('src_associations','modified','Time this entry was modified');
502 CREATE UNIQUE INDEX src_associations_source_suite ON src_associations(source,suite);
503
504
505 CREATE TYPE bug_status_type AS ENUM ('absent','found','fixed','undef');
506 CREATE TABLE bug_status_cache (
507        bug INT NOT NULL REFERENCES bug ON DELETE CASCADE ON UPDATE CASCADE,
508        suite INT REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
509        arch INT REFERENCES arch ON DELETE CASCADE ON UPDATE CASCADE,
510        status bug_status_type NOT NULL,
511        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
512        asof TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
513 );
514 CREATE UNIQUE INDEX bug_status_cache_bug_suite_arch_idx ON
515        bug_status_cache(bug,suite,arch);
516 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
517        bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
518 CREATE INDEX bug_status_cache_idx_bug ON bug_status_cache(bug);
519 CREATE INDEX bug_status_cache_idx_status ON bug_status_cache(status);
520 CREATE INDEX bug_status_cache_idx_arch ON bug_status_cache(arch);
521 CREATE INDEX bug_status_cache_idx_suite ON bug_status_cache(suite);
522 CREATE INDEX bug_status_cache_idx_asof ON bug_status_cache(asof);
523 INSERT INTO table_comments  VALUES ('bug_status_cache','Bug Status Cache');
524 INSERT INTO column_comments VALUES ('bug_status_cache','bug','Bug number (matches bug)');
525 INSERT INTO column_comments VALUES ('bug_status_cache','suite','Suite id (matches suite)');
526 INSERT INTO column_comments VALUES ('bug_status_cache','arch','Architecture id (matches arch)');
527 INSERT INTO column_comments VALUES ('bug_status_cache','status','Status (bug status)');
528 INSERT INTO column_comments VALUES ('bug_status_cache','modified','Time that this status was last modified');
529 INSERT INTO column_comments VALUES ('bug_status_cache','asof','Time that this status was last calculated');
530
531
532
533 CREATE TABLE message (
534        id SERIAL PRIMARY KEY,
535        msgid TEXT NOT NULL DEFAULT '',
536        from_complete TEXT NOT NULL DEFAULT '',
537        to_complete TEXT NOT NULL DEFAULT '',
538        subject TEXT NOT NULL DEFAULT '',
539        sent_date TIMESTAMP WITH TIME ZONE,
540        refs TEXT NOT NULL DEFAULT '',
541        spam_score FLOAT NOT NULL DEFAULT 0,
542        is_spam BOOLEAN NOT NULL DEFAULT FALSE
543 );
544 INSERT INTO table_comments VALUES ('message','Messages sent to bugs');
545 INSERT INTO column_comments VALUES ('message','id','Message id');
546 INSERT INTO column_comments VALUES ('message','msgid','Message id header');
547 INSERT INTO column_comments VALUES ('message','from_complete','Complete from header of message');
548 INSERT INTO column_comments VALUES ('message','to_complete','Complete to header of message');
549 INSERT INTO column_comments VALUES ('message','subject','Subject of the message');
550 INSERT INTO column_comments VALUES ('message','sent_date','Time/date message was sent (from Date header)');
551 INSERT INTO column_comments VALUES ('message','refs','Contents of References: header');
552 INSERT INTO column_comments VALUES ('message','spam_score','Spam score from spamassassin');
553 INSERT INTO column_comments VALUES ('message','is_spam','True if this message was spam and should not be shown');
554 CREATE INDEX message_msgid_idx ON message(msgid);
555 CREATE UNIQUE INDEX message_msgid_from_complete_to_complete_subject_idx
556     ON message(msgid,from_complete,to_complete,subject);
557 CREATE INDEX message_subject_idx ON message(subject);
558
559 CREATE TABLE message_refs (
560        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
561        refs INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
562        inferred BOOLEAN DEFAULT FALSE,
563        primary_ref BOOLEAN DEFAULT FALSE,
564        CONSTRAINT message_doesnt_reference_itself CHECK (message <> refs)
565 );
566 CREATE UNIQUE INDEX message_refs_message_refs_idx ON message_refs(message,refs);
567 CREATE INDEX message_refs_idx_refs ON message_refs(refs);
568 CREATE INDEX message_refs_idx_message ON message_refs(message);
569 INSERT INTO table_comments VALUES ('message_refs','Message references');
570 INSERT INTO column_comments VALUES ('message_refs','message','Message id (matches message)');
571 INSERT INTO column_comments VALUES ('message_refs','refs','Reference id (matches message)');
572 INSERT INTO column_comments VALUES ('message_refs','inferred','TRUE if this message reference was reconstructed; primarily of use for messages which lack In-Reply-To: or References: headers');
573 INSERT INTO column_comments VALUES ('message_refs','primary_ref','TRUE if this message->ref came from In-Reply-To: or similar.');
574
575
576
577 CREATE TABLE correspondent_full_name(
578        correspondent INT NOT NULL REFERENCES correspondent ON DELETE CASCADE ON UPDATE CASCADE,
579        full_name TEXT NOT NULL,
580        last_seen TIMESTAMP NOT NULL DEFAULT NOW()
581 );
582 CREATE UNIQUE INDEX correspondent_full_name_correspondent_full_name_idx 
583     ON correspondent_full_name(correspondent,full_name);
584 CREATE INDEX correspondent_full_name_idx_full_name ON correspondent_full_name(full_name);
585 CREATE INDEX correspondent_full_name_idx_last_seen ON correspondent_full_name(last_seen);
586 INSERT INTO table_comments VALUES ('correspondent_full_name','Full names of BTS correspondents');
587 INSERT INTO column_comments VALUES ('correspondent_full_name','correspondent','Correspondent ID (matches correspondent)');
588 INSERT INTO column_comments VALUES ('correspondent_full_name','full_name','Correspondent full name (includes e-mail address)');
589
590 CREATE TYPE message_correspondent_type AS ENUM ('to','from','envfrom','cc','recv');
591
592 CREATE TABLE message_correspondent (
593        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
594        correspondent INT NOT NULL REFERENCES correspondent ON DELETE CASCADE ON UPDATE CASCADE,
595        correspondent_type message_correspondent_type NOT NULL DEFAULT 'to'
596 );
597 INSERT INTO table_comments VALUES ('message_correspondent','Linkage between correspondent and message');
598 INSERT INTO column_comments VALUES ('message_correspondent','message','Message id (matches message)');
599 INSERT INTO column_comments VALUES ('message_correspondent','correspondent','Correspondent (matches correspondent)');
600 INSERT INTO column_comments VALUES ('message_correspondent','correspondent_type','Type of correspondent (to, from, envfrom, cc, etc.)');
601
602 CREATE UNIQUE INDEX message_correspondent_message_correspondent_correspondent_t_idx 
603     ON message_correspondent(message,correspondent,correspondent_type);
604 CREATE INDEX message_correspondent_idx_correspondent ON message_correspondent(correspondent);
605 CREATE INDEX message_correspondent_idx_message ON message_correspondent(message);
606
607 CREATE TABLE bug_message (
608        bug INT NOT NULL REFERENCES bug ON DELETE CASCADE ON UPDATE CASCADE,
609        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
610        message_number INT NOT NULL,
611        bug_log_offset INT,
612        offset_valid TIMESTAMP WITH TIME ZONE
613 );
614 CREATE UNIQUE INDEX bug_message_bug_message_idx ON bug_message(bug,message);
615 CREATE INDEX bug_message_idx_bug_message_number ON bug_message(bug,message_number);
616 INSERT INTO table_comments VALUES ('bug_mesage','Mapping between a bug and a message');
617 INSERT INTO column_comments VALUES ('bug_message','bug','Bug id (matches bug)');
618 INSERT INTO column_comments VALUES ('bug_message','message','Message id (matches message)');
619 INSERT INTO column_comments VALUES ('bug_message','message_number','Message number in the bug log');
620 INSERT INTO column_comments VALUES ('bug_message','bug_log_offset','Byte offset in the bug log');
621 INSERT INTO column_comments VALUES ('bug_message','offset_valid','Time offset was valid');
622
623 CREATE VIEW bug_status --(id,bug_num,tags,subject,
624 --        severity,package,originator,log_modified,date,
625 --        last_modified, blocks, blockedby, mergedwith,
626 --        fixed_versions,found_versions)
627        AS
628        SELECT b.id AS id,
629        b.id AS bug_num,
630        string_agg(t.tag,',') AS tags,
631        b.subject AS subject,
632        (SELECT s.severity FROM severity s WHERE s.id=b.severity) AS severity,
633        (SELECT string_agg(package.package,',' ORDER BY package)
634         FROM (SELECT bp.pkg AS package
635                      FROM bug_binpackage bbp
636                      JOIN bin_pkg bp ON bbp.bin_pkg=bp.id
637                      WHERE bbp.bug=b.id
638               UNION
639               SELECT CONCAT('src:',sp.pkg) AS package
640                      FROM bug_srcpackage bsp
641                      JOIN src_pkg sp ON bsp.src_pkg=sp.id
642                      WHERE bsp.bug=b.id) AS package
643         ) AS package,
644        (SELECT string_agg(affects.affects,',' ORDER BY affects)
645         FROM (SELECT bp.pkg AS affects
646                      FROM bug_affects_binpackage bbp
647                      JOIN bin_pkg bp ON bbp.bin_pkg=bp.id
648                      WHERE bbp.bug=b.id
649               UNION
650               SELECT CONCAT('src:',sp.pkg) AS affects
651                      FROM bug_affects_srcpackage bsp
652                      JOIN src_pkg sp ON bsp.src_pkg=sp.id
653                      WHERE bsp.bug=b.id) AS affects
654         ) AS affects,
655         b.submitter_full AS originator,
656         EXTRACT(EPOCH FROM b.log_modified) AS log_modified,
657         EXTRACT(EPOCH FROM b.creation) AS date,
658         EXTRACT(EPOCH FROM b.last_modified) AS last_modified,
659         b.done_full AS done,
660         string_agg(bb.blocks::text,' ' ORDER BY bb.blocks) AS blocks,
661         string_agg(bbb.bug::text,' ' ORDER BY bbb.bug) AS blockedby,
662         (SELECT string_agg(bug.bug::text,' ' ORDER BY bug.bug)
663          FROM (SELECT bm.merged AS bug FROM bug_merged bm WHERE bm.bug=b.id
664                           UNION
665          SELECT bm.bug AS bug FROM bug_merged bm WHERE bm.merged=b.id) AS bug) AS mergedwith,
666         (SELECT string_agg(bv.ver_string,' ') FROM bug_ver bv WHERE bv.bug=b.id AND bv.found IS TRUE)
667                 AS found_versions,
668         (SELECT string_agg(bv.ver_string,' ') FROM bug_ver bv WHERE bv.bug=b.id AND bv.found IS FALSE)
669                 AS fixed_versions
670         FROM bug b
671         LEFT JOIN bug_tag bt ON bt.bug=b.id
672         LEFT JOIN tag t ON bt.tag=t.id
673         LEFT JOIN bug_blocks bb ON bb.bug=b.id
674         LEFT JOIN bug_blocks bbb ON bbb.blocks=b.id
675         GROUP BY b.id;