Skip to content

test(nextly): fail integration tests that leave a transaction aborted - #412

Merged
mobeenabdullah merged 7 commits into
mainfrom
fix/pg-transaction-abort-guard
Jul 30, 2026
Merged

test(nextly): fail integration tests that leave a transaction aborted#412
mobeenabdullah merged 7 commits into
mainfrom
fix/pg-transaction-abort-guard

Conversation

@mobeenabdullah

@mobeenabdullah mobeenabdullah commented Jul 30, 2026

Copy link
Copy Markdown
Collaborator

The class of bug this catches

current transaction is aborted, commands ignored until end of transaction block is never the real error. It is PostgreSQL reporting that an earlier statement inside the transaction already failed and was swallowed — so every statement after it fails too, far from the cause.

The shape that produces it is an existence check written as "run a query and catch the failure":

try { await adapter.executeQuery(`SELECT 1 FROM ${t} LIMIT 0`); return true; }
catch { return false; }

Perfectly valid on SQLite and MySQL. On PostgreSQL, inside a transaction, it poisons the connection — the probe catches its own error and reports "absent", and the write that follows dies.

A suite can be green on two dialects and quietly broken on the third. During #382 that shape was found four times by reading code and zero times by the tests.

What this adds

  • The harness wraps every test adapter's transaction and records any aborted-transaction error before it propagates.
  • src/__tests__/setup.ts asserts after every test, so the failure is attributed to the test that caused it rather than to whichever runs last.
  • The message says plainly that it is the shadow of a swallowed error, and to go find that instead.

It ships with its own proof

A guard nobody has seen fail is only a green check. aborted-transaction-guard.integration.test.ts induces the state deliberately — swallow a query against a missing relation inside a transaction, then issue another — and asserts the guard recorded it, plus a companion case asserting silence when nothing aborts.

The test consumes its own sighting, which is both the assertion and the cleanup: the shared afterEach reads the same buffer and would otherwise fail it for demonstrating exactly what it set out to demonstrate.

PostgreSQL only — the other two dialects do not poison a transaction on a failed statement, which is the whole reason this is needed.

Verification

3-dialect on freshly recreated databases: sqlite 768, postgres17 888, mysql 832 — all exit=0. check-types and lint clean.

Notably the guard finds nothing on current main, which is the honest result: the one known instance (the read-path companion join) has no test exercising it, since that reproduction was removed from #382 when it could not pass. So this lands green and starts catching regressions from here, and the follow-up that fixes the read path will land its reproduction against a suite that can already see the failure.

Summary by CodeRabbit

  • Bug Fixes

    • Added detection for PostgreSQL transactions that remain aborted after a swallowed failure.
    • Test runs now report clear diagnostics instead of silently continuing after transaction errors.
    • Added safeguards to prevent duplicate transaction monitoring during reinitialization.
  • New Features

    • Added testing helpers for inspecting and describing aborted-transaction sightings.
    • Exported the diagnostics through the testing APIs for broader test-suite integration.
  • Tests

    • Added coverage for localized, nested, and malformed transaction errors, clean transactions, shared sightings, and repeated initialization.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4c340e13-6e3c-4ffa-a45b-3060b8f220d8

📥 Commits

Reviewing files that changed from the base of the PR and between 1e0ef91 and a0e2459.

📒 Files selected for processing (7)
  • packages/nextly/src/__tests__/setup.ts
  • packages/nextly/src/plugins/__tests__/aborted-transaction-guard.integration.test.ts
  • packages/nextly/src/plugins/__tests__/aborted-transaction-sightings.test.ts
  • packages/nextly/src/plugins/aborted-transaction-sightings.ts
  • packages/nextly/src/plugins/test-nextly.ts
  • packages/nextly/src/testing.ts
  • packages/plugin-sdk/src/testing.ts

📝 Walkthrough

Walkthrough

Adds PostgreSQL aborted-transaction detection, shared sighting storage, adapter transaction instrumentation, centralized test failure reporting, and public testing helpers with unit and integration coverage.

Changes

Aborted-transaction guard

Layer / File(s) Summary
Sightings detection and reporting
packages/nextly/src/plugins/aborted-transaction-sightings.ts, packages/nextly/src/plugins/__tests__/aborted-transaction-sightings.test.ts
Detects PostgreSQL aborted transactions through SQLSTATE, messages, and nested causes; records and consumes shared sightings; formats diagnostic descriptions.
Transaction adapter instrumentation
packages/nextly/src/plugins/test-nextly.ts, packages/nextly/src/plugins/__tests__/aborted-transaction-guard.integration.test.ts
Probes transactions with savepoints, records aborted states, preserves other outcomes, and prevents duplicate adapter wrapping.
Test harness enforcement and exports
packages/nextly/src/__tests__/setup.ts, packages/nextly/src/testing.ts, packages/plugin-sdk/src/testing.ts
Fails tests when aborted-transaction sightings remain and exposes the diagnostic helpers through testing entry points.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TestCallback
  participant AdapterTransaction
  participant SavepointProbe
  participant SightingsBuffer
  participant AfterEachHook
  TestCallback->>AdapterTransaction: run transaction callback
  AdapterTransaction->>SavepointProbe: probe with savepoint
  SavepointProbe->>SightingsBuffer: record aborted transaction
  AfterEachHook->>SightingsBuffer: describe and consume sightings
  AfterEachHook-->>TestCallback: fail test when diagnosis exists
Loading

Suggested labels: scope: core, scope: plugin

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning It covers summary and verification, but misses most required template sections like Type of change, Related issues, Changeset, Test plan, and Checklist. Rewrite the PR description using the repository template and fill in the missing sections, especially Type of change, Changeset, Test plan, and Checklist.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: failing tests when a transaction is left aborted.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/pg-transaction-abort-guard

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added scope: core nextly type: docs Documentation only labels Jul 30, 2026
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex please review this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

@nextlyhq/adapter-drizzle

npm i https://pkg.pr.new/@nextlyhq/adapter-drizzle@a0e2459

@nextlyhq/adapter-mysql

npm i https://pkg.pr.new/@nextlyhq/adapter-mysql@a0e2459

@nextlyhq/adapter-postgres

npm i https://pkg.pr.new/@nextlyhq/adapter-postgres@a0e2459

@nextlyhq/adapter-sqlite

npm i https://pkg.pr.new/@nextlyhq/adapter-sqlite@a0e2459

@nextlyhq/admin

npm i https://pkg.pr.new/@nextlyhq/admin@a0e2459

@nextlyhq/admin-css

npm i https://pkg.pr.new/@nextlyhq/admin-css@a0e2459

@nextlyhq/blocks-engine

npm i https://pkg.pr.new/@nextlyhq/blocks-engine@a0e2459

create-nextly-app

npm i https://pkg.pr.new/create-nextly-app@a0e2459

nextly

npm i https://pkg.pr.new/nextly@a0e2459

@nextlyhq/plugin-form-builder

npm i https://pkg.pr.new/@nextlyhq/plugin-form-builder@a0e2459

@nextlyhq/plugin-page-builder

npm i https://pkg.pr.new/@nextlyhq/plugin-page-builder@a0e2459

@nextlyhq/plugin-sdk

npm i https://pkg.pr.new/@nextlyhq/plugin-sdk@a0e2459

@nextlyhq/plugin-seo

npm i https://pkg.pr.new/@nextlyhq/plugin-seo@a0e2459

@nextlyhq/storage-s3

npm i https://pkg.pr.new/@nextlyhq/storage-s3@a0e2459

@nextlyhq/storage-uploadthing

npm i https://pkg.pr.new/@nextlyhq/storage-uploadthing@a0e2459

@nextlyhq/storage-vercel-blob

npm i https://pkg.pr.new/@nextlyhq/storage-vercel-blob@a0e2459

@nextlyhq/ui

npm i https://pkg.pr.new/@nextlyhq/ui@a0e2459

commit: a0e2459

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a95008866

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/plugins/test-nextly.ts
Comment thread packages/nextly/src/__tests__/setup.ts Outdated
Comment thread packages/nextly/src/plugins/test-nextly.ts Outdated
Comment thread packages/nextly/src/__tests__/setup.ts Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a48f7a61fd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/plugins/test-nextly.ts Outdated
Comment thread packages/nextly/src/plugins/test-nextly.ts Outdated
`current transaction is aborted` is never the real error. It is PostgreSQL
reporting that an earlier statement inside the transaction already failed and was
swallowed, so everything after it fails too. The shape that produces it is an
existence check written as "run a query and catch the failure" — valid on SQLite
and MySQL, fatal on PostgreSQL, and therefore invisible to a suite that passes on
the first two.

That class has been found repeatedly by reading code and never once by the tests,
because the symptom surfaces far from its cause and no individual test knows to
look for it. The harness now records any sighting and the shared setup asserts
after EVERY test, so the failure is attributed to the test that caused it rather
than to whichever happens to run last.

Shipped with a test that induces the state deliberately — swallow a query against
a missing relation inside a transaction, then issue another — and asserts the
guard saw it. A guard that has never been observed failing is only a green check,
and this one is verified by construction rather than by whether it happens to
catch a bug someone already knows about. The test consumes its own sighting,
which is both the assertion and the cleanup: the shared hook reads the same
buffer and would otherwise fail it for demonstrating exactly what it demonstrates.

PostgreSQL only, since the other two dialects do not poison a transaction on a
failed statement, which is the whole reason this is needed.
`src/__tests__/setup.ts` is the setup file for BOTH vitest configs, so importing
the integration harness there loaded the DI registry, the adapters and the event
bus into every unit test. Seven schema-pipeline and database suites failed on CI
as a result, while the integration legs stayed green locally — the import was only
harmful where the harness was never meant to go.

The sightings buffer moves to its own dependency-free module, which both the setup
file and the harness import. The assertion still runs for every test; it just no
longer drags the harness with it.
The guard only saw aborts that propagated out of `transaction()`. A callback
that catches its own failure and returns normally never produces one:
PostgreSQL accepts the COMMIT, downgrades it to a rollback, and the call
resolves over a transaction that kept nothing. The bulk write paths do exactly
this for per-item errors while `stopOnError` is false, so the swallow-and-
continue class the guard targets could stay green.

Probe the transaction context once the callback resolves, so the aborted state
itself answers rather than the error that caused it. Report through
`expect.fail` instead of a bare `Error`.
@mobeenabdullah
mobeenabdullah force-pushed the fix/pg-transaction-abort-guard branch from a48f7a6 to b16a7d7 Compare July 30, 2026 08:52
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex please review this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b16a7d73cc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread .changeset/pg-aborted-transaction-guard.md Outdated
The buffer lived in module scope, and there is more than one instance of the
module: a suite importing the harness through `nextly/testing` gets the copy
bundled into `dist/testing.mjs`, while the shared setup reads the source file.
An abort recorded in one array was invisible to the assertion reading the
other, so the guard failed open and reported green. Hold it on `globalThis`,
which is how `init/schema-snapshot-cache.ts` already handles the same problem.

Detect the abort by SQLSTATE `25P02` rather than only by its English text,
which PostgreSQL translates according to `lc_messages`. The adapter preserves
the driver's code on the errors it classifies, and the cause chain is walked
because the code can sit on either the wrapper or the original.
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex please review this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d18157ff42

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/plugins/test-nextly.ts
Comment thread packages/nextly/src/plugins/test-nextly.ts
Comment thread packages/nextly/src/plugins/test-nextly.ts
Publish the recording through `nextly/testing`. `createTestNextly` reaches
plugin authors via `@nextlyhq/plugin-sdk/testing`, where the recording had no
reader, so a swallowed abort still reported green. `describeAbortedTransactions`
returns the message instead of throwing: shipped code cannot import vitest, and
a NextlyError would replace the sightings with its generic public message. Both
this package's setup file and a consumer's now share one buffer and one
diagnosis, each asserting with its own runner.

Install the wrapper once per adapter. An adapter handed to `createTestNextly`
across boots was wrapped again each time, so one abort reported once per boot.

Propagate probe failures that are not the abort signature. A `statement_timeout`
from the `timeoutMs` option would otherwise be swallowed after the probe had
already aborted an untouched transaction, which is the silent discard this
guards against.

Drop the changeset: this only instruments the test harness.
@github-actions github-actions Bot removed the type: docs Documentation only label Jul 30, 2026
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex please review this PR

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b941f165e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/nextly/src/testing.ts
Comment thread packages/nextly/src/plugins/test-nextly.ts Outdated
`nextly/testing` ships this module, so a raw statement in it puts SQL where
product code is not allowed to keep it. An aborted transaction rejects
`SAVEPOINT` with the same code it rejects a query by, so the adapter's own
savepoint method answers the question and the string goes away. Released
immediately when the transaction is healthy, which leaves it as the callback
left it.

Forward the readers through `@nextlyhq/plugin-sdk/testing` as well. That is the
surface plugin authors are pointed at, and until now the recording had no import
there, so a swallowed abort still reported green. The error matcher stays behind:
a plugin classifying driver errors by hand means something else has gone wrong.
@github-actions github-actions Bot added the scope: plugin @nextlyhq/plugin-* packages label Jul 30, 2026
@mobeenabdullah

Copy link
Copy Markdown
Collaborator Author

@codex please review this PR

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: a0e24595f0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@mobeenabdullah
mobeenabdullah merged commit 37127c6 into main Jul 30, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: core nextly scope: plugin @nextlyhq/plugin-* packages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant