forked from gochan-org/gochan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitdb_postgres.sql
271 lines (248 loc) · 9.63 KB
/
initdb_postgres.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
-- Gochan master template for new database script
-- Contains macros in the form [curlybrace open]macro text[curlybrace close]
-- Macros are substituted by build_initdb.py to the supported database files. Must not contain extra spaces
-- Versioning numbering goes by whole numbers. Upgrade script migrate existing databases between versions
-- Foreign and unique constraints must be named so they can be dropped.
-- MYSQL requires constraint names to be unique globally, hence the long constraint names.
-- Database version: 1
CREATE TABLE DBPREFIXdatabase_version(
component VARCHAR(40) NOT NULL PRIMARY KEY,
version INT NOT NULL
);
CREATE TABLE DBPREFIXsections(
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
abbreviation TEXT NOT NULL,
position SMALLINT NOT NULL,
hidden BOOL NOT NULL
);
CREATE TABLE DBPREFIXboards(
id BIGSERIAL PRIMARY KEY,
section_id BIGINT NOT NULL,
uri VARCHAR(45) NOT NULL,
dir VARCHAR(45) NOT NULL,
navbar_position SMALLINT NOT NULL,
title VARCHAR(45) NOT NULL,
subtitle VARCHAR(64) NOT NULL,
description VARCHAR(64) NOT NULL,
max_file_size INT NOT NULL,
max_threads SMALLINT NOT NULL,
default_style VARCHAR(45) NOT NULL,
locked BOOL NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
anonymous_name VARCHAR(45) NOT NULL DEFAULT 'Anonymous',
force_anonymous BOOL NOT NULL,
autosage_after SMALLINT NOT NULL,
no_images_after SMALLINT NOT NULL,
max_message_length SMALLINT NOT NULL,
min_message_length SMALLINT NOT NULL,
allow_embeds BOOL NOT NULL,
redirect_to_thread BOOL NOT NULL,
require_file BOOL NOT NULL,
enable_catalog BOOL NOT NULL,
CONSTRAINT boards_section_id_fk FOREIGN KEY(section_id) REFERENCES DBPREFIXsections(id),
CONSTRAINT boards_dir_unique UNIQUE(dir),
CONSTRAINT boards_uri_unique UNIQUE(uri)
);
CREATE TABLE DBPREFIXthreads(
id BIGSERIAL PRIMARY KEY,
board_id BIGINT NOT NULL,
locked BOOL NOT NULL DEFAULT FALSE,
stickied BOOL NOT NULL DEFAULT FALSE,
anchored BOOL NOT NULL DEFAULT FALSE,
cyclical BOOL NOT NULL DEFAULT FALSE,
last_bump TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
is_deleted BOOL NOT NULL DEFAULT FALSE,
CONSTRAINT threads_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE
);
CREATE INDEX thread_deleted_index ON DBPREFIXthreads(is_deleted);
CREATE TABLE DBPREFIXposts(
id BIGSERIAL PRIMARY KEY,
thread_id BIGINT NOT NULL,
is_top_post BOOL NOT NULL DEFAULT FALSE,
ip VARCHAR(45) NOT NULL,
created_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
name VARCHAR(50) NOT NULL DEFAULT '',
tripcode VARCHAR(10) NOT NULL DEFAULT '',
is_role_signature BOOL NOT NULL DEFAULT FALSE,
email VARCHAR(50) NOT NULL DEFAULT '',
subject VARCHAR(100) NOT NULL DEFAULT '',
message TEXT NOT NULL,
message_raw TEXT NOT NULL,
password TEXT NOT NULL,
deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
is_deleted BOOL NOT NULL DEFAULT FALSE,
banned_message TEXT,
CONSTRAINT posts_thread_id_fk FOREIGN KEY(thread_id) REFERENCES DBPREFIXthreads(id) ON DELETE CASCADE
);
CREATE INDEX top_post_index ON DBPREFIXposts(is_top_post);
CREATE TABLE DBPREFIXfiles(
id BIGSERIAL PRIMARY KEY,
post_id BIGINT NOT NULL,
file_order INT NOT NULL,
original_filename VARCHAR(255) NOT NULL,
filename VARCHAR(45) NOT NULL,
checksum TEXT NOT NULL,
file_size INT NOT NULL,
is_spoilered BOOL NOT NULL,
thumbnail_width INT NOT NULL,
thumbnail_height INT NOT NULL,
width INT NOT NULL,
height INT NOT NULL,
CONSTRAINT files_post_id_fk FOREIGN KEY(post_id) REFERENCES DBPREFIXposts(id) ON DELETE CASCADE,
CONSTRAINT files_post_id_file_order_unique UNIQUE(post_id, file_order)
);
CREATE TABLE DBPREFIXstaff(
id BIGSERIAL PRIMARY KEY,
username VARCHAR(45) NOT NULL,
password_checksum VARCHAR(120) NOT NULL,
global_rank INT,
added_on TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
is_active BOOL NOT NULL DEFAULT TRUE,
CONSTRAINT staff_username_unique UNIQUE(username)
);
CREATE TABLE DBPREFIXsessions(
id BIGSERIAL PRIMARY KEY,
staff_id BIGINT NOT NULL,
expires TIMESTAMP NOT NULL,
data VARCHAR(45) NOT NULL,
CONSTRAINT sessions_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE
);
CREATE TABLE DBPREFIXboard_staff(
board_id BIGINT NOT NULL,
staff_id BIGINT NOT NULL,
CONSTRAINT board_staff_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE,
CONSTRAINT board_staff_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id) ON DELETE CASCADE,
CONSTRAINT board_staff_pk PRIMARY KEY (board_id,staff_id)
);
CREATE TABLE DBPREFIXannouncements(
id BIGSERIAL PRIMARY KEY,
staff_id BIGINT NOT NULL,
subject VARCHAR(45) NOT NULL,
message TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT announcements_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id)
);
CREATE TABLE DBPREFIXip_ban(
id BIGSERIAL PRIMARY KEY,
staff_id BIGINT NOT NULL,
board_id BIGINT,
banned_for_post_id BIGINT,
copy_post_text TEXT NOT NULL,
is_thread_ban BOOL NOT NULL,
is_active BOOL NOT NULL,
ip VARCHAR(45) NOT NULL,
issued_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
can_appeal BOOL NOT NULL,
CONSTRAINT ip_ban_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE,
CONSTRAINT ip_ban_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT ip_ban_banned_for_post_id_fk FOREIGN KEY(banned_for_post_id) REFERENCES DBPREFIXposts(id) ON DELETE SET NULL
);
CREATE TABLE DBPREFIXip_ban_audit(
ip_ban_id BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
staff_id BIGINT NOT NULL,
is_active BOOL NOT NULL,
is_thread_ban BOOL NOT NULL,
expires_at TIMESTAMP NOT NULL,
appeal_at TIMESTAMP NOT NULL,
permanent BOOL NOT NULL,
staff_note VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
can_appeal BOOL NOT NULL,
PRIMARY KEY(ip_ban_id, timestamp),
CONSTRAINT ip_ban_audit_ip_ban_id_fk FOREIGN KEY(ip_ban_id) REFERENCES DBPREFIXip_ban(id) ON DELETE CASCADE,
CONSTRAINT ip_ban_audit_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id)
);
CREATE TABLE DBPREFIXip_ban_appeals(
id BIGSERIAL PRIMARY KEY,
staff_id BIGINT,
ip_ban_id BIGINT NOT NULL,
appeal_text TEXT NOT NULL,
staff_response TEXT,
is_denied BOOL NOT NULL,
CONSTRAINT ip_ban_appeals_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT ip_ban_appeals_ip_ban_id_fk FOREIGN KEY(ip_ban_id) REFERENCES DBPREFIXip_ban(id) ON DELETE CASCADE
);
CREATE TABLE DBPREFIXip_ban_appeals_audit(
appeal_id BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
staff_id BIGINT,
appeal_text TEXT NOT NULL,
staff_response TEXT,
is_denied BOOL NOT NULL,
PRIMARY KEY(appeal_id, timestamp),
CONSTRAINT ip_ban_appeals_audit_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT ip_ban_appeals_audit_appeal_id_fk FOREIGN KEY(appeal_id) REFERENCES DBPREFIXip_ban_appeals(id) ON DELETE CASCADE
);
CREATE TABLE DBPREFIXreports(
id BIGSERIAL PRIMARY KEY,
handled_by_staff_id BIGINT,
post_id BIGINT NOT NULL,
ip VARCHAR(45) NOT NULL,
reason TEXT NOT NULL,
is_cleared BOOL NOT NULL,
CONSTRAINT reports_handled_by_staff_id_fk FOREIGN KEY(handled_by_staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT reports_post_id_fk FOREIGN KEY(post_id) REFERENCES DBPREFIXposts(id) ON DELETE CASCADE
);
CREATE TABLE DBPREFIXreports_audit(
report_id BIGINT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
handled_by_staff_id BIGINT,
is_cleared BOOL NOT NULL,
CONSTRAINT reports_audit_handled_by_staff_id_fk FOREIGN KEY(handled_by_staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT reports_audit_report_id_fk FOREIGN KEY(report_id) REFERENCES DBPREFIXreports(id) ON DELETE CASCADE
);
CREATE TABLE DBPREFIXfilename_ban(
id BIGSERIAL PRIMARY KEY,
board_id BIGINT,
staff_id BIGINT NOT NULL,
staff_note VARCHAR(255) NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
filename VARCHAR(255) NOT NULL,
is_regex BOOL NOT NULL,
CONSTRAINT filename_ban_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE,
CONSTRAINT filename_ban_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id)
);
CREATE TABLE DBPREFIXusername_ban(
id BIGSERIAL PRIMARY KEY,
board_id BIGINT,
staff_id BIGINT NOT NULL,
staff_note VARCHAR(255) NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
username VARCHAR(255) NOT NULL,
is_regex BOOL NOT NULL,
CONSTRAINT username_ban_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE,
CONSTRAINT username_ban_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id)
);
CREATE TABLE DBPREFIXfile_ban(
id BIGSERIAL PRIMARY KEY,
board_id BIGINT,
staff_id BIGINT NOT NULL,
staff_note VARCHAR(255) NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
checksum TEXT NOT NULL,
CONSTRAINT file_ban_board_id_fk FOREIGN KEY(board_id) REFERENCES DBPREFIXboards(id) ON DELETE CASCADE,
CONSTRAINT file_ban_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id)
);
CREATE TABLE DBPREFIXwordfilters(
id BIGSERIAL PRIMARY KEY,
board_dirs VARCHAR(255) DEFAULT '*',
staff_id BIGINT NOT NULL,
staff_note VARCHAR(255) NOT NULL,
issued_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
search VARCHAR(75) NOT NULL,
is_regex BOOL NOT NULL,
change_to VARCHAR(75) NOT NULL,
CONSTRAINT wordfilters_staff_id_fk FOREIGN KEY(staff_id) REFERENCES DBPREFIXstaff(id),
CONSTRAINT wordfilters_search_check CHECK (search <> '')
);
INSERT INTO DBPREFIXdatabase_version(component, version)
VALUES('gochan', 1);