Skip to content

fix: await rollback in dry-run transactions#291

Merged
mesaugat merged 1 commit into
masterfrom
fix/dry-run-await-rollback
Jul 24, 2026
Merged

fix: await rollback in dry-run transactions#291
mesaugat merged 1 commit into
masterfrom
fix/dry-run-await-rollback

Conversation

@mesaugat

@mesaugat mesaugat commented Jul 22, 2026

Copy link
Copy Markdown
Member

Summary

  • withTransaction() in src/util/db.ts runs the dry-run path by opening a transaction, running the callback, then rolling back so nothing is committed. trx.rollback() was called without await, i.e. a floating promise.
  • This does not put the dry-run "no writes persist" guarantee at risk — commit() is never called on this path, so nothing becomes durable regardless of rollback timing; and if the connection drops before an explicit ROLLBACK is processed, the DB server itself aborts any open transaction on disconnect. The actual risks of the floating promise are:
    • Unhandled rejection crash: if rollback() itself rejects (e.g. connection already dropped), nothing awaits/catches it. There's no global unhandledRejection handler in this codebase, and Node 15+ terminates the process by default on an unhandled rejection — so a transient rollback error could crash the CLI with a misleading error after the dry run had already succeeded.
    • Misleading log ordering: 'END: Dry Run transaction rolled back successfully' was logged before rollback was confirmed to have completed.
    • Connection-pool cleanup timing when sync-db is used programmatically (not via the CLI's process.exit) in a longer-lived process — the connection isn't guaranteed released back to the pool by the time withTransaction resolves.
  • Fix: await trx.rollback() so the promise is properly awaited (and any rejection is attached to the returned promise chain) before withTransaction resolves.
  • Add test/unit/util/db.test.ts covering the await ordering (fails against the previous unawaited code), that no commit ever happens in dry-run, and that callback errors still propagate.

Test plan

  • yarn test:unit — 88 passing, including 3 new tests in UTIL: db
  • yarn lint — passing
  • Manually reverted the fix locally and confirmed the new test fails, proving it catches the regression

trx.rollback() was fired without awaiting, so withTransaction could
resolve (and the CLI could process.exit) before the ROLLBACK actually
completed against the database. Add a regression test covering the
ordering.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes dry-run transaction handling in withTransaction() by awaiting the rollback operation, preventing a floating rollback promise and improving correctness of async ordering and error propagation in dry-run mode.

Changes:

  • Await trx.rollback() in the dry-run path of withTransaction() to avoid unhandled rejections and ensure deterministic completion ordering.
  • Add unit tests covering rollback-await ordering, no-commit behavior in dry-run, and callback error propagation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/util/db.ts Awaits dry-run rollback to ensure the rollback promise is properly handled before returning.
test/unit/util/db.test.ts Adds unit tests validating dry-run rollback ordering and behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/util/db.ts
Comment on lines 91 to 95
const trx = await db.connection.transaction();
const res = await callback(trx);

trx.rollback();
await trx.rollback();

@silwalanish silwalanish left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GG

@mesaugat
mesaugat merged commit b39f951 into master Jul 24, 2026
5 checks passed
@mesaugat
mesaugat deleted the fix/dry-run-await-rollback branch July 24, 2026 11:05
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.

4 participants