Fix get_optional_database_session to handle uninitialized database
- Moved RuntimeError catch to encompass get_db_session() call - Previously only caught during import, not during execution - Now properly yields None when database not initialized - Fixes test_add_series_endpoint_authenticated test failure
This commit is contained in:
@@ -165,21 +165,21 @@ async def get_optional_database_session() -> AsyncGenerator:
|
||||
"""
|
||||
try:
|
||||
from src.server.database import get_db_session
|
||||
|
||||
# Try to get a session - if database not initialized, this will raise RuntimeError
|
||||
async with get_db_session() as session:
|
||||
try:
|
||||
yield session
|
||||
# Auto-commit on successful completion
|
||||
await session.commit()
|
||||
except Exception:
|
||||
# Auto-rollback on error
|
||||
await session.rollback()
|
||||
raise
|
||||
except (ImportError, RuntimeError):
|
||||
# Database not available - yield None
|
||||
# Database not available or not initialized - yield None
|
||||
yield None
|
||||
return
|
||||
|
||||
# Use get_db_session context manager properly
|
||||
async with get_db_session() as session:
|
||||
try:
|
||||
yield session
|
||||
# Auto-commit on successful completion
|
||||
await session.commit()
|
||||
except Exception:
|
||||
# Auto-rollback on error
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
|
||||
def get_current_user(
|
||||
|
||||
Reference in New Issue
Block a user