from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import DeclarativeBase from .config import settings engine = create_async_engine( settings.database_url, echo=False, pool_size=20, max_overflow=10, pool_pre_ping=True, ) AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession) class Base(DeclarativeBase): pass async def get_db(): async with AsyncSessionLocal() as session: try: yield session finally: await session.close() async def init_db(): from .models import provider, channel, user, epg, custom_category, log, notification, jellyfin # noqa: ensure all models imported async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all)