refactor: replace exec_dml with CRUD operations in various files and improve session handling
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user