summaryrefslogtreecommitdiff
path: root/db/0-create.psql
blob: 1316a36a94961a1a11a36e56bf8c3c0068a93780 (plain)
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
CREATE DATABASE dumpfm;

CREATE TABLE users (
    user_id SERIAL PRIMARY KEY,
    nick text UNIQUE NOT NULL,
    hash text NOT NULL,
    email text NOT NULL,
    created_on timestamp NOT NULL DEFAULT now(),
    is_admin boolean NOT NULL DEFAULT false,
    avatar text NOT NULL DEFAULT '',
    contact text NOT NULL DEFAULT '',
    bio text NOT NULL DEFAULT '',
    profile_bg text
);

CREATE TABLE rooms (
    room_id SERIAL PRIMARY KEY,
    key text UNIQUE NOT NULL,
    name text NOT NULL,
    description text NOT NULL,
    created_on timestamp NOT NULL DEFAULT now(),
    admin_only bool NOT NULL DEFAULT false
);

CREATE TABLE messages (
    message_id SERIAL PRIMARY KEY,
    user_id integer NOT NULL REFERENCES users,
    room_id integer NOT NULL REFERENCES rooms,
    content text NOT NULL,
    created_on timestamp NOT NULL DEFAULT now(),
    is_image bool NOT NULL
);

-- Queries to support:
-- 1) What are my favorite images? (By room, time, or author)
-- 2) Who favorited me? (By user, image, or time)
-- 3) What are the most favorited images? (By room, time, or author)
CREATE TABLE favorites (
    favorite_id SERIAL PRIMARY KEY,
    src_user_id integer NOT NULL REFERENCES users,
    message_id integer NOT NULL REFERENCES messages,
    created_on timestamp NOT NULL DEFAULT now()
);

CREATE INDEX user_id_idx ON messages (user_id);
CREATE INDEX room_id_idx ON messages (room_id);
CREATE INDEX created_on_idx ON messages (created_on);
CREATE INDEX is_image_idx ON messages (is_image);

CREATE INDEX src_user_id_idx ON favorites (src_user_id);
CREATE INDEX favorites_created_on_idx on favorites (created_on);

INSERT INTO rooms (key, name, description, admin_only) 
       VALUES ('RoomA', 'Room A', 'Hangout', false);
INSERT INTO rooms (key, name, description, admin_only)
       VALUES ('VIP', 'The VIP Room', 'Command Post', true);