from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.pool import StaticPool
from app.core.config import settings
class Base(DeclarativeBase): pass
kw = {"pool_pre_ping": True}
if settings.database_url.startswith("sqlite"):
    kw.update(poolclass=StaticPool, connect_args={"check_same_thread": False})
engine = create_async_engine(settings.database_url, **kw)
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)
async def get_db():
    async with AsyncSessionLocal() as session:
        try: yield session
        except Exception:
            await session.rollback()
            raise
