-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_database.py
More file actions
106 lines (90 loc) · 3.02 KB
/
make_database.py
File metadata and controls
106 lines (90 loc) · 3.02 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import pymysql
import dotenv
def create_database():
dotenv.load_dotenv()
env = os.environ
db_name = env.get('DB_NAME', 'circle')
conn = pymysql.connect(
host=env.get('DB_HOST', 'localhost'),
user=env.get('DB_USER', 'root'),
password=env.get('DB_PASS', ''),
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
cursor = conn.cursor()
cursor.execute(f'DROP DATABASE IF EXISTS {db_name};')
cursor.execute(f'CREATE DATABASE IF NOT EXISTS {db_name};')
cursor.execute(f'USE {db_name};')
cursor.execute('''
CREATE TABLE IF NOT EXISTS circles (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE,
password_hash VARCHAR(255) NOT NULL,
name VARCHAR(100) NOT NULL,
bio VARCHAR(256) DEFAULT '',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS auths (
id INT PRIMARY KEY AUTO_INCREMENT,
circle_id INT,
token VARCHAR(255) NOT NULL,
agent VARCHAR(255) NOT NULL,
FOREIGN KEY(circle_id) REFERENCES circles(id)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS bubbles (
id INT PRIMARY KEY AUTO_INCREMENT,
circle_id INT,
content VARCHAR(256) NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
anchor INT DEFAULT NULL,
FOREIGN KEY(circle_id) REFERENCES circles(id),
FOREIGN KEY(anchor) REFERENCES bubbles(id)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS joins (
id INT PRIMARY KEY AUTO_INCREMENT,
joiner_id INT,
joinee_id INT,
FOREIGN KEY(joiner_id) REFERENCES circles(id),
FOREIGN KEY(joinee_id) REFERENCES circles(id)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS pops (
id INT PRIMARY KEY AUTO_INCREMENT,
popper_id INT NOT NULL,
popped_id INT NOT NULL,
emoji VARCHAR(10) NOT NULL,
FOREIGN KEY(popper_id) REFERENCES circles(id),
FOREIGN KEY(popped_id) REFERENCES bubbles(id),
UNIQUE(popper_id, popped_id, emoji)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS notifications (
id INT PRIMARY KEY AUTO_INCREMENT,
circle_id INT,
content VARCHAR(512) NOT NULL,
is_read BOOLEAN DEFAULT FALSE,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(circle_id) REFERENCES circles(id)
);
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS medias (
id INT PRIMARY KEY AUTO_INCREMENT,
bubble_id INT,
url VARCHAR(255) NOT NULL,
FOREIGN KEY(bubble_id) REFERENCES bubbles(id)
);
''')
conn.commit()
conn.close()
if __name__ == "__main__":
create_database()