Fix user delete flow 12

This commit is contained in:
Urtzi Alfaro
2025-08-03 14:42:33 +02:00
parent b35eb7c875
commit b0d83720fd
7 changed files with 341 additions and 54 deletions

View File

@@ -7,6 +7,7 @@ from sqlalchemy import create_engine
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.pool import StaticPool
from contextlib import asynccontextmanager
import logging
logger = logging.getLogger(__name__)
@@ -34,7 +35,7 @@ class DatabaseManager:
)
async def get_db(self):
"""Get database session"""
"""Get database session for request handlers"""
async with self.async_session_local() as session:
try:
yield session
@@ -45,6 +46,27 @@ class DatabaseManager:
finally:
await session.close()
@asynccontextmanager
async def get_background_session(self):
"""
✅ NEW: Get database session for background tasks
Usage:
async with database_manager.get_background_session() as session:
# Your background task code here
await session.commit()
"""
async with self.async_session_local() as session:
try:
yield session
await session.commit()
except Exception as e:
await session.rollback()
logger.error(f"Background task database error: {e}")
raise
finally:
await session.close()
async def create_tables(self):
"""Create database tables"""
async with self.async_engine.begin() as conn: