-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.sql
76 lines (61 loc) · 1.97 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
65
66
67
68
69
70
71
72
73
74
75
-- Drop existing tables if they exist
DROP TABLE IF EXISTS belongings;
DROP TABLE IF EXISTS chunks_lore;
DROP TABLE IF EXISTS lore;
DROP TABLE IF EXISTS chunks;
DROP TABLE IF EXISTS users_communities;
DROP TABLE IF EXISTS eligible_users_for_communities;
DROP TABLE IF EXISTS communities_owners;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS communities;
-- Create Tables
-- Human Users
CREATE TABLE users(
id SERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
readme TEXT DEFAULT '', -- markdown readme
tier_plan INTEGER DEFAULT 1 -- 1 == ('FREE_TIER' | 2 == 'PRO_TIER' | 3 == 'SCALE_TIER' | 4 == 'ADMIN_TIER')
);
CREATE TABLE communities(
id SERIAL PRIMARY KEY,
name TEXT NOT NULL UNIQUE -- this is more like an @, which is unique to each community
);
-- Junction table between users and communities for many-to-many relationship
CREATE TABLE users_communities(
user_id INTEGER REFERENCES users(id),
community_id INTEGER REFERENCES communities(id)
);
CREATE TABLE eligible_users_for_communities(
user_id INTEGER REFERENCES users(id),
community_id INTEGER REFERENCES communities(id)
);
CREATE TABLE communities_owners(
user_id INTEGER REFERENCES users(id),
community_id INTEGER REFERENCES communities(id)
);
CREATE TABLE chunks(
id BIGINT PRIMARY KEY,
name TEXT NOT NULL,
profile TEXT NOT NULL,
community_id INTEGER REFERENCES communities(id),
parent_chunk INTEGER REFERENCES chunks(id), -- can be NULL if no parent Chunk
UNIQUE(community_id, name) -- query with this
);
-- "Knowledge"
CREATE TABLE lore(
id SERIAL PRIMARY KEY,
lore_text TEXT NOT NULL,
UNIQUE(lore_text)
);
-- Junction table between chunks and lore to deal with the many-to-many relationships
CREATE TABLE chunks_lore(
chunk_id BIGINT REFERENCES chunks(id),
lore_id INTEGER REFERENCES lore(id),
CONSTRAINT chunks_lore_pk PRIMARY KEY(chunk_id, lore_id)
);
-- "Data"/"Information"
CREATE TABLE belongings(
id SERIAL PRIMARY KEY,
content TEXT,
owner BIGINT REFERENCES chunks(id)
);