-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_tables.py
76 lines (58 loc) · 2.05 KB
/
create_tables.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
from sql_queries import drop_table_queries, create_table_queries
import psycopg2
def create_database():
'''
- Creates and connects to sparkifydb
- Returns a cursor and connection objects
'''
# Connect to postgres database and setup a cursor plus autocommit
conn = psycopg2.connect(dbname='postgres',
user='admin',
password='admin',
host='127.0.0.1')
conn.set_session(autocommit=True)
cur = conn.cursor()
# Drop sparkifydb if exists and create it anew
cur.execute("DROP DATABASE IF EXISTS sparkifydb")
# template0 is a pristine template with no encoding involved
# it allows to specify a chosen encoding
cur.execute("CREATE DATABASE sparkifydb WITH ENCODING 'utf8' TEMPLATE template0")
# Close connection to postgres database
cur.close()
# Open connection to sparkifydb
conn = psycopg2.connect(dbname='sparkifydb',
user='admin',
password='admin',
host='127.0.0.1')
conn.set_session(autocommit=True)
cur = conn.cursor()
return conn, cur
def drop_tables(cur, conn):
"""
Drops each table using the queries in `drop_table_queries` list from sql_queries.py
"""
for query in drop_table_queries:
cur.execute(query)
conn.commit()
def create_tables(cur, conn):
"""
Creates each table using the queries in `create_table_queries` list sql_queries.py.
"""
for query in create_table_queries:
cur.execute(query)
conn.commit()
def main():
"""
- Drops (if exists) and Creates the sparkifydb
- Establishes connection with the sparkifydb and gets
cursor to it.
- Drops all the tables.
- Creates all tables needed.
- Finally, closes the connection.
"""
cur, conn = create_database()
drop_tables(conn, cur)
create_tables(conn, cur)
conn.close()
if __name__ == "__main__":
main()