-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathschema.sql
150 lines (135 loc) · 4.62 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
CREATE TABLE db_meta
( key varchar(512) primary key
, value blob
);
INSERT INTO db_meta (key, value) VALUES ('schema_version', 2);
CREATE TABLE textes
( id integer primary key not null
, nature text not null
, num text
, nor char(12) unique -- only used during factorization
, titrefull_s text unique -- only used during factorization
, UNIQUE (nature, num)
);
CREATE TABLE textes_structs
( id char(20) unique not null
, versions text
, dossier text not null
, cid char(20) not null
, mtime int not null
);
CREATE TABLE textes_versions
( id char(20) unique not null
, nature text
, titre text
, titrefull text
, titrefull_s varchar(512)
, etat text
, date_debut day
, date_fin day
, autorite text
, ministere text
, num text
, num_sequence int
, nor char(12)
, date_publi day
, date_texte day
, derniere_modification day
, origine_publi text
, page_deb_publi int
, page_fin_publi int
, visas text
, signataires text
, tp text
, nota text
, abro text
, rect text
, dossier text not null
, cid char(20) not null
, mtime int not null
, texte_id int references textes
);
CREATE INDEX textes_versions_titrefull_s ON textes_versions (titrefull_s);
CREATE INDEX textes_versions_texte_id ON textes_versions (texte_id);
CREATE TABLE sections
( id char(20) unique not null
, titre_ta text
, commentaire text
, parent char(20) -- REFERENCES sections(id)
, dossier text not null
, cid char(20) not null
, mtime int not null
);
CREATE TABLE articles
( id char(20) unique not null
, section char(20) -- REFERENCES sections(id)
, num text
, etat text
, date_debut day
, date_fin day
, type text
, nota text
, bloc_textuel text
, dossier text not null
, cid char(20) not null
, mtime int not null
);
CREATE TABLE sommaires
( cid char(20) not null
, parent char(20) -- REFERENCES sections
, element char(20) not null -- REFERENCES articles OR sections
, debut day
, fin day
, etat text
, num text
, position int
, _source text -- to support incremental updates
);
CREATE INDEX sommaires_cid_idx ON sommaires (cid);
CREATE TABLE liens
( src_id char(20) not null
, dst_cid char(20)
, dst_id char(20)
, dst_titre text
, typelien text
, _reversed bool -- to support incremental updates
, CHECK (length(dst_cid) > 0 OR length(dst_id) > 0 OR length(dst_titre) > 0)
);
CREATE INDEX liens_src_idx ON liens (src_id) WHERE NOT _reversed;
CREATE INDEX liens_dst_idx ON liens (dst_id) WHERE _reversed;
CREATE TABLE duplicate_files
( id char(20) not null
, sous_dossier text not null
, cid char(20) not null
, dossier text not null
, mtime int not null
, data text not null
, other_cid char(20) not null
, other_dossier text not null
, other_mtime int not null
, UNIQUE (id, sous_dossier, cid, dossier)
);
CREATE TABLE textes_versions_brutes
( id char(20) unique not null
, bits int not null
, nature text
, titre text
, titrefull text
, autorite text
, num text
, date_texte day
, dossier text not null
, cid char(20) not null
, mtime int not null
);
CREATE VIEW textes_versions_brutes_view AS
SELECT a.dossier, a.cid, a.id,
(CASE WHEN b.bits & 1 > 0 THEN b.nature ELSE a.nature END) AS nature,
(CASE WHEN b.bits & 2 > 0 THEN b.titre ELSE a.titre END) AS titre,
(CASE WHEN b.bits & 4 > 0 THEN b.titrefull ELSE a.titrefull END) AS titrefull,
(CASE WHEN b.bits & 8 > 0 THEN b.autorite ELSE a.autorite END) AS autorite,
(CASE WHEN b.bits & 16 > 0 THEN b.num ELSE a.num END) AS num,
(CASE WHEN b.bits & 32 > 0 THEN b.date_texte ELSE a.date_texte END) AS date_texte
FROM textes_versions a
LEFT JOIN textes_versions_brutes b
ON b.id = a.id AND b.cid = a.cid AND b.dossier = a.dossier AND b.mtime = a.mtime;