fix: rollback aborted PG transaction on user delete FK violation
PostgreSQL enters 'current transaction is aborted' state after a ForeignKeyViolation. Without rollback(), every subsequent query on the shared connection fails with 'commands ignored until end of transaction block'. Now both db.py and server.py rollback on the error path.
This commit is contained in:
@@ -194,6 +194,7 @@ def user_delete(conn, user_id: str) -> dict:
|
||||
if _is_pg(conn):
|
||||
import psycopg
|
||||
if isinstance(e, psycopg.errors.ForeignKeyViolation):
|
||||
conn.rollback() # clear aborted transaction state
|
||||
return {"ok": False, "error": "user_has_references", "hint": "Inactivate the user instead of deleting."}
|
||||
raise
|
||||
return {"ok": False, "error": "user_has_references", "hint": "Inactivate the user instead of deleting."}
|
||||
|
||||
@@ -1105,6 +1105,7 @@ class HTTPServer:
|
||||
return (400, {"Content-Type": "application/json"}, json.dumps({"error": "cannot delete your own account"}))
|
||||
result = _db.user_delete(self._conn, uid)
|
||||
if not result.get("ok"):
|
||||
self._conn.rollback()
|
||||
status = 404 if result.get("error") == "not_found" else 409
|
||||
return (status, {"Content-Type": "application/json"}, json.dumps(result))
|
||||
actor = self._auth_user_id(auth)
|
||||
|
||||
Reference in New Issue
Block a user