create .env and update db_connection to use it

This commit is contained in:
Bobby Abellana 2025-02-03 20:20:19 -08:00
parent bbc307f3d8
commit ec31b8701d
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -1,18 +1,26 @@
### `src/db/db_connection.py` (ClickHouse example)
```python
import logging
import clickhouse_connect
import uuid
import os
import time
import uuid
def create_client(clickhouse_password):
import clickhouse_connect
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def create_client():
"""
Create a ClickHouse client with a unique session and retry logic.
Create a ClickHouse client with a unique session and retry logic,
using the password loaded from environment variables.
"""
clickhouse_password = os.getenv("CLICKHOUSE_PASSWORD")
if not clickhouse_password:
raise ValueError("CLICKHOUSE_PASSWORD environment variable not set.")
max_retries = 3
retry_delay = 2 # seconds
for attempt in range(max_retries):
try:
client = clickhouse_connect.get_client(
@ -24,7 +32,7 @@ def create_client(clickhouse_password):
session_id=str(uuid.uuid4()),
settings={'session_timeout': 60}
)
# Test the connection
# Test the connection.
client.query('SELECT 1')
return client
except Exception as e: