fix: address review feedback on session handler and counter

- Restore NoResultFound behavior on update_session, complete_session,
  fail_session when row is missing (scalar returns None, old .one()
  raised -- silent None is a semantic regression)
- Remove redundant get_session call from _increment_session_counter;
  the atomic SQL increment is already a no-op on missing rows
- Log warning when passed session_id is not found in _sync_device
  instead of silently creating an orphan session
This commit is contained in:
nendo
2026-03-16 10:59:49 +09:00
parent 55638d15dc
commit 4edb1710a5
3 changed files with 21 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ from collections.abc import Sequence
from datetime import datetime, timezone
from sqlalchemy import select, update
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import Session
from decorators.database import begin_session
@@ -75,7 +76,10 @@ class DBSyncSessionsHandler(DBBaseHandler):
.values(**data)
.execution_options(synchronize_session="evaluate")
)
return session.scalar(select(SyncSession).filter_by(id=session_id))
result = session.scalar(select(SyncSession).filter_by(id=session_id))
if not result:
raise NoResultFound(f"SyncSession {session_id} not found after update")
return result
@begin_session
def increment_operations_completed(
@@ -111,7 +115,10 @@ class DBSyncSessionsHandler(DBBaseHandler):
)
.execution_options(synchronize_session="evaluate")
)
return session.scalar(select(SyncSession).filter_by(id=session_id))
result = session.scalar(select(SyncSession).filter_by(id=session_id))
if not result:
raise NoResultFound(f"SyncSession {session_id} not found after complete")
return result
@begin_session
def fail_session(
@@ -130,7 +137,10 @@ class DBSyncSessionsHandler(DBBaseHandler):
)
.execution_options(synchronize_session="evaluate")
)
return session.scalar(select(SyncSession).filter_by(id=session_id))
result = session.scalar(select(SyncSession).filter_by(id=session_id))
if not result:
raise NoResultFound(f"SyncSession {session_id} not found after fail")
return result
@begin_session
def cancel_active_sessions(