refactor(csv_parser): Implement SAVEPOINT handling for activity import
All checks were successful
Deploy Development / deploy (push) Successful in 47s
Build Test / pytest-backend (push) Successful in 9s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 15s

- Added SAVEPOINT management to the `_import_activity` function to improve error handling during CSV imports.
- Moved the training type resolution logic to occur within a transaction block, ensuring that errors can be rolled back without affecting the entire import process.
- Enhanced error logging to capture evaluation failures, providing better insights during CSV import operations.
This commit is contained in:
Lars 2026-04-11 06:31:42 +02:00
parent 894ee1dd02
commit 08a2485f43

View File

@ -872,13 +872,14 @@ def _import_activity(
dist = _sf_act(mapped.get("distance_km"))
wtype = str(activity_type).strip()
iso = date_d.isoformat()
# Pro Zeile: bei SQL-Fehler sonst „current transaction is aborted“ bis Xact-Ende.
cur.execute("SAVEPOINT csv_activity_row")
try:
training_type_id, training_category, training_subcategory = _resolve_training_type_for_activity(
cur, wtype, profile_id
)
iso = date_d.isoformat()
try:
cur.execute(
"""
SELECT id FROM activity_log
@ -986,7 +987,12 @@ def _import_activity(
_evaluate_and_save_activity(cur, aid, activity_dict, training_type_id, profile_id)
except Exception as eval_err:
logger.warning("[csv activity] Auto-Eval fehlgeschlagen: %s", eval_err)
cur.execute("RELEASE SAVEPOINT csv_activity_row")
except Exception as e:
try:
cur.execute("ROLLBACK TO SAVEPOINT csv_activity_row")
except Exception:
pass
error_details.append({"row": rows_total, "error": str(e)})
return {