Code reviewed and issue fixed.
This commit is contained in:
@@ -54,8 +54,16 @@ def log_action(user_id, action, entity_type=None, entity_id=None, details=None):
|
||||
own business objects. This ensures the log entry is only persisted when
|
||||
the parent operation succeeds — an independent commit here would leave
|
||||
orphaned log entries for operations that were subsequently rolled back.
|
||||
|
||||
Error isolation
|
||||
---------------
|
||||
On failure, only the log entry itself is expelled from the session via
|
||||
expunge(). db.session.rollback() is intentionally NOT called here because
|
||||
that would wipe the entire session — silently undoing the parent business
|
||||
operation (ticket creation, user update, etc.) that triggered this log call.
|
||||
"""
|
||||
ip = _get_real_ip()
|
||||
entry = None
|
||||
|
||||
try:
|
||||
entry = ActivityLog(
|
||||
@@ -74,8 +82,14 @@ def log_action(user_id, action, entity_type=None, entity_id=None, details=None):
|
||||
f'user_id={user_id} ip={ip}'
|
||||
)
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
logger.error(f'[ACTIVITY LOG ERROR] {exc}')
|
||||
# Expunge only the failed log entry — do NOT roll back the full session,
|
||||
# as that would undo the parent operation that called this function.
|
||||
if entry is not None:
|
||||
try:
|
||||
db.session.expunge(entry)
|
||||
except Exception:
|
||||
pass
|
||||
logger.error(f'[ACTIVITY LOG ERROR] action={action} entity={entity_type}:{entity_id} error={exc}')
|
||||
|
||||
|
||||
def log_ticket_history(ticket, field_name, old_value, new_value, changed_by_id):
|
||||
@@ -85,8 +99,15 @@ def log_ticket_history(ticket, field_name, old_value, new_value, changed_by_id):
|
||||
----------------
|
||||
Like log_action, this function does NOT commit — the caller is responsible
|
||||
for committing the session after all field changes have been recorded.
|
||||
|
||||
Error isolation
|
||||
---------------
|
||||
On failure, only the failed history entry is expelled from the session.
|
||||
db.session.rollback() is intentionally NOT called here — that would undo
|
||||
the parent ticket update that triggered this history recording.
|
||||
"""
|
||||
from app.models import TicketHistory
|
||||
entry = None
|
||||
try:
|
||||
entry = TicketHistory(
|
||||
ticket_id = ticket.id,
|
||||
@@ -102,5 +123,11 @@ def log_ticket_history(ticket, field_name, old_value, new_value, changed_by_id):
|
||||
f'"{old_value}" -> "{new_value}" by user_id={changed_by_id}'
|
||||
)
|
||||
except Exception as exc:
|
||||
db.session.rollback()
|
||||
logger.error(f'[TICKET HISTORY ERROR] {exc}')
|
||||
# Expunge only the failed history entry — do NOT roll back the full
|
||||
# session, as that would undo the parent ticket update.
|
||||
if entry is not None:
|
||||
try:
|
||||
db.session.expunge(entry)
|
||||
except Exception:
|
||||
pass
|
||||
logger.error(f'[TICKET HISTORY ERROR] ticket_id={ticket.id} field={field_name} error={exc}')
|
||||
Reference in New Issue
Block a user