forked from itucsdb1920/itucsdb1920
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbinit.py
84 lines (70 loc) · 2.32 KB
/
dbinit.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
import os
import sys
import json
import psycopg2 as dbapi2
from book import Book
def readFile():
books = []
with open('books.json',errors='ignore') as json_file:
data = json.load(json_file)
for book in data['books']["book"]:
books.append(Book(title=book["title"], author=book["author"], genres=book["genres"], year=book["year"], pageNumber=book["pgnum"], cover=book["cover"], description=book["description"]))
return books
INIT_STATEMENTS = [
"DROP TABLE IF EXISTS REVIEW",
"DROP TABLE IF EXISTS ADMINS",
"DROP TABLE IF EXISTS BOOKWORM",
"DROP TABLE IF EXISTS GENRES",
"DROP TABLE IF EXISTS BOOK",
"DROP TABLE IF EXISTS AUTHOR",
"""CREATE TABLE AUTHOR(
ID SERIAL PRIMARY KEY,
NAME VARCHAR(50) NOT NULL,
DESCRIPTION VARCHAR(2000),
PHOTO VARCHAR(200)
)""",
"""CREATE TABLE BOOK(
ID SERIAL PRIMARY KEY,
TITLE VARCHAR(80) NOT NULL,
YR INTEGER CHECK(YR>=0 AND YR<=2019),
AUTHORID INTEGER REFERENCES AUTHOR ON DELETE SET NULL,
PGNUM INTEGER CHECK(PGNUM>=0),
COVER VARCHAR(200),
DESCRIPTION VARCHAR(2000)
)""",
"""CREATE TABLE BOOKWORM(
ID SERIAL PRIMARY KEY,
USERNAME VARCHAR(20) UNIQUE NOT NULL,
PASSWORD VARCHAR(100) NOT NULL,
EMAIL VARCHAR(50) UNIQUE NOT NULL,
PROFILEPICTURE VARCHAR(30),
GENDER VARCHAR(10)
)""",
"""CREATE TABLE REVIEW(
ID SERIAL PRIMARY KEY,
SCORE INTEGER NOT NULL CHECK(SCORE>=1 AND SCORE<=10),
COMMENT VARCHAR(2000),
DATEWRITTEN TIMESTAMP,
BOOKID INTEGER REFERENCES BOOK ON DELETE CASCADE,
USERID INTEGER REFERENCES BOOKWORM ON DELETE CASCADE
)""",
"""CREATE TABLE ADMINS(
ADMINID INTEGER UNIQUE REFERENCES BOOKWORM ON DELETE CASCADE
)""",
"""CREATE TABLE GENRES(
BOOKID INTEGER REFERENCES BOOK ON DELETE CASCADE,
GENRE VARCHAR(20)
)"""
]
def initialize(url):
with dbapi2.connect(url) as connection:
cursor = connection.cursor()
for statement in INIT_STATEMENTS:
cursor.execute(statement)
cursor.close()
if __name__ == "__main__":
url = os.getenv("DATABASE_URL")
if url is None:
print("Usage: DATABASE_URL=url python dbinit.py", file=sys.stderr)
sys.exit(1)
initialize(url)