-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_utils.py
More file actions
63 lines (54 loc) · 2.18 KB
/
Copy pathdatabase_utils.py
File metadata and controls
63 lines (54 loc) · 2.18 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
import sqlalchemy
import yaml
class DatabaseConnector:
"""
A utility class to connect to different databases (AWS RDS or local).
"""
def __init__(self, creds_file):
"""
Initializes the DatabaseConnector with credentials.
Args:
creds_file (str): Path to the YAML file containing database credentials.
"""
self.creds = self.read_db_creds(creds_file)
def read_db_creds(self, creds_file):
"""
Reads the database credentials from a YAML file.
Args:
creds_file (str): Path to the YAML file.
Returns:
dict: Database credentials.
"""
with open(creds_file, "r") as file:
return yaml.safe_load(file)
def init_db_engine(self, source="aws"):
"""Initializes and returns a SQLAlchemy database engine."""
try:
if source == "aws":
connection_string = (
f"postgresql://{self.creds['RDS_USER']}:{self.creds['RDS_PASSWORD']}@"
f"{self.creds['RDS_HOST']}:{self.creds['RDS_PORT']}/{self.creds['RDS_DATABASE']}"
)
elif source == "local":
connection_string = (
f"postgresql://{self.creds['RDS_USER']}:{self.creds['RDS_PASSWORD']}@"
f"{self.creds['RDS_HOST']}:{self.creds['RDS_PORT']}/{self.creds['RDS_DATABASE']}"
)
return sqlalchemy.create_engine(connection_string)
except Exception as e:
print(f"Error initializing database engine: {e}")
return None
def list_db_tables(self):
"""
Lists all tables in the AWS RDS database.
"""
engine = self.init_db_engine(source="aws")
with engine.connect() as connection:
return sqlalchemy.inspect(engine).get_table_names()
def upload_to_db(self, df, table_name, dtype=None):
"""
Uploads a DataFrame to the local PostgreSQL database.
"""
engine = self.init_db_engine(source="local")
df.to_sql(table_name, engine, if_exists="replace", index=False, dtype=dtype)
print(f"Uploaded {len(df)} rows to {table_name}.")