Files
DinQuant/backend_api_python/app/utils/db.py
T

66 lines
1.5 KiB
Python
Raw Normal View History

2025-12-29 03:06:49 +08:00
"""
Database Connection Utility - PostgreSQL Only
2025-12-29 03:06:49 +08:00
Provides unified interface for PostgreSQL database operations.
2025-12-29 03:06:49 +08:00
Usage:
from app.utils.db import get_db_connection
with get_db_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
row = cursor.fetchone()
conn.commit()
Configuration:
DATABASE_URL=postgresql://user:password@host:port/dbname
"""
2025-12-29 03:06:49 +08:00
# Re-export from PostgreSQL module
from app.utils.db_postgres import (
get_pg_connection as get_db_connection,
get_pg_connection_sync as get_db_connection_sync,
is_postgres_available,
close_pool as close_db,
)
2025-12-29 03:06:49 +08:00
def get_db_type() -> str:
"""Get database type (always postgresql)"""
return 'postgresql'
2025-12-29 03:06:49 +08:00
def is_postgres() -> bool:
"""Check if using PostgreSQL (always True)"""
return True
2025-12-29 03:06:49 +08:00
def init_database():
2025-12-29 03:06:49 +08:00
"""
Initialize database connection.
Schema is created via migrations/init.sql on PostgreSQL container start.
2025-12-29 03:06:49 +08:00
"""
if is_postgres_available():
from app.utils.logger import get_logger
logger = get_logger(__name__)
logger.info("PostgreSQL connection verified")
else:
raise RuntimeError("Cannot connect to PostgreSQL. Check DATABASE_URL.")
2025-12-29 03:06:49 +08:00
# Legacy alias
2025-12-29 03:06:49 +08:00
def close_db_connection():
"""Legacy alias for close_db"""
2025-12-29 03:06:49 +08:00
pass
__all__ = [
'get_db_connection',
'get_db_connection_sync',
'close_db_connection',
'init_database',
'close_db',
'get_db_type',
'is_postgres',
]