Skip to content

fix(drizzle): reject commitTransaction when the database refuses the COMMIT - #18664

Open
SahinurDEV wants to merge 1 commit into
payloadcms:mainfrom
SahinurDEV:fix/drizzle-commit-failure-rejects
Open

SahinurDEV wants to merge 1 commit into
payloadcms:mainfrom
SahinurDEV:fix/drizzle-commit-failure-rejects

Conversation

@SahinurDEV

Copy link
Copy Markdown

What?

payload.db.commitTransaction(id) now rejects when the database refuses the COMMIT. Before this change it resolved normally while the database rolled the rows back.

Fixes #18653

Why?

When Postgres (or SQLite) refuses a COMMIT, for example because of a deferred constraint, a serialization failure or a connection that drops during the commit, drizzle rolls back and rejects the promise returned by drizzle.transaction(...). Two places dropped that error:

  1. beginTransaction attaches a .catch() to that promise so that a connection failure before the callback runs rejects beginTransaction instead of hanging. Because of that .catch(), the promise returned by resolve() can never reject. After the callback has run, transactionFailed(err) is a no-op on an already settled promise, so a COMMIT error was silently discarded.
  2. commitTransaction wrapped session.resolve() in try { … } catch (_) { await session.reject() }, so even if resolve() had thrown, the error would have been swallowed.

The result: payload.create / update / delete returned the document, afterChange hooks and application code ran as if the write had succeeded, and the data was gone.

How?

  • beginTransaction records that resolve() was called (committing). If the transaction promise rejects after that point, the error is kept and resolve() rethrows it once done settles. A rejection before the callback runs still goes through transactionFailed, so the "cannot begin transaction" path is unchanged. A rollback (reject()) also resolves as before.
  • commitTransaction still calls session.reject() when resolve() throws, and then rethrows the error. payload's operations already catch errors from commitTransaction(req), call killTransaction (a no-op here, because the session has already been removed) and rethrow, so the operation now rejects with the database error.

This is in @payloadcms/drizzle, so it covers db-postgres, db-vercel-postgres, db-sqlite and db-d1-sqlite. The same code is on 3.x, which is the version the reporter is on, so this should be easy to backport.

Tests

Added packages/drizzle/src/transactions/transactions.spec.ts. It runs the real transaction functions against drizzle + libsql on a temporary SQLite file, with a deferrable initially deferred foreign key so that the violation only shows up on COMMIT:

  • a committed transaction persists its rows
  • a rolled back transaction discards its rows and does not throw
  • a refused COMMIT rejects commitTransaction with the FOREIGN KEY error, and no rows are left behind. This test fails on main without the fix (it resolves)
  • beginTransaction still rejects with cannot begin transaction: … when the transaction can't be started
pnpm vitest run --project unit packages/drizzle/src/transactions   # 4 passed
pnpm eslint packages/drizzle/src/transactions

…COMMIT

The .catch() on the drizzle transaction promise, added for connection
failures before the callback runs, also swallowed errors thrown by the
COMMIT, and commitTransaction swallowed anything resolve() threw. A refused
COMMIT (deferred constraint, serialization failure, dropped connection)
therefore resolved normally while the rows were rolled back.

Keep the error when it happens after resolve() was called, rethrow it from
resolve(), and let commitTransaction propagate it.

Fixes payloadcms#18653

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

db-postgres / drizzle: commitTransaction resolves when the COMMIT fails, so a lost transaction looks like a success

1 participant