Spaces:
Sleeping
Sleeping
File size: 1,450 Bytes
7190fd0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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
|