]> git.donarmstrong.com Git - debbugs.git/blob - sql/debbugs_schema.sql
Merge branch 'mouseify'
[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 CREATE UNIQUE INDEX bug_binpackage_bin_pkg_bug_idx ON bug_binpackage(bin_pkg,bug);
410 INSERT INTO table_comments VALUES ('bug_binpackage','Bug <-> binary package mapping');
411 INSERT INTO column_comments VALUES ('bug_binpackage','bug','Bug id (matches bug)');
412 INSERT INTO column_comments VALUES ('bug_binpackage','bin_pkg','Binary package id (matches bin_pkg)');
413
414 CREATE TABLE bug_srcpackage (
415        bug INT NOT NULL REFERENCES bug,
416        src_pkg INT NOT NULL REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE
417 );
418 CREATE UNIQUE INDEX bug_srcpackage_id_pkg ON bug_srcpackage(bug,src_pkg);
419 CREATE INDEX bug_srcpackage_idx_src_pkg ON bug_srcpackage(src_pkg);
420
421 INSERT INTO table_comments VALUES ('bug_srcpackage','Bug <-> source package mapping');
422 INSERT INTO column_comments VALUES ('bug_srcpackage','bug','Bug id (matches bug)');
423 INSERT INTO column_comments VALUES ('bug_srcpackage','src_pkg','Source package id (matches src_pkg)');
424
425 CREATE TABLE bug_affects_binpackage (
426        bug INT NOT NULL REFERENCES bug,
427        bin_pkg INT NOT NULL REFERENCES bin_pkg ON UPDATE CASCADE ON DELETE CASCADE
428 );
429 CREATE UNIQUE INDEX bug_affects_binpackage_id_pkg ON bug_affects_binpackage(bug,bin_pkg);
430 INSERT INTO table_comments VALUES ('bug_affects_binpackage','Bug <-> binary package mapping');
431 INSERT INTO column_comments VALUES ('bug_affects_binpackage','bug','Bug id (matches bug)');
432 INSERT INTO column_comments VALUES ('bug_affects_binpackage','bin_pkg','Binary package id (matches bin_pkg)');
433
434 CREATE TABLE bug_affects_srcpackage (
435        bug INT NOT NULL REFERENCES bug,
436        src_pkg INT NOT NULL REFERENCES src_pkg ON UPDATE CASCADE ON DELETE CASCADE
437 );
438 CREATE UNIQUE INDEX bug_affects_srcpackage_id_pkg ON bug_affects_srcpackage(bug,src_pkg);
439 INSERT INTO table_comments VALUES ('bug_affects_srcpackage','Bug <-> source package mapping');
440 INSERT INTO column_comments VALUES ('bug_affects_srcpackage','bug','Bug id (matches bug)');
441 INSERT INTO column_comments VALUES ('bug_affects_srcpackage','src_pkg','Source package id (matches src_pkg)');
442
443 CREATE VIEW bug_package (bug,pkg_id,pkg_type,package) AS
444        SELECT b.bug,b.bin_pkg,'binary',bp.pkg FROM bug_binpackage b JOIN bin_pkg bp ON bp.id=b.bin_pkg UNION
445               SELECT s.bug,s.src_pkg,'source',sp.pkg FROM bug_srcpackage s JOIN src_pkg sp ON sp.id=s.src_pkg UNION
446        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
447               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;
448
449 CREATE VIEW binary_versions (src_pkg, src_ver, bin_pkg, arch, bin_ver) AS
450        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,
451        svb.ver AS src_ver_based_on, spb.pkg AS src_pkg_based_on
452        FROM bin_ver b JOIN arch a ON b.arch = a.id
453                       JOIN bin_pkg bp ON b.bin_pkg  = bp.id
454                       JOIN src_ver sv ON b.src_ver  = sv.id
455                       JOIN src_pkg sp ON sv.src_pkg = sp.id
456                       LEFT OUTER JOIN src_ver svb ON sv.based_on = svb.id
457                       LEFT OUTER JOIN src_pkg spb ON spb.id = svb.src_pkg;
458
459 CREATE TABLE suite (
460        id SERIAL PRIMARY KEY,
461        codename TEXT NOT NULL,
462        suite_name TEXT,
463        version TEXT,
464        active BOOLEAN DEFAULT TRUE);
465 CREATE UNIQUE INDEX suite_idx_codename ON suite(codename);
466 CREATE UNIQUE INDEX suite_suite_name_key ON suite(suite_name);
467 CREATE UNIQUE INDEX suite_idx_version ON suite(version);
468 INSERT INTO table_comments VALUES ('suite','Debian Release Suite (stable, testing, etc.)');
469 INSERT INTO column_comments VALUES ('suite','id','Suite id');
470 INSERT INTO column_comments VALUES ('suite','suite_name','Suite name (testing, stable, etc.)');
471 INSERT INTO column_comments VALUES ('suite','version','Suite version; NULL if there is no appropriate version');
472 INSERT INTO column_comments VALUES ('suite','codename','Suite codename (sid, squeeze, etc.)');
473 INSERT INTO column_comments VALUES ('suite','active','TRUE if the suite is still accepting uploads');
474
475 CREATE TABLE bin_associations (
476        id SERIAL PRIMARY KEY,
477        suite INT NOT NULL REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
478        bin INT NOT NULL REFERENCES bin_ver ON DELETE CASCADE ON UPDATE CASCADE,
479        created TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
480        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
481 );
482 INSERT INTO table_comments VALUES ('bin_associations','Binary <-> suite associations');
483 INSERT INTO column_comments VALUES ('bin_associations','id','Binary <-> suite association id');
484 INSERT INTO column_comments VALUES ('bin_associations','suite','Suite id (matches suite)');
485 INSERT INTO column_comments VALUES ('bin_associations','bin','Binary version id (matches bin_ver)');
486 INSERT INTO column_comments VALUES ('bin_associations','created','Time this binary package entered this suite');
487 INSERT INTO column_comments VALUES ('bin_associations','modified','Time this entry was modified');
488 CREATE UNIQUE INDEX bin_associations_bin_suite ON bin_associations(bin,suite);
489
490 CREATE TABLE src_associations (
491        id SERIAL PRIMARY KEY,
492        suite INT NOT NULL REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
493        source INT NOT NULL REFERENCES src_ver ON DELETE CASCADE ON UPDATE CASCADE,
494        created TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
495        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
496 );
497 INSERT INTO table_comments VALUES ('src_associations','Source <-> suite associations');
498 INSERT INTO column_comments VALUES ('src_associations','id','Source <-> suite association id');
499 INSERT INTO column_comments VALUES ('src_associations','suite','Suite id (matches suite)');
500 INSERT INTO column_comments VALUES ('src_associations','source','Source version id (matches src_ver)');
501 INSERT INTO column_comments VALUES ('src_associations','created','Time this source package entered this suite');
502 INSERT INTO column_comments VALUES ('src_associations','modified','Time this entry was modified');
503 CREATE UNIQUE INDEX src_associations_source_suite ON src_associations(source,suite);
504
505
506 CREATE TYPE bug_status_type AS ENUM ('absent','found','fixed','undef');
507 CREATE TABLE bug_status_cache (
508        bug INT NOT NULL REFERENCES bug ON DELETE CASCADE ON UPDATE CASCADE,
509        suite INT REFERENCES suite ON DELETE CASCADE ON UPDATE CASCADE,
510        arch INT REFERENCES arch ON DELETE CASCADE ON UPDATE CASCADE,
511        status bug_status_type NOT NULL,
512        modified TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
513        asof TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
514 );
515 CREATE UNIQUE INDEX bug_status_cache_bug_suite_arch_idx ON
516        bug_status_cache(bug,suite,arch);
517 CREATE UNIQUE INDEX bug_status_cache_bug_col_suite_col_arch_idx ON
518        bug_status_cache(bug,COALESCE(suite,0),COALESCE(arch,0));
519 CREATE INDEX bug_status_cache_idx_bug ON bug_status_cache(bug);
520 CREATE INDEX bug_status_cache_idx_status ON bug_status_cache(status);
521 CREATE INDEX bug_status_cache_idx_arch ON bug_status_cache(arch);
522 CREATE INDEX bug_status_cache_idx_suite ON bug_status_cache(suite);
523 CREATE INDEX bug_status_cache_idx_asof ON bug_status_cache(asof);
524 INSERT INTO table_comments  VALUES ('bug_status_cache','Bug Status Cache');
525 INSERT INTO column_comments VALUES ('bug_status_cache','bug','Bug number (matches bug)');
526 INSERT INTO column_comments VALUES ('bug_status_cache','suite','Suite id (matches suite)');
527 INSERT INTO column_comments VALUES ('bug_status_cache','arch','Architecture id (matches arch)');
528 INSERT INTO column_comments VALUES ('bug_status_cache','status','Status (bug status)');
529 INSERT INTO column_comments VALUES ('bug_status_cache','modified','Time that this status was last modified');
530 INSERT INTO column_comments VALUES ('bug_status_cache','asof','Time that this status was last calculated');
531
532
533
534 CREATE TABLE message (
535        id SERIAL PRIMARY KEY,
536        msgid TEXT NOT NULL DEFAULT '',
537        from_complete TEXT NOT NULL DEFAULT '',
538        to_complete TEXT NOT NULL DEFAULT '',
539        subject TEXT NOT NULL DEFAULT '',
540        sent_date TIMESTAMP WITH TIME ZONE,
541        refs TEXT NOT NULL DEFAULT '',
542        spam_score FLOAT NOT NULL DEFAULT 0,
543        is_spam BOOLEAN NOT NULL DEFAULT FALSE
544 );
545 INSERT INTO table_comments VALUES ('message','Messages sent to bugs');
546 INSERT INTO column_comments VALUES ('message','id','Message id');
547 INSERT INTO column_comments VALUES ('message','msgid','Message id header');
548 INSERT INTO column_comments VALUES ('message','from_complete','Complete from header of message');
549 INSERT INTO column_comments VALUES ('message','to_complete','Complete to header of message');
550 INSERT INTO column_comments VALUES ('message','subject','Subject of the message');
551 INSERT INTO column_comments VALUES ('message','sent_date','Time/date message was sent (from Date header)');
552 INSERT INTO column_comments VALUES ('message','refs','Contents of References: header');
553 INSERT INTO column_comments VALUES ('message','spam_score','Spam score from spamassassin');
554 INSERT INTO column_comments VALUES ('message','is_spam','True if this message was spam and should not be shown');
555 CREATE INDEX message_msgid_idx ON message(msgid);
556 CREATE UNIQUE INDEX message_msgid_from_complete_to_complete_subject_idx
557     ON message(msgid,from_complete,to_complete,subject);
558 CREATE INDEX message_subject_idx ON message(subject);
559
560 CREATE TABLE message_refs (
561        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
562        refs INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
563        inferred BOOLEAN DEFAULT FALSE,
564        primary_ref BOOLEAN DEFAULT FALSE,
565        CONSTRAINT message_doesnt_reference_itself CHECK (message <> refs)
566 );
567 CREATE UNIQUE INDEX message_refs_message_refs_idx ON message_refs(message,refs);
568 CREATE INDEX message_refs_idx_refs ON message_refs(refs);
569 CREATE INDEX message_refs_idx_message ON message_refs(message);
570 INSERT INTO table_comments VALUES ('message_refs','Message references');
571 INSERT INTO column_comments VALUES ('message_refs','message','Message id (matches message)');
572 INSERT INTO column_comments VALUES ('message_refs','refs','Reference id (matches message)');
573 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');
574 INSERT INTO column_comments VALUES ('message_refs','primary_ref','TRUE if this message->ref came from In-Reply-To: or similar.');
575
576
577
578 CREATE TABLE correspondent_full_name(
579        correspondent INT NOT NULL REFERENCES correspondent ON DELETE CASCADE ON UPDATE CASCADE,
580        full_name TEXT NOT NULL,
581        last_seen TIMESTAMP NOT NULL DEFAULT NOW()
582 );
583 CREATE UNIQUE INDEX correspondent_full_name_correspondent_full_name_idx 
584     ON correspondent_full_name(correspondent,full_name);
585 CREATE INDEX correspondent_full_name_idx_full_name ON correspondent_full_name(full_name);
586 CREATE INDEX correspondent_full_name_idx_last_seen ON correspondent_full_name(last_seen);
587 INSERT INTO table_comments VALUES ('correspondent_full_name','Full names of BTS correspondents');
588 INSERT INTO column_comments VALUES ('correspondent_full_name','correspondent','Correspondent ID (matches correspondent)');
589 INSERT INTO column_comments VALUES ('correspondent_full_name','full_name','Correspondent full name (includes e-mail address)');
590
591 CREATE TYPE message_correspondent_type AS ENUM ('to','from','envfrom','cc','recv');
592
593 CREATE TABLE message_correspondent (
594        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
595        correspondent INT NOT NULL REFERENCES correspondent ON DELETE CASCADE ON UPDATE CASCADE,
596        correspondent_type message_correspondent_type NOT NULL DEFAULT 'to'
597 );
598 INSERT INTO table_comments VALUES ('message_correspondent','Linkage between correspondent and message');
599 INSERT INTO column_comments VALUES ('message_correspondent','message','Message id (matches message)');
600 INSERT INTO column_comments VALUES ('message_correspondent','correspondent','Correspondent (matches correspondent)');
601 INSERT INTO column_comments VALUES ('message_correspondent','correspondent_type','Type of correspondent (to, from, envfrom, cc, etc.)');
602
603 CREATE UNIQUE INDEX message_correspondent_message_correspondent_correspondent_t_idx 
604     ON message_correspondent(message,correspondent,correspondent_type);
605 CREATE INDEX message_correspondent_idx_correspondent ON message_correspondent(correspondent);
606 CREATE INDEX message_correspondent_idx_message ON message_correspondent(message);
607
608 CREATE TABLE bug_message (
609        bug INT NOT NULL REFERENCES bug ON DELETE CASCADE ON UPDATE CASCADE,
610        message INT NOT NULL REFERENCES message ON DELETE CASCADE ON UPDATE CASCADE,
611        message_number INT NOT NULL,
612        bug_log_offset INT,
613        offset_valid TIMESTAMP WITH TIME ZONE
614 );
615 CREATE UNIQUE INDEX bug_message_bug_message_idx ON bug_message(bug,message);
616 CREATE INDEX bug_message_idx_bug_message_number ON bug_message(bug,message_number);
617 INSERT INTO table_comments VALUES ('bug_mesage','Mapping between a bug and a message');
618 INSERT INTO column_comments VALUES ('bug_message','bug','Bug id (matches bug)');
619 INSERT INTO column_comments VALUES ('bug_message','message','Message id (matches message)');
620 INSERT INTO column_comments VALUES ('bug_message','message_number','Message number in the bug log');
621 INSERT INTO column_comments VALUES ('bug_message','bug_log_offset','Byte offset in the bug log');
622 INSERT INTO column_comments VALUES ('bug_message','offset_valid','Time offset was valid');
623
624 CREATE VIEW bug_status --(id,bug_num,tags,subject,
625 --        severity,package,originator,log_modified,date,
626 --        last_modified, blocks, blockedby, mergedwith,
627 --        fixed_versions,found_versions)
628        AS
629        SELECT b.id AS id,
630        b.id AS bug_num,
631        string_agg(t.tag,',') AS tags,
632        b.subject AS subject,
633        (SELECT s.severity FROM severity s WHERE s.id=b.severity) AS severity,
634        (SELECT string_agg(package.package,',' ORDER BY package)
635         FROM (SELECT bp.pkg AS package
636                      FROM bug_binpackage bbp
637                      JOIN bin_pkg bp ON bbp.bin_pkg=bp.id
638                      WHERE bbp.bug=b.id
639               UNION
640               SELECT CONCAT('src:',sp.pkg) AS package
641                      FROM bug_srcpackage bsp
642                      JOIN src_pkg sp ON bsp.src_pkg=sp.id
643                      WHERE bsp.bug=b.id) AS package
644         ) AS package,
645        (SELECT string_agg(affects.affects,',' ORDER BY affects)
646         FROM (SELECT bp.pkg AS affects
647                      FROM bug_affects_binpackage bbp
648                      JOIN bin_pkg bp ON bbp.bin_pkg=bp.id
649                      WHERE bbp.bug=b.id
650               UNION
651               SELECT CONCAT('src:',sp.pkg) AS affects
652                      FROM bug_affects_srcpackage bsp
653                      JOIN src_pkg sp ON bsp.src_pkg=sp.id
654                      WHERE bsp.bug=b.id) AS affects
655         ) AS affects,
656         (SELECT msgid FROM message m LEFT JOIN bug_message bm ON bm.message=m.id
657                 WHERE bm.bug=b.id ORDER BY m.sent_date ASC limit 1) AS message_id,
658         b.submitter_full AS originator,
659         EXTRACT(EPOCH FROM b.log_modified) AS log_modified,
660         EXTRACT(EPOCH FROM b.creation) AS date,
661         EXTRACT(EPOCH FROM b.last_modified) AS last_modified,
662         b.done_full AS done,
663         string_agg(bb.blocks::text,' ' ORDER BY bb.blocks) AS blocks,
664         string_agg(bbb.bug::text,' ' ORDER BY bbb.bug) AS blockedby,
665         (SELECT string_agg(bug.bug::text,' ' ORDER BY bug.bug)
666          FROM (SELECT bm.merged AS bug FROM bug_merged bm WHERE bm.bug=b.id
667                           UNION
668          SELECT bm.bug AS bug FROM bug_merged bm WHERE bm.merged=b.id) AS bug) AS mergedwith,
669         (SELECT string_agg(bv.ver_string,' ') FROM bug_ver bv WHERE bv.bug=b.id AND bv.found IS TRUE)
670                 AS found_versions,
671         (SELECT string_agg(bv.ver_string,' ') FROM bug_ver bv WHERE bv.bug=b.id AND bv.found IS FALSE)
672                 AS fixed_versions
673         FROM bug b
674         LEFT JOIN bug_tag bt ON bt.bug=b.id
675         LEFT JOIN tag t ON bt.tag=t.id
676         LEFT JOIN bug_blocks bb ON bb.bug=b.id
677         LEFT JOIN bug_blocks bbb ON bbb.blocks=b.id
678         GROUP BY b.id;