-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
81 lines (71 loc) · 2.89 KB
/
database.py
File metadata and controls
81 lines (71 loc) · 2.89 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
"""
File initializes database tables given configuration parameters
Authors: Edward Mattout & Daniella Grimberg
"""
import logging
import sys
import mysql.connector
from database_utils import connect_to_database, close_database_connection, LOG_FILE_FORMAT, LOG_FILE_NAME
formatter = logging.Formatter(LOG_FILE_FORMAT)
logger = logging.getLogger('database_init')
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(LOG_FILE_NAME)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.ERROR)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
def make_tables():
"""
Function creates database tables
:return:
"""
connection, cursor = None, None
try:
connection, cursor = connect_to_database()
if not connection and cursor:
raise mysql.connector.Error("No connection")
cursor.execute("""CREATE TABLE authors (
author_id INT NOT NULL AUTO_INCREMENT,
full_name VARCHAR(45) UNIQUE,
twitter_handle VARCHAR(45),
PRIMARY KEY (author_id))""")
cursor.execute("""CREATE TABLE articles (
article_id INT NOT NULL AUTO_INCREMENT,
link VARCHAR(250) UNIQUE,
title VARCHAR(250) UNIQUE,
date DATETIME,
PRIMARY KEY (article_id))""")
cursor.execute("""CREATE TABLE tags (
tag_id INT NOT NULL AUTO_INCREMENT,
tag_text VARCHAR(45) UNIQUE,
PRIMARY KEY (tag_id))
""")
cursor.execute("""CREATE TABLE article_to_tags (
id INT NOT NULL AUTO_INCREMENT,
article_id INT,
tag_id INT,
CONSTRAINT article_id_tag FOREIGN KEY (article_id)
REFERENCES articles(article_id) ON DELETE CASCADE,
CONSTRAINT tag_id_article FOREIGN KEY (tag_id)
REFERENCES tags(tag_id) ON DELETE CASCADE,
PRIMARY KEY (id)
)""")
cursor.execute("""CREATE TABLE article_to_authors (
id INT NOT NULL AUTO_INCREMENT,
article_id INT,
author_id INT,
FOREIGN KEY (article_id) REFERENCES articles(article_id),
FOREIGN KEY (author_id) REFERENCES authors(author_id),
PRIMARY KEY (id)
)""")
logger.info("Successfully created tables in database")
except mysql.connector.Error as error:
logger.error("Failed to create table in MySQL: {}".format(error))
finally:
if connection and cursor:
close_database_connection(connection, cursor)
if __name__ == '__main__':
make_tables()