-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathschema.sql
45 lines (39 loc) · 1.27 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
BEGIN;
CREATE TABLE "users" (
"id" INTEGER,
"name" TEXT NOT NULL UNIQUE,
"email" TEXT NOT NULL UNIQUE,
"password_hash" TEXT,
"is_banned" INTEGER NOT NULL DEFAULT 0 ,
"avatar" BLOB,
PRIMARY KEY("id" AUTOINCREMENT),
CHECK ("is_banned" IN (0, 1))
);
CREATE TABLE "messages" (
"id" INTEGER,
"author_id" INTEGER,
"text" TEXT NOT NULL,
"created_at" INTEGER NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("author_id") REFERENCES "users"("id") ON DELETE SET NULL
);
CREATE TABLE "contacts" (
"id" INTEGER,
"owner_id" INTEGER NOT NULL,
"friend_id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT),
FOREIGN KEY("owner_id") REFERENCES "users"("id") ON DELETE CASCADE,
FOREIGN KEY("friend_id") REFERENCES "users"("id") ON DELETE SET NULL
);
CREATE TABLE "contact_messages" (
"contact_id" INTEGER,
"message_id" INTEGER,
PRIMARY KEY("contact_id", "message_id"),
FOREIGN KEY("contact_id") REFERENCES "contacts"("id") ON DELETE CASCADE,
FOREIGN KEY("message_id") REFERENCES "messages"("id") ON DELETE CASCADE
);
CREATE INDEX "ix_contacts_contact_id" ON "contacts" (
"owner_id"
);
COMMIT;