from contextlib import asynccontextmanager
from typing import Annotated

from fastapi import Depends
from sqlalchemy.ext.asyncio import AsyncSession

{% if has_redis %}
from {{ name }}.db.sql.utils.session import s
{% else %}
from {{ name }}.db.utils.session import s
{% endif %}


@asynccontextmanager
async def get_session():
    async with s.maker() as session:
        s.session = session
        try:
            yield session
            await session.commit()
        except Exception:
            await session.rollback()
            raise


SessionDep = Annotated[AsyncSession, Depends(get_session)]
