Spaces:
Sleeping
Sleeping
Download app/database.py from AswinMathew/anime-gen-api: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/AswinMathew/anime-gen-api/resolve/main/app/database.py
- Command line
-
hf download hf://spaces/AswinMathew/anime-gen-api/app/database.py
-
curl -L -o database.py https://huggingface.co/spaces/AswinMathew/anime-gen-api/resolve/main/app/database.py
1.45 kB
| 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 | |