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