-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
124 lines (99 loc) · 3.64 KB
/
db.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from dbinit import cnx, DB_NAME
import mysql.connector
from colorama import init, Fore, Back, Style
from mysql.connector import errorcode
init(convert=True)
# Database schema
def init_db():
cursor = cnx.cursor(buffered = True)
cursor.execute(f"""
CREATE DATABASE IF NOT EXISTS {DB_NAME};
""")
cursor.execute("""SET NAMES utf8mb4;""")
cursor.execute(f"""ALTER DATABASE {DB_NAME} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci;""")
cnx.database = DB_NAME
# Select the db
cursor.execute(f"""
USE {DB_NAME};
""")
cnx.commit()
# Create table to hold account details
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS account (
id VARCHAR(36) PRIMARY KEY ,
user_at VARCHAR(50) NOT NULL UNIQUE,
is_private BOOLEAN NOT NULL,
AVI_path VARCHAR(500),
description VARCHAR(165),
created DATE NOT NULL
);
""")
cnx.commit()
# Create the followers table
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS follows (
followed_id VARCHAR(36) NOT NULL,
follower_id VARCHAR(36) NOT NULL,
FOREIGN KEY fk_followed_id(followed_id)
REFERENCES account(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY fk_follower_id(follower_id)
REFERENCES account(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT pk PRIMARY KEY (followed_id, follower_id)
);
""")
cnx.commit()
#-----------------------------------------------------------------#
#-----------------------------------------------------------------#
# Create the table to hold MAP scores
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS MAP_score (
user_id VARCHAR(36) NOT NULL,
score DECIMAL(4,3) NOT NULL,
FOREIGN KEY fk_user_id(user_id)
REFERENCES account(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT pk PRIMARY KEY (user_id, score)
);
""")
cnx.commit()
# Create the table to hold ZOO scores
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS ZOO_score (
user_id VARCHAR(36) NOT NULL,
score DECIMAL(4,3) NOT NULL,
FOREIGN KEY fk_user_id(user_id)
REFERENCES account(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT pk PRIMARY KEY (user_id, score)
);
""")
cnx.commit()
#-----------------------------------------------------------------#
#-----------------------------------------------------------------#
# Check if db exists
def db_exists():
cursor = cnx.cursor()
cursor.execute(f"""
SHOW DATABASES LIKE '{DB_NAME}';
""")
return not not len(cursor.fetchall())
# Attempt to either connect to/initialize db
def db_connect():
try:
if not db_exists():
print(f"\nDatabase {DB_NAME} does not exist, Initializing . . .\n")
init_db()
print(f">> Connected to Database '{DB_NAME}'")
else:
cnx.database = DB_NAME
print(f">> Connected to Database '{DB_NAME}'")
return True
except mysql.connector.Error as error:
print("\n\033[91mERROR: Could not connect to database instance.\033[0m\n\t└", error.msg)
return False