This repository was archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
180 lines (165 loc) · 7.93 KB
/
schema.sql
File metadata and controls
180 lines (165 loc) · 7.93 KB
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
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "hstore";
CREATE EXTENSION IF NOT EXISTS "pg_rational";
CREATE TABLE media
(
"id" uuid NOT NULL DEFAULT uuid_generate_v1mc() PRIMARY KEY,
"mime_type" text NOT NULL DEFAULT '',
"uploader_id" uuid NOT NULL,
"filename" text NOT NULL,
"original_filename" text NOT NULL DEFAULT '',
"hash" text NOT NULL,
"size" integer NOT NULL,
"description" text NOT NULL DEFAULT '',
"source" text NOT NULL DEFAULT '',
"created" timestamp NOT NULL DEFAULT (now() at time zone 'utc')
);
CREATE TABLE users
(
"id" uuid NOT NULL DEFAULT uuid_generate_v1mc() PRIMARY KEY,
"email" text NOT NULL UNIQUE,
"username" text NOT NULL UNIQUE,
"nickname" text NOT NULL,
"password" text NOT NULL,
"bio" text NOT NULL DEFAULT '',
"joined" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"deactivated" boolean NOT NULL DEFAULT false,
"avatar_id" uuid DEFAULT NULL
CONSTRAINT "user_avatar" REFERENCES media (id) ON DELETE SET NULL
);
ALTER TABLE media
ADD CONSTRAINT "media_uploader" FOREIGN KEY (uploader_id) REFERENCES users (id) ON DELETE RESTRICT;
CREATE TABLE users_extension
(
"user_id" uuid NOT NULL PRIMARY KEY
CONSTRAINT "extension_user" REFERENCES users (id) ON DELETE CASCADE,
"settings" jsonb NOT NULL DEFAULT '{}'
);
CREATE TABLE spaces
(
"id" uuid NOT NULL DEFAULT uuid_generate_v1mc() PRIMARY KEY,
"name" text NOT NULL,
"description" text NOT NULL DEFAULT '',
"created" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"modified" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"owner_id" uuid NOT NULL
CONSTRAINT "space_owner" REFERENCES users (id) ON DELETE RESTRICT,
"is_public" boolean NOT NULL DEFAULT true,
"deleted" boolean NOT NULL DEFAULT false,
"password" text NOT NULL DEFAULT '', -- plain text
"language" text NOT NULL DEFAULT '', -- ISO 639-1
"default_dice_type" text NOT NULL DEFAULT 'd20', -- d20, d100, FATE ...
"invite_token" uuid NOT NULL DEFAULT gen_random_uuid(),
"explorable" boolean NOT NULL DEFAULT false,
"allow_spectator" boolean NOT NULL DEFAULT true
);
CREATE TABLE space_members
(
"user_id" uuid NOT NULL
CONSTRAINT "space_member_user" REFERENCES users (id) ON DELETE CASCADE,
"space_id" uuid NOT NULL
CONSTRAINT "space_member_space" REFERENCES spaces (id) ON DELETE CASCADE,
"is_admin" boolean NOT NULL DEFAULT false,
"join_date" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
CONSTRAINT "user_space_id_pair" PRIMARY KEY ("user_id", "space_id")
);
CREATE TABLE channels
(
"id" uuid NOT NULL DEFAULT uuid_generate_v1mc() PRIMARY KEY,
"name" text NOT NULL,
"topic" text NOT NULL DEFAULT '',
"space_id" uuid NOT NULL
CONSTRAINT "channel_space" REFERENCES spaces (id) ON DELETE CASCADE,
"created" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"is_public" boolean NOT NULL DEFAULT true,
"deleted" boolean NOT NULL DEFAULT false,
"default_dice_type" text NOT NULL DEFAULT 'd20',
"default_roll_command" text NOT NULL DEFAULT 'd',
"is_document" bool NOT NULL DEFAULT false,
"old_name" text NOT NULL DEFAULT '',
CONSTRAINT "unique_channel_name_in_space" UNIQUE (space_id, name)
);
CREATE TABLE channel_members
(
"user_id" uuid NOT NULL
CONSTRAINT "channel_member_user" REFERENCES users (id) ON DELETE CASCADE,
"channel_id" uuid NOT NULL
CONSTRAINT "channel_member_channel" REFERENCES channels (id) ON DELETE CASCADE,
"join_date" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"character_name" text NOT NULL,
text_color text DEFAULT NULL,
is_joined boolean NOT NULL DEFAULT true,
is_master bool NOT NULL DEFAULT false,
CONSTRAINT "user_channel_id_pair" PRIMARY KEY ("user_id", "channel_id")
);
CREATE TABLE messages
(
"id" uuid NOT NULL DEFAULT uuid_generate_v1mc() PRIMARY KEY,
"sender_id" uuid NOT NULL
CONSTRAINT "message_sender" REFERENCES users (id) ON DELETE CASCADE,
"channel_id" uuid NOT NULL
CONSTRAINT "message_channel" REFERENCES channels (id) ON DELETE CASCADE,
"parent_message_id" uuid DEFAULT null
CONSTRAINT "message_parent" REFERENCES messages (id) ON DELETE CASCADE,
"name" text NOT NULL,
"media_id" uuid DEFAULT null,
"seed" bytea NOT NULL DEFAULT gen_random_bytes(4),
"deleted" boolean NOT NULL DEFAULT false,
"in_game" boolean NOT NULL DEFAULT false,
"is_action" boolean NOT NULL DEFAULT false,
"is_master" boolean NOT NULL DEFAULT false,
"pinned" boolean NOT NULL DEFAULT false,
"tags" text[] NOT NULL DEFAULT '{}',
-- A mark that represents the message was invalid.
"folded" boolean NOT NULL DEFAULT false,
"text" text NOT NULL DEFAULT '',
-- whisper_to_users values mean
-- null: public message.
-- []: only master is able to read the message.
-- [user1, user2]: both master, user1 and user2 are able to read the message.
"whisper_to_users" uuid[] DEFAULT null,
"entities" jsonb NOT NULL DEFAULT '[]',
"created" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"modified" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"order_date" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"order_offset" integer NOT NULL DEFAULT 0,
"pos" float NOT NULL DEFAULT 0.0
);
ALTER TABLE messages
ADD CONSTRAINT pos_unique UNIQUE (channel_id, pos) DEFERRABLE INITIALLY IMMEDIATE;
CREATE INDEX "message_pos" ON messages (pos);
CREATE INDEX "message_tags" ON messages USING GIN (tags);
CREATE INDEX "message_channel" ON messages USING btree (channel_id);
CREATE TABLE restrained_members
(
"user_id" uuid NOT NULL
CONSTRAINT "restrained_member_user" REFERENCES users (id) ON DELETE CASCADE,
"space_id" uuid NOT NULL
CONSTRAINT "restrained_member_space" REFERENCES spaces (id) ON DELETE CASCADE,
"blocked" boolean NOT NULL DEFAULT false,
"muted" boolean NOT NULL DEFAULT false,
"restrained_date" timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
"operator_id" uuid DEFAULT null
CONSTRAINT "restrain_operator" REFERENCES users (id) ON DELETE SET NULL,
CONSTRAINT "restrained_space_id_pair" PRIMARY KEY (user_id, space_id)
);
CREATE TYPE event_type AS ENUM (
'Joined',
'Left',
'NewMaster',
'NewAdmin'
);
CREATE TABLE events
(
"id" uuid NOT NULL PRIMARY KEY,
"type" event_type NOT NULL,
"channel_id" uuid DEFAULT NULL
CONSTRAINT "event_channel" REFERENCES channels (id) ON DELETE CASCADE,
"space_id" uuid DEFAULT NULL
CONSTRAINT "event_space" REFERENCES spaces (id) ON DELETE CASCADE,
"receiver_id" uuid
CONSTRAINT "event_receiver" REFERENCES users (id) ON DELETE CASCADE,
"payload" jsonb NOT NULL DEFAULT '{}',
"created" timestamp NOT NULL default (now() at time zone 'utc')
);