""" SQLAlchemy database setup with SQLite. Uses synchronous SQLAlchemy for simplicity and compatibility. """ import os from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, declarative_base from app.config import get_settings settings = get_settings() # ── Ensure data directory exists ───────────────────── db_path = settings.DATABASE_URL.replace("sqlite:///", "") os.makedirs(os.path.dirname(db_path) if os.path.dirname(db_path) else ".", exist_ok=True) # ── Engine & Session ───────────────────────────────── engine = create_engine( settings.DATABASE_URL, connect_args={"check_same_thread": False}, # Required for SQLite echo=settings.DEBUG, ) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() def get_db(): """FastAPI dependency — yields a DB session per request.""" db = SessionLocal() try: yield db finally: db.close() def init_db(): """Create all tables on startup.""" from app import models # noqa: F401 — import to register models Base.metadata.create_all(bind=engine)