Spaces:
Running
Running
| """ | |
| Database connection and session management. | |
| Uses SQLAlchemy 2.0 async-compatible patterns. | |
| """ | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, declarative_base | |
| from app.config import get_settings | |
| settings = get_settings() | |
| # Create engine - using psycopg3 driver | |
| # Convert postgresql:// to postgresql+psycopg:// for psycopg3 | |
| db_url = settings.database_url | |
| if db_url.startswith("postgresql://"): | |
| db_url = db_url.replace("postgresql://", "postgresql+psycopg://", 1) | |
| engine = create_engine( | |
| db_url, | |
| pool_pre_ping=True, # Verify connections before using | |
| pool_size=5, | |
| max_overflow=10 | |
| ) | |
| # Session factory | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Base class for all models | |
| Base = declarative_base() | |
| def get_db(): | |
| """ | |
| Dependency that provides a database session. | |
| Ensures proper cleanup after request completes. | |
| """ | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |