-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload_staging_db_script.py
114 lines (94 loc) · 3.18 KB
/
load_staging_db_script.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
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
# %%
# importing libraries
import os
import re
import psycopg2
import psycopg2.extras as extras
from psycopg2 import Error
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
# %% [markdown]
# ### Populate Tables
# %%
# set up connection variables
db_host = "localhost"
db_port = "5432"
db_user = "postgres"
db_pass = "abcd"
db_name = "tpc_di"
# function to connect with postgres
def connect_postgres(db_host, db_port, db_user, db_pass, db_name):
try:
# Connect to an existing database
connection = psycopg2.connect(host = db_host,
port = db_port,
user = db_user,
password = db_pass,
database = db_name)
# Set auto-commit
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
# Create a cursor to perform database operations
cur = connection.cursor()
# Print PostgreSQL details
print("PostgreSQL server information")
print(connection.get_dsn_parameters(), "\n")
# Executing a SQL query
cur.execute("SELECT version();")
# Fetch result
record = cur.fetchone()
print("You are connected to - ", record, "\n")
except (Exception, Error) as error:
print("Error while connecting to PostgreSQL", error)
else:
return cur
# %%
# connect to postgres
cur = connect_postgres(db_host, db_port, db_user, db_pass, db_name)
# %%
# drop tables if exists in db
cur.execute(
"""
TRUNCATE TABLE staging.audit;
TRUNCATE TABLE staging.batchdate;
TRUNCATE TABLE staging.cashtransaction;
TRUNCATE TABLE staging.customermgmt;
TRUNCATE TABLE staging.dailymarket;
TRUNCATE TABLE staging.date;
TRUNCATE TABLE staging.finwire;
TRUNCATE TABLE staging.finwire_cmp;
TRUNCATE TABLE staging.finwire_fin;
TRUNCATE TABLE staging.finwire_sec;
TRUNCATE TABLE staging.holdinghistory;
TRUNCATE TABLE staging.hr;
TRUNCATE TABLE staging.industry;
TRUNCATE TABLE staging.prospect;
TRUNCATE TABLE staging.statustype;
TRUNCATE TABLE staging.taxrate;
TRUNCATE TABLE staging.time;
TRUNCATE TABLE staging.trade;
TRUNCATE TABLE staging.tradehistory;
TRUNCATE TABLE staging.tradetype;
TRUNCATE TABLE staging.watchhistory;
"""
)
print("SQL Status Output:\n", cur.statusmessage)
# %%
cur.execute(open("staging_data_commands.sql", "r").read())
print("Staging DB tables populated")
# %%
sql_commands_file = open('staging_finwire_load1.sql','w')
path = os.getcwd() + '\\sf_3\\output_data\\Batch1'
files = [f for f in os.listdir(path) if not f.startswith('.')]
for file in files:
abs_path = path + '/'+ file
if (re.search('^FINWIRE', file)) and (re.search('audit.csv$', file) == None):
sql_command = "COPY staging.finwire"+" FROM '"+abs_path+"';\n"
sql_commands_file.write(sql_command)
sql_commands_file.close()
# %%
cur.execute(open("staging_finwire_load1.sql", "r").read())
print("Finwire DB tables populated")
# %%
cur.execute(open("load_staging_finwire_db.sql", "r").read())
print("Finwire CMP, SEC and FIN Staging DB tables populated")
# %%
cur.close()