-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_tables.py
More file actions
101 lines (76 loc) · 2.93 KB
/
create_tables.py
File metadata and controls
101 lines (76 loc) · 2.93 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
import configparser
import logging
import psycopg2
from sql_queries import (create_schema_queries, create_table_queries,
drop_schema_queries, drop_table_queries)
from utilityFunctions import execute_queries
# setting up logging
logging.basicConfig(level=logging.INFO)
def drop_tables(cur, conn):
"""
Executes the drop tables queries and drops
the tables passed in drop_table_queries
"""
logging.info("Running drop table queries")
execute_queries(cur, conn, drop_table_queries)
def drop_schema(cur, conn):
"""
Executes the drop schema queruies and drops
the schemas passed in drop_schema_queries
"""
logging.info("Running drop schema queries")
execute_queries(cur, conn, drop_schema_queries)
def create_schema(cur, conn):
"""
Executes the create schemas queries and creates
schemas passed in create_schema_queries
"""
logging.info("Running create schema queries")
execute_queries(cur, conn, create_schema_queries)
def create_tables(cur, conn):
"""
Executes the create tables queries and creates
tables passed in the create_table_queries
"""
logging.info("Running create table queries")
execute_queries(cur, conn, create_table_queries)
def main():
"""
Wrapper function that reads the config file, creates the connection
and executes all the queries to prepare database (schema) and tables
on the redshift cluster.
"""
config = configparser.ConfigParser()
# reading the config file
config.read('dwh.cfg')
DWH_DB=config.get("CLUSTER","DB_NAME")
DWH_DB_USER=config.get("CLUSTER","DB_USER")
DWH_DB_PASSWORD=config.get("CLUSTER","DB_PASSWORD")
DWH_PORT=config.get("CLUSTER","DB_PORT")
DWH_ENDPOINT=config.get("DWH_EXTRAS","DWH_ENDPOINT")
conn_string="postgresql://{}:{}@{}:{}/{}".format(DWH_DB_USER,
DWH_DB_PASSWORD,
DWH_ENDPOINT,
DWH_PORT,
DWH_DB)
logging.info(f"creating the connection to redshift cluster {DWH_DB}")
try:
# creating the connection
conn = psycopg2.connect(conn_string)
cur = conn.cursor()
# running the create schema in the first line, when connecting
# to the cluster first time else erroe will be raised when
# dropping tables since the database doesnt exist
logging.info("Deleting the database and tables")
create_schema(cur, conn)
drop_tables(cur, conn)
drop_schema(cur, conn)
logging.info("Creating the database and tables")
create_schema(cur, conn)
create_tables(cur, conn)
# closing the connection
conn.close()
except psycopg2.Error as err:
logging.error(err)
if __name__ == "__main__":
main()