-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredshift.py
61 lines (49 loc) · 1.78 KB
/
redshift.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
import configparser
import logging
from sql_queries import create_staging_table_queries, drop_staging_table_queries, \
create_analytical_table_queries, drop_analytical_table_queries
logging.basicConfig(level=logging.INFO)
config = configparser.ConfigParser()
config.read_file(open('dwh.cfg'))
class RedshiftManager():
"""
Helper class for creating and dropping Redshift tables.
...
Attributes
----------
conn : psycopg2 connection
an object representing the connection to the database
cur : psycopg2 cursor
an object representing the database cursor
"""
def __init__(self, conn, cur):
self.conn = conn
self.cur = cur
def drop_staging_tables(self):
"""Drops all staging tables."""
logging.info("Dropping staging tables...")
for query in drop_staging_table_queries:
self.cur.execute(query)
self.conn.commit()
def drop_analytical_tables(self):
"""Drops all analytical tables."""
logging.info("Dropping analytical tables...")
for query in drop_analytical_table_queries:
self.cur.execute(query)
self.conn.commit()
def create_staging_tables(self):
"""Creates all staging tables."""
logging.info("Creating staging tables...")
for query in create_staging_table_queries:
self.cur.execute(query)
self.conn.commit()
def create_analytical_tables(self):
"""Drops all staging tables."""
logging.info("Creating analytical tables...")
for query in create_analytical_table_queries:
self.cur.execute(query)
self.conn.commit()
def main():
redshift = RedshiftManager()
if __name__ == "__main__":
main()