-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
71 lines (57 loc) · 2.3 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- SPDX-License-Identifier: AGPL-3.0-or-later
CREATE TABLE polit_prisoner_source
(
id UUID,
last_import TIMESTAMPTZ NOT NULL,
CONSTRAINT polit_prisoner_source_pk PRIMARY KEY (id)
);
CREATE TABLE polit_prisoner
(
id SMALLINT,
name VARCHAR(255) NOT NULL,
born DATE,
source UUID NOT NULL,
last_seen TIMESTAMPTZ NOT NULL,
awareness SMALLINT DEFAULT NULL,
born_month SMALLINT NOT NULL GENERATED ALWAYS AS ( CASE WHEN born ISNULL THEN 0 ELSE EXTRACT(MONTH FROM born) END ) STORED,
born_dom SMALLINT NOT NULL GENERATED ALWAYS AS ( CASE WHEN born ISNULL THEN 0 ELSE EXTRACT(DAY FROM born) END ) STORED,
CONSTRAINT polit_prisoner_pk PRIMARY KEY (id),
CONSTRAINT polit_prisoner_uk_name UNIQUE (name),
CONSTRAINT polit_prisoner_fk_source FOREIGN KEY (source) REFERENCES polit_prisoner_source (id)
);
CREATE INDEX polit_prisoner_ix_birthday ON polit_prisoner (born_month, born_dom);
CREATE TABLE polit_prisoner_field
(
id SMALLINT,
name VARCHAR(255) NOT NULL,
CONSTRAINT polit_prisoner_field_pk PRIMARY KEY (id),
CONSTRAINT polit_prisoner_field_uk_name UNIQUE (name)
);
CREATE TABLE polit_prisoner_attr
(
polit_prisoner SMALLINT,
field SMALLINT,
value TEXT NOT NULL,
last_seen TIMESTAMPTZ NOT NULL,
CONSTRAINT polit_prisoner_attr_pk PRIMARY KEY (polit_prisoner, field),
CONSTRAINT polit_prisoner_attr_fk_polit_prisoner FOREIGN KEY (polit_prisoner) REFERENCES polit_prisoner (id),
CONSTRAINT polit_prisoner_attr_fk_field FOREIGN KEY (field) REFERENCES polit_prisoner_field (id)
);
CREATE TABLE web_user
(
id SMALLINT,
name VARCHAR(255) NOT NULL,
CONSTRAINT web_user_pk PRIMARY KEY (id),
CONSTRAINT web_user_uk_name UNIQUE (name)
);
CREATE TABLE polit_prisoner_awareness
(
polit_prisoner SMALLINT,
edited TIMESTAMPTZ,
editor SMALLINT NOT NULL,
awareness SMALLINT DEFAULT NULL,
comment TEXT NOT NULL,
CONSTRAINT polit_prisoner_awareness_pk PRIMARY KEY (polit_prisoner, edited),
CONSTRAINT polit_prisoner_awareness_fk_polit_prisoner FOREIGN KEY (polit_prisoner) REFERENCES polit_prisoner (id),
CONSTRAINT polit_prisoner_awareness_fk_editor FOREIGN KEY (editor) REFERENCES web_user (id)
);