refactor: replace exec_dml with CRUD operations in various files and improve session handling

This commit is contained in:
Abhimanyu Saharan
2026-02-09 02:17:34 +05:30
parent 228b99bc9b
commit fafcac1e16
12 changed files with 392 additions and 156 deletions

View File

@@ -11,9 +11,12 @@ from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async
from sqlmodel import SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from app import models # noqa: F401
from app import models as _models
from app.core.config import settings
# Import model modules so SQLModel metadata is fully registered at startup.
_MODEL_REGISTRY = _models
def _normalize_database_url(database_url: str) -> str:
if "://" not in database_url:
@@ -64,4 +67,11 @@ async def init_db() -> None:
async def get_session() -> AsyncGenerator[AsyncSession, None]:
async with async_session_maker() as session:
yield session
try:
yield session
except Exception:
try:
await session.rollback()
except Exception:
logger.exception("Failed to rollback session after request error.")
raise