Spaces:
Sleeping
Sleeping
| from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession | |
| from sqlalchemy.orm import DeclarativeBase | |
| from app.config import get_settings | |
| settings = get_settings() | |
| engine = create_async_engine( | |
| settings.database_url, | |
| echo=False, | |
| connect_args={"timeout": 30}, | |
| ) | |
| async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) | |
| class Base(DeclarativeBase): | |
| pass | |
| async def get_db(): | |
| async with async_session() as session: | |
| try: | |
| yield session | |
| finally: | |
| await session.close() | |
| async def init_db(): | |
| async with engine.begin() as conn: | |
| await conn.run_sync(Base.metadata.create_all) | |
| # Migrate existing DBs: add chapter_ids_json to generation_jobs | |
| async with engine.begin() as conn: | |
| try: | |
| await conn.execute( | |
| __import__("sqlalchemy").text( | |
| "ALTER TABLE generation_jobs ADD COLUMN chapter_ids_json TEXT" | |
| ) | |
| ) | |
| except Exception: | |
| pass # Column already exists | |
| # Migrate existing DBs: add recap_summary to episodes | |
| async with engine.begin() as conn: | |
| try: | |
| await conn.execute( | |
| __import__("sqlalchemy").text( | |
| "ALTER TABLE episodes ADD COLUMN recap_summary TEXT" | |
| ) | |
| ) | |
| except Exception: | |
| pass # Column already exists | |