-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_db_data.py
executable file
·44 lines (31 loc) · 1.06 KB
/
run_db_data.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
import os
from sqlalchemy.orm import Session
from db.database import SessionLocal
class DbData:
def __init__(self):
self.root_directory: str = "db_merge_scripts"
self.scripts = [
"loader.sql"
]
def sync(self, db: Session):
for script in self.scripts:
try:
directory = os.path.join(self.root_directory, script)
print(directory)
sql = open(directory, "r").read()
db.execute(sql)
db.commit()
print(greed("Data file processed: " + directory))
except Exception as e:
print(red("Error to process data file: " + directory))
print(e)
def colored(text, r, g, b):
return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text)
def red(text):
return colored(text, 255, 0, 0)
def greed(text):
return colored(text, 0, 255, 0)
def add_master_data():
db = SessionLocal()
DbData().sync(db)
db.close()