import os from pathlib import Path from typing import Annotated from fastapi import Depends from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session, sessionmaker Base = declarative_base() # Database configuration # Use the preprocessor's database for consistency # Get absolute path to the preprocessor database APP_DIR = Path(__file__).parent.parent PREPROCESSOR_DB = APP_DIR.parent / "preprocessor" / "version_two.db" DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite:///{PREPROCESSOR_DB}") # Create engine engine = create_engine(DATABASE_URL, echo=False) # Create session factory SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def get_db(): db = SessionLocal() try: yield db finally: db.close() db_dependency = Annotated[Session, Depends(get_db)] def init_database(): """Initialize the database by creating all tables""" Base.metadata.create_all(bind=engine) def get_session_sync() -> Session: """Get a database session for synchronous operations""" return SessionLocal() def get_db_session(): """Get a database session for direct use.""" return SessionLocal()