Skip to content

Keep the original error when a transaction is already rolled back - #244

Open
RaphaelFakhri wants to merge 1 commit into
tursodatabase:mainfrom
RaphaelFakhri:fix-transaction-rollback-error
Open

RaphaelFakhri wants to merge 1 commit into
tursodatabase:mainfrom
RaphaelFakhri:fix-transaction-rollback-error

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Database.transaction() runs ROLLBACK whenever the wrapped function throws. If SQLite has already rolled the transaction back (for example after INSERT OR ROLLBACK or RAISE(ROLLBACK, ...)), that ROLLBACK fails with cannot rollback - no transaction is active and replaces the real error.

This change runs ROLLBACK only while db.inTransaction is true, which matches better-sqlite3. It applies to both libsql and libsql/promise.

Changes

  • compat.js and promise.js: skip ROLLBACK in the transaction wrapper when no transaction is active.
  • integration-tests/tests/sync.test.js and async.test.js: add a test that triggers INSERT OR ROLLBACK inside a transaction and expects SQLITE_CONSTRAINT_PRIMARYKEY. The sync test also passes against better-sqlite3 (PROVIDER=sqlite).

Testing

  • Without the fix, the new test fails on test:libsql and test:async with SQLITE_ERROR: cannot rollback - no transaction is active.
  • With the fix, all integration suites pass: test:sqlite 53, test:libsql 53, test:async 68, test:extensions 29, test:concurrency 4, test:connections 2.

Fixes #243

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.

Database.transaction() hides the original error when SQLite already rolled back the transaction

1 participant