Uprava prace s DB, refaktoring
This commit is contained in:
+2
-14
@@ -1,8 +1,4 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database import async_session
|
||||
|
||||
|
||||
@@ -13,18 +9,10 @@ async def get_session():
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except:
|
||||
except Exception as e:
|
||||
await session.rollback()
|
||||
print(f"ERROR: {e}")
|
||||
raise
|
||||
finally:
|
||||
session.expunge_all()
|
||||
await session.close()
|
||||
|
||||
|
||||
async def db_session():
|
||||
async with async_session() as session:
|
||||
async with session.begin():
|
||||
yield session
|
||||
await session.flush()
|
||||
await session.commit()
|
||||
print("CCCCCCCCCCCCCOOOOOOOOOOOOOOMMMMMMMMMMIIIIIIIIIIIITTTTTTTTTTTT")
|
||||
|
||||
Reference in New Issue
Block a user