-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_db_script.py
More file actions
139 lines (125 loc) · 4.74 KB
/
Copy pathcreate_db_script.py
File metadata and controls
139 lines (125 loc) · 4.74 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import mysql.connector
from mysql.connector import errorcode
# DATABASE CONFIGURATION
DB_CONFIG = {
'user': '****',
'password': '****',
'host': '****',
'database': '****',
'port': ****,
'raise_on_warnings': True,
'use_pure': True,
'connection_timeout': 10
}
def create_database(config):
conn = None
cursor = None
try:
conn = mysql.connector.connect(**config)
cursor = conn.cursor()
# Drop existing tables
# Drop tables with Foreign Keys first
tables_drop = [
"Movie_Language", "Movie_Country", "Staff_Movie",
"Language", "Country", "Person", "Movie"
]
# Disable FK checks to allow dropping tables in any order
cursor.execute("SET FOREIGN_KEY_CHECKS = 0;")
for table in tables_drop:
try:
cursor.execute(f"DROP TABLE IF EXISTS `{table}`;")
print(f"Table {table} dropped.")
except mysql.connector.Error as err:
# Ignore specific "Unknown table" errors if they somehow bypass IF EXISTS
if err.errno == 1051:
print(f"Table {table} did not exist (skipped).")
else:
print(f"Warning dropping {table}: {err}")
cursor.execute("SET FOREIGN_KEY_CHECKS = 1;")
# Define Tables
tables = {}
tables['Movie'] = (
"CREATE TABLE `Movie` ("
" `movie_id` INT NOT NULL AUTO_INCREMENT,"
" `title` VARCHAR(255),"
" `average_rating` FLOAT,"
" `release_date` DATE,"
" `budget` BIGINT,"
" `revenue` BIGINT,"
" `runtime` FLOAT,"
" `meta_score` INT,"
" PRIMARY KEY (`movie_id`)"
");"
)
tables['Person'] = (
"CREATE TABLE `Person` ("
" `name` VARCHAR(255) NOT NULL,"
" PRIMARY KEY (`name`)"
")"
)
tables['Staff_Movie'] = (
"CREATE TABLE `Staff_Movie` ("
" `person_name` VARCHAR(255) NOT NULL,"
" `movie_id` INT NOT NULL,"
" `role` ENUM('actor', 'writer', 'director'),"
" FOREIGN KEY (`person_name`) REFERENCES `Person`(`name`),"
" FOREIGN KEY (`movie_id`) REFERENCES `Movie`(`movie_id`),"
" PRIMARY KEY (`person_name`, `movie_id`, `role`)"
");"
)
tables['Country'] = (
"CREATE TABLE `Country` ("
" `country_name` VARCHAR(255) NOT NULL,"
" PRIMARY KEY (`country_name`)"
")"
)
tables['Movie_Country'] = (
"CREATE TABLE `Movie_Country` ("
" `movie_id` INT NOT NULL,"
" `country_name` VARCHAR(255) NOT NULL,"
" FOREIGN KEY (`movie_id`) REFERENCES `Movie`(`movie_id`),"
" FOREIGN KEY (`country_name`) REFERENCES `Country`(`country_name`)"
")"
)
tables['Language'] = (
"CREATE TABLE `Language` ("
" `language_name` VARCHAR(255) NOT NULL,"
" PRIMARY KEY (`language_name`)"
")"
)
tables['Movie_Language'] = (
"CREATE TABLE `Movie_Language` ("
" `movie_id` INT NOT NULL,"
" `language_name` VARCHAR(255) NOT NULL,"
" FOREIGN KEY (`movie_id`) REFERENCES `Movie`(`movie_id`),"
" FOREIGN KEY (`language_name`) REFERENCES `Language`(`language_name`)"
")"
)
# create Tables
for table_name, table_description in tables.items():
try:
print(f"Creating table {table_name}: ", end="")
cursor.execute(table_description)
print("OK")
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("already exists.")
else:
print(err.msg)
# Create Full-Text Indices
print("Creating Full-Text Indices...")
try:
cursor.execute("CREATE FULLTEXT INDEX idx_movie_title ON Movie(title);")
cursor.execute("CREATE FULLTEXT INDEX idx_person_name ON Staff_Movie(person_name);")
print("Indices created.")
except mysql.connector.Error as err:
print(f"Index warning: {err.msg}")
conn.commit()
print("Database schema created successfully.")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
if cursor: cursor.close()
if conn: conn.close()
if __name__ == "__main__":
create_database(DB_CONFIG)