fix(postgres): savepoint Plaid bank-account creation loop

add_bank_accounts() inserts a Bank Account per Plaid account in a loop. On a
duplicate the bare insert raises UniqueValidationError, which on Postgres aborts
the whole transaction; the handler only msgprint'd and continued, so the next
iteration's insert died with InFailedSqlTransaction. Wrap each iteration in a
savepoint and roll back to it in the handlers (the pattern frappe#40075 prescribes
after dropping the blanket per-insert savepoint). No-op on MariaDB.
This commit is contained in:
Mihir Kandoi
2026-06-21 15:48:32 +05:30
parent 07aa0fe6c1
commit 0e25a77a62

View File

@@ -113,6 +113,8 @@ def add_bank_accounts(response: str | dict, bank: str | dict, company: str):
if not existing_bank_account:
try:
# savepoint so a failed insert doesn't poison the transaction on postgres
frappe.db.savepoint("plaid_bank_account")
gl_account = frappe.get_doc(
{
"doctype": "Account",
@@ -142,12 +144,14 @@ def add_bank_accounts(response: str | dict, bank: str | dict, company: str):
result.append(new_account.name)
except frappe.UniqueValidationError:
frappe.db.rollback(save_point="plaid_bank_account") # preserve transaction in postgres
frappe.msgprint(
_("Bank account {0} already exists and could not be created again").format(
account["name"]
)
)
except Exception:
frappe.db.rollback(save_point="plaid_bank_account") # preserve transaction in postgres
frappe.log_error("Plaid Link Error")
frappe.throw(
_("There was an error creating Bank Account while linking with Plaid."),