-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_utils.py
More file actions
78 lines (63 loc) · 3.1 KB
/
Copy pathdatabase_utils.py
File metadata and controls
78 lines (63 loc) · 3.1 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
import yaml
from sqlalchemy import create_engine, text
class DatabaseConnector:
"""
This class provides methods for connecting to a PostgreSQL database and performing database-related tasks.
Attributes:
db_creds (dict): A dictionary containing the database credentials.
engine (sqlalchemy.engine.base.Engine): A SQLAlchemy database engine used for database connections.
Methods:
1. read_db_creds()
- Reads the database credentials from a 'db_creds.yaml' file.
- Returns:
- dict: A dictionary containing the database credentials.
2. init_db_engine()
- Initializes a SQLAlchemy database engine using the database credentials.
- Returns:
- sqlalchemy.engine.base.Engine: A SQLAlchemy database engine.
3. list_db_tables()
- Lists all tables in the connected PostgreSQL database.
- Returns:
- list: A list of table names in the 'public' schema of the database.
4. upload_to_db(data_df, table_name)
- Uploads data from a Pandas DataFrame to a specified database table.
- Parameters:
- data_df (DataFrame): The data to be uploaded.
- table_name (str): The name of the table in the database.
5. close_connection()
- Closes the database connection if it's open.
Usage:
Example usage of this class can be found in the '__main__' block of this script.
"""
def __init__(self):
self.db_creds = self.read_db_creds()
self.engine = self.init_db_engine()
def read_db_creds(self):
try:
with open('db_creds.yaml', 'r') as yaml_file:
db_creds = yaml.safe_load(yaml_file)
return db_creds
except FileNotFoundError:
print("db_creds.yaml file not found. Make sure to create it with the correct credentials.")
return {}
def init_db_engine(self):
db_url = f"postgresql://{self.db_creds['RDS_USER']}:{self.db_creds['RDS_PASSWORD']}@{self.db_creds['RDS_HOST']}:{self.db_creds['RDS_PORT']}/{self.db_creds['RDS_DATABASE']}"
engine = create_engine(db_url)
return engine
def list_db_tables(self):
with self.engine.connect() as connection:
query = text("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'")
result = connection.execute(query)
table_names = [row[0] for row in result]
# print(table_names)
return table_names
def upload_to_db(self, data_df, table_name):
with open('db_creds_local.yaml') as db_creds_local:
creds = yaml.safe_load(db_creds_local)
engine = create_engine(f"{'postgresql'}+{'psycopg2'}://{creds['user']}:{creds['password']}@{creds['host']}:{creds['port']}/{creds['dbname']}")
engine.connect()
data_df.to_sql(table_name, engine, if_exists='replace')
def close_connection(self):
if hasattr(self, 'conn'):
self.engine.dispose()
print("Database connection closed")