Skip to content

Commit f862fe9

Browse files
Merge pull request #101 from socraticDevBlog/20250427-refactordbconnectiondetails
Refactor database connection handling by centralizing configuration i…
2 parents e873085 + ae8c6e3 commit f862fe9

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

backend/app/db_configs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from dotenv import load_dotenv
2+
import os
3+
4+
5+
def get_db_connection_details():
6+
"""
7+
Load and return database connection details from environment variables.
8+
"""
9+
load_dotenv()
10+
11+
return {
12+
"host": os.getenv("DB_HOST", "localhost"),
13+
"port": os.getenv("DB_PORT", "5432"),
14+
"user": os.getenv("DB_USER"),
15+
"password": os.getenv("DB_PASSWORD"),
16+
"database": os.getenv("DB_NAME"),
17+
"schema_name": os.getenv("DB_SCHEMA", "public"),
18+
}

backend/app/services/db_init.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
from dotenv import load_dotenv
2-
import os
31
from app.scripts.postgresql import create_schema_if_not_exists, create_paste_table
2+
from app.db_configs import get_db_connection_details
43

54

65
async def initialize_database():
76
"""
87
Initialize the database by creating the schema and table if they don't exist.
98
"""
10-
load_dotenv()
11-
conn_details = {
12-
"host": os.getenv("DB_HOST", "localhost"),
13-
"port": os.getenv("DB_PORT", "5432"),
14-
"user": os.getenv("DB_USER"),
15-
"password": os.getenv("DB_PASSWORD"),
16-
"database": os.getenv("DB_NAME"),
17-
}
18-
schema_name = os.getenv("DB_SCHEMA", "public")
9+
conn_details = get_db_connection_details()
10+
schema_name = conn_details.pop("schema_name")
1911

2012
# Ensure schema and table exist
2113
await create_schema_if_not_exists(
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
from dotenv import load_dotenv
21
from app.services.db_client import DBClient
3-
import os
2+
from app.db_configs import get_db_connection_details
43

54

65
def get_db_client():
76
"""
87
Dependency to provide a DBClient instance.
98
"""
10-
11-
load_dotenv()
12-
13-
conn_details = {
14-
"host": os.getenv("DB_HOST", "localhost"),
15-
"port": os.getenv("DB_PORT", "5432"),
16-
"user": os.getenv("DB_USER"),
17-
"password": os.getenv("DB_PASSWORD"),
18-
"database": os.getenv("DB_NAME"),
19-
}
20-
schema_name = os.getenv("DB_SCHEMA", "public")
9+
conn_details = get_db_connection_details()
10+
schema_name = conn_details.pop("schema_name")
2111
return DBClient(conn_details, schema_name)

backend/app/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
DEFAULT_ENCODING = "utf-8"
21
import base64
32

43
DEFAULT_ENCODING = "utf-8"
@@ -15,7 +14,6 @@ def is_base64(s: str, encoding: str = DEFAULT_ENCODING) -> bool:
1514
bool: True if the string is valid Base64, False otherwise.
1615
"""
1716
try:
18-
# Decode and re-encode to verify Base64 validity
1917
return base64.b64encode(base64.b64decode(s)).decode(encoding=encoding) == s
2018
except Exception:
2119
return False

0 commit comments

Comments
 (0)