Skip to content

Commit

Permalink
feat: 트랜잭션의 범위를 넓힘
Browse files Browse the repository at this point in the history
  • Loading branch information
2jun0 committed Apr 12, 2024
1 parent ed63c52 commit abf200d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 10 deletions.
1 change: 1 addition & 0 deletions backend/src/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
async def get_session() -> AsyncGenerator[AsyncSession, Any]:
async with AsyncSession(engine, expire_on_commit=False) as session:
yield session
await session.commit()


async def es_client() -> AsyncGenerator[AsyncElasticsearch, Any]:
Expand Down
11 changes: 1 addition & 10 deletions backend/src/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Generic, Protocol, Type, TypeVar

from sqlalchemy import exc
from sqlmodel import SQLModel, exists, select
from sqlmodel.ext.asyncio.session import AsyncSession

Expand All @@ -25,13 +24,5 @@ async def exists(self: IRepository[ModelTypeS], *, id: int) -> bool:
return rs.one()

async def create(self: IRepository[ModelTypeS], *, model: ModelTypeS) -> ModelTypeS:
# TODO: try except가 필요한지 확인
try:
self._session.add(model)
await self._session.commit()
except exc.IntegrityError:
await self._session.rollback()
raise

await self._session.refresh(model)
self._session.add(model)
return model

0 comments on commit abf200d

Please sign in to comment.