-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
65 lines (47 loc) · 1.99 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
drop table if exists users;
create table users (
id integer not null primary key autoincrement,
username text unique not null,
fullname text not null,
password text not null,
avatar_path text null,
created_at timestamp default current_timestamp not null,
updated_at timestamp null,
check (updated_at is null or updated_at > created_at)
);
drop table if exists friends;
create table friends (
your_id integer not null,
their_id integer not null,
created_at timestamp default current_timestamp not null,
primary key (your_id, their_id),
foreign key (your_id) references users(id),
foreign key (their_id) references users(id)
);
drop table if exists friend_requests;
-- a friend request is pending by existence
-- once the request is accepted, the users become friends and the request is deleted
-- or once the request is rejected, the request is just deleted without the users becoming friends
create table friend_requests (
sender_id integer not null,
receiver_id integer not null,
created_at timestamp default current_timestamp not null,
primary key (sender_id, receiver_id),
foreign key (sender_id) references users(id),
foreign key (receiver_id) references users(id)
);
create index idx_friend_requests_receiver on friend_requests(receiver_id, sender_id);
drop table if exists messages;
create table messages (
sender_id integer not null,
receiver_id integer not null,
id integer not null primary key autoincrement,
sent_at timestamp not null default current_timestamp,
text_contents text null,
file_reference text null,
/* one and only one is present */
check (text_contents is not null or file_reference is not null),
check (text_contents is null or file_reference is null),
foreign key (sender_id) references users (id),
foreign key (receiver_id) references users (id)
);