2026-01-06 17:22:39 +00:00
|
|
|
"""
|
|
|
|
|
Database connection setup using SQLAlchemy.
|
2026-03-27 09:29:27 +00:00
|
|
|
The schema is managed by dbt — the backend only reads from marts.* tables.
|
2026-01-06 17:22:39 +00:00
|
|
|
"""
|
|
|
|
|
|
2026-03-27 09:29:27 +00:00
|
|
|
from contextlib import contextmanager
|
2026-01-16 10:23:02 +00:00
|
|
|
|
2026-03-27 09:29:27 +00:00
|
|
|
from sqlalchemy import create_engine
|
2026-01-06 17:22:39 +00:00
|
|
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
|
|
|
|
|
|
|
|
|
from .config import settings
|
|
|
|
|
|
|
|
|
|
engine = create_engine(
|
|
|
|
|
settings.database_url,
|
|
|
|
|
pool_size=10,
|
|
|
|
|
max_overflow=20,
|
2026-03-27 09:29:27 +00:00
|
|
|
pool_pre_ping=True,
|
2026-04-15 22:45:46 +01:00
|
|
|
pool_recycle=1800, # recycle connections every 30 min to avoid stale TCP
|
2026-03-27 09:29:27 +00:00
|
|
|
echo=False,
|
2026-01-06 17:22:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_db():
|
2026-03-27 09:29:27 +00:00
|
|
|
"""Dependency for FastAPI routes."""
|
2026-01-06 17:22:39 +00:00
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
|
def get_db_session():
|
2026-04-15 22:45:46 +01:00
|
|
|
"""Context manager for non-FastAPI contexts (read-only)."""
|
2026-01-06 17:22:39 +00:00
|
|
|
db = SessionLocal()
|
|
|
|
|
try:
|
|
|
|
|
yield db
|
|
|
|
|
finally:
|
|
|
|
|
db.close()
|