Ушел от try except и вынес rollback на уровень сессии

This commit is contained in:
Raykov-MS 2026-05-21 11:20:44 +03:00
parent 411773b6e9
commit e129f562dc
2 changed files with 28 additions and 40 deletions

View File

@ -15,6 +15,9 @@ async def get_db() -> AsyncGenerator:
try: try:
yield db yield db
await db.commit() await db.commit()
except Exception:
await db.rollback()
raise
finally: finally:
await db.close() await db.close()

View File

@ -105,8 +105,7 @@ class UserRepository:
query = select(UserOrg.org_unit_id).where(UserOrg.user_id == user_id) query = select(UserOrg.org_unit_id).where(UserOrg.user_id == user_id)
return (await self.db.execute(query)).scalars().all() return (await self.db.execute(query)).scalars().all()
async def set_many_ssp(self, user_id: int, ssp_ids: list[int]) -> bool: async def set_many_ssp(self, user_id: int, ssp_ids: list[int]) -> None:
try:
for ssp_id in ssp_ids: for ssp_id in ssp_ids:
await self.db.execute( await self.db.execute(
text( text(
@ -115,13 +114,8 @@ class UserRepository:
{"user_id": user_id, "org_unit_id": ssp_id}, {"user_id": user_id, "org_unit_id": ssp_id},
) )
await self.db.commit() await self.db.commit()
return True
except Exception:
await self.db.rollback()
return False
async def unset_many_ssp(self, user_id: int, ssp_ids: list[int]) -> bool: async def unset_many_ssp(self, user_id: int, ssp_ids: list[int]) -> None:
try:
for ssp_id in ssp_ids: for ssp_id in ssp_ids:
await self.db.execute( await self.db.execute(
text( text(
@ -130,13 +124,8 @@ class UserRepository:
{"user_id": user_id, "org_unit_id": ssp_id}, {"user_id": user_id, "org_unit_id": ssp_id},
) )
await self.db.commit() await self.db.commit()
return True
except Exception:
await self.db.rollback()
return False
async def clear_many_ssp(self, user_id: int) -> bool: async def clear_many_ssp(self, user_id: int) -> None:
try:
org_unit_ids = await self.get_many_ssp_ids(user_id) org_unit_ids = await self.get_many_ssp_ids(user_id)
await self.db.execute( await self.db.execute(
text( text(
@ -145,10 +134,6 @@ class UserRepository:
{"user_id": user_id, "org_unit_ids": org_unit_ids}, {"user_id": user_id, "org_unit_ids": org_unit_ids},
) )
await self.db.commit() await self.db.commit()
return True
except Exception:
await self.db.rollback()
return False
async def create(self, user_data: dict) -> AppUser: async def create(self, user_data: dict) -> AppUser:
hashed_password = ( hashed_password = (