-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_ddl.sql
85 lines (65 loc) · 2.41 KB
/
db_ddl.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
START TRANSACTION ISOLATION LEVEL REPEATABLE READ;
CREATE TABLE users (
id SERIAL PRIMARY KEY,
nickname VARCHAR(128),
email VARCHAR(128) UNIQUE NOT NULL,
password_hash VARCHAR(256),
rating INTEGER DEFAULT 0
);
ALTER TABLE users ADD privileges INTEGER DEFAULT 1;
CREATE TYPE room_state AS ENUM ('PLAYING', 'WAITING', 'DEAD');
CREATE TABLE rooms (
id SERIAL PRIMARY KEY,
state room_state DEFAULT 'WAITING',
creation_dttm TIMESTAMP NOT NULL DEFAULT NOW()::TIMESTAMP
);
CREATE TABLE viewers (
user_id INTEGER REFERENCES users(id),
room_id INTEGER REFERENCES rooms(id),
joined_dttm TIMESTAMP DEFAULT NOW()::TIMESTAMP,
left_dttm TIMESTAMP
);
ALTER TABLE viewers ADD CONSTRAINT PK_viewers PRIMARY KEY (user_id, room_id);
CREATE TYPE game_outcome AS ENUM ('PLAYING', 'WHITE_WON', 'BLACK_WON', 'DRAW', 'CANCELLED');
CREATE TABLE games (
id SERIAL PRIMARY KEY,
outcome game_outcome DEFAULT 'PLAYING',
room_id INTEGER REFERENCES rooms(id),
time_start TIMESTAMP DEFAULT NOW()::TIMESTAMP
);
CREATE TABLE user_games (
user_id INTEGER REFERENCES users(id),
game_id INTEGER REFERENCES games(id),
is_white BOOLEAN NOT NULL
);
ALTER TABLE user_games ADD CONSTRAINT PK_user_games PRIMARY KEY (user_id, game_id);
CREATE TABLE turns (
index INTEGER,
body VARCHAR(32) NOT NULL,
dttm TIMESTAMP DEFAULT NOW()::TIMESTAMP,
user_id INTEGER,
game_id INTEGER
);
ALTER TABLE turns ADD CONSTRAINT FK_turns FOREIGN KEY (user_id, game_id) REFERENCES user_games(user_id, game_id);
CREATE TABLE messages (
user_id INTEGER,
room_id INTEGER,
msg_body VARCHAR(120),
dttm TIMESTAMP DEFAULT NOW()::TIMESTAMP
);
ALTER TABLE messages ADD CONSTRAINT PK_messages FOREIGN KEY (user_id, room_id) REFERENCES viewers(user_id, room_id);
CREATE TABLE rating_history (
user_id INTEGER,
rating INTEGER,
dttm TIMESTAMP DEFAULT NOW()::TIMESTAMP
);
ALTER TABLE rating_history ADD CONSTRAINT FK_rating FOREIGN KEY (user_id) REFERENCES users(id);
CREATE TABLE room_history (
room_id INTEGER,
state room_state,
dttm TIMESTAMP DEFAULT NOW()::TIMESTAMP
);
ALTER TABLE room_history ADD CONSTRAINT FK_rooms FOREIGN KEY (room_id) REFERENCES rooms(id);
-- добавить update скрипт который будет менять состяние всех стархы комнат
-- и добавлять время выхода вьюверам оттуда
COMMIT;