Repository navigation
fix(drizzle): reject commitTransaction when the database refuses the COMMIT - #18664
Open
SahinurDEV wants to merge 1 commit into
Open
SahinurDEV wants to merge 1 commit into
SahinurDEV wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:beginTransactionattaches a.catch()to that promise so that a connection failure before the callback runs rejectsbeginTransactioninstead of hanging. Because of that.catch(), the promise returned byresolve()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.commitTransactionwrappedsession.resolve()intry { … } catch (_) { await session.reject() }, so even ifresolve()had thrown, the error would have been swallowed.The result:
payload.create/update/deletereturned the document,afterChangehooks and application code ran as if the write had succeeded, and the data was gone.How?
beginTransactionrecords thatresolve()was called (committing). If the transaction promise rejects after that point, the error is kept andresolve()rethrows it oncedonesettles. A rejection before the callback runs still goes throughtransactionFailed, so the "cannot begin transaction" path is unchanged. A rollback (reject()) also resolves as before.commitTransactionstill callssession.reject()whenresolve()throws, and then rethrows the error.payload's operations already catch errors fromcommitTransaction(req), callkillTransaction(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 coversdb-postgres,db-vercel-postgres,db-sqliteanddb-d1-sqlite. The same code is on3.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 adeferrable initially deferredforeign key so that the violation only shows up on COMMIT:commitTransactionwith theFOREIGN KEYerror, and no rows are left behind. This test fails onmainwithout the fix (it resolves)beginTransactionstill rejects withcannot begin transaction: …when the transaction can't be started