Skip to content

fix(core): plugin storage where-filters fail on Postgres with boolean = integer (#920) - #1898

Merged
ascorbic merged 4 commits into
emdash-cms:mainfrom
swissky:fix/plugin-storage-pg-boolean
Jul 20, 2026
Merged

ascorbic merged 4 commits into
emdash-cms:mainfrom
swissky:fix/plugin-storage-pg-boolean

Conversation

@swissky

@swissky swissky commented Jul 9, 2026

Copy link
Copy Markdown
Member

What does this PR do?

Fixes #920: on PostgreSQL, every plugin storage query() / count() call with a where filter failed with operator does not exist: boolean = integer (reported via emdash-smtp, whose provider lookups filter storage).

Root cause: PluginStorageRepository fit the raw JSON-extraction predicate into Kysely's binary eb(lhs, "=", rhs) by wrapping it as (json_extract(...) = ?) = 1. SQLite coerces booleans to integers so the extra = 1 was harmless there; Postgres has a strict boolean type and rejects the comparison outright — so filtered reads never worked on PG.

Fix: pass the predicate directly to .where() as a boolean raw expression (rawWhereExpr helper, shared by query() and count()), keeping the existing ?-placeholder parameterization intact. No SQL semantics change on SQLite.

Tests: new storage-dialect.test.ts runs the filtered query/count paths end-to-end via describeEachDialect, so the regression is exercised against real Postgres in CI's PG job (equality filters, where + orderBy, range filters, filtered counts). Verified green on SQLite locally; I don't have a local Postgres, so relying on the CI PG matrix for the dialect that actually regressed — the failure mode is deterministic (the old SQL is a hard type error on PG).

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change) — plugin storage unit + integration suites
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable) — n/a
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion — n/a, bug fix

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Claude (Fable 5)

Screenshots / test output

 Test Files  6 passed (6)
      Tests  116 passed (116)   (integration/plugins, SQLite)
 Test Files  1 passed (1)
      Tests  30 passed (30)     (unit plugin-storage)

)

query() and count() wrapped the JSON-extraction predicate in
`(...) = 1` to fit eb(lhs, "=", rhs). SQLite coerces booleans to
integers so it worked there, but Postgres has a strict boolean type
and rejected every filtered call with "operator does not exist:
boolean = integer". Use the predicate directly as a boolean WHERE
expression instead; dialect-parity regression tests included.
@changeset-bot

changeset-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f2922fb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1898

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1898

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1898

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1898

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1898

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1898

emdash

npm i https://pkg.pr.new/emdash@1898

create-emdash

npm i https://pkg.pr.new/create-emdash@1898

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1898

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1898

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1898

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1898

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1898

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1898

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1898

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1898

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1898

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1898

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1898

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1898

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1898

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1898

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1898

commit: f2922fb

CI's PG job surfaced the layer below the `= 1` wrapper:
_plugin_storage.data is a text column, so `data->>'field'` fails with
"operator does not exist: text ->> unknown". Cast via `(data)::jsonb`
in jsonExtractExpr — shared by queries and expression indexes, so the
planner keeps matching them. Also switch test .sort() to .toSorted()
for the type-aware lint.
@swissky

swissky commented Jul 9, 2026

Copy link
Copy Markdown
Member Author

CI's PG job did its job and caught a second, deeper layer of the same bug — updated in 604d00c:

The = 1 wrapper wasn't the only reason filtered reads never worked on Postgres. _plugin_storage.data is a text column, and jsonExtractExpr emitted data->>'field' on PG — text ->> unknown isn't an operator, so the extraction itself was broken too (the CI failure: operator does not exist: text ->> unknown). The fix casts through (data)::jsonb->>'field' in jsonExtractExpr, which is shared by the query builder and the expression-index builder (storage-indexes.ts), so indexes and predicates keep emitting the identical expression and the planner can still match them. It also means plugin storage expression indexes were never creatable on PG before this — same operator error at CREATE INDEX time.

Also swapped two .sort() calls in the new test for .toSorted() to satisfy the type-aware lint (that was the Lint job failure).

The dialect-parity tests in this PR now exercise the fixed path end-to-end on the CI PG matrix.

pg returns COUNT(*) (bigint) as a string; Number() it like every
other repository count in the codebase.
@swissky swissky added the bot:review Trigger an emdashbot code review on this PR label Jul 10, 2026
@ascorbic ascorbic added bot:review Trigger an emdashbot code review on this PR and removed bot:review Trigger an emdashbot code review on this PR labels Jul 20, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is the right fix for the right problem.

The PR removes the (...) = 1 wrapper that made filtered plugin-storage queries fail on Postgres (boolean = integer), and it supplies the predicate directly as a boolean raw expression via the new rawWhereExpr helper. The jsonExtractExpr change from column->>'path' to (column)::jsonb->>'path' is necessary because _plugin_storage.data is stored as text, not jsonb.

What I checked:

  • Root-cause fix. Both query() and count() now call the shared rawWhereExpr helper instead of building eb(predicate, "=", 1). This keeps SQLite behavior identical while resolving the Postgres type error.
  • SQL safety. Identifiers are validated through validateIdentifier/validateJsonFieldName before interpolation, and values remain parameterized through ? placeholders. No raw string interpolation of untrusted input.
  • Index discipline. Expression indexes are created via the same jsonExtractExpr helper used in queries/order-by (storage-indexes.ts), so index and query expressions stay identical after the ::jsonb cast.
  • Tests & changeset. A new describeEachDialect integration test exercises filtered query()/count() on both SQLite and Postgres, and a changeset is present for the emdash package.
  • AGENTS.md conventions. No Lingui/RTL surface, no content-table locale filtering, no unauthorized route changes. Scope is tight and focused.

One latent, pre-existing limitation worth noting: because Postgres ->> always returns text, numeric orderBy on multi-digit values will sort lexicographically rather than numerically. That behavior predates this PR and is not introduced by the fix; the new test happens to use single-digit priorities where lexicographic and numeric order agree. If that becomes a real-world issue it should be addressed separately, but it is not a regression from this change.

I found no blocking issues or AGENTS.md violations. LGTM.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Jul 20, 2026
@ascorbic
ascorbic merged commit e2c7549 into emdash-cms:main Jul 20, 2026
46 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 20, 2026
vedanshujain added a commit to vedanshujain/emdash that referenced this pull request Jul 20, 2026
… Postgres

`_plugin_storage.data` is a `text` column. emdash-cms#1898 added the `(data)::jsonb->>'…'`
cast so extraction no longer errors and boolean/equality filters work, but `->>`
still yields text and the query/count/order paths compare it directly. On
Postgres that means numeric guards compare lexically — `'9' >= '10'` is TRUE —
so a `{ stock: { gte: 10 } }` filter over 9/10/100 silently includes 9
(over-count / oversell) and numeric orderBy returns [10, 100, 9].

Adds a dialect-parametric suite (describeEachDialect) pinning the remaining
failure modes. SQLite's json_extract is typed, so it stays green; the Postgres
dialect fails on the range/count/order cases on current main. Postgres runs only
when EMDASH_TEST_PG is set.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW
ascorbic pushed a commit that referenced this pull request Jul 21, 2026
…n Postgres (#2152)

* test(core): plugin-storage numeric filters/order compare lexically on Postgres

`_plugin_storage.data` is a `text` column. #1898 added the `(data)::jsonb->>'…'`
cast so extraction no longer errors and boolean/equality filters work, but `->>`
still yields text and the query/count/order paths compare it directly. On
Postgres that means numeric guards compare lexically — `'9' >= '10'` is TRUE —
so a `{ stock: { gte: 10 } }` filter over 9/10/100 silently includes 9
(over-count / oversell) and numeric orderBy returns [10, 100, 9].

Adds a dialect-parametric suite (describeEachDialect) pinning the remaining
failure modes. SQLite's json_extract is typed, so it stays green; the Postgres
dialect fails on the range/count/order cases on current main. Postgres runs only
when EMDASH_TEST_PG is set.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

* fix(core): compare plugin-storage numeric filters and ordering numerically on Postgres

`_plugin_storage.data` is a `text` column. After the `(data)::jsonb->>'field'`
cast (#1898) the extracted value is still text, so numeric guards compared
lexically on Postgres — `'9' >= '10'` is TRUE — and `{ stock: { gte: 10 } }`
over 9/10/100 matched 9 (silent over-count / oversell), while numeric `orderBy`
sorted `[10, 100, 9]`.

- Add `pluginDataExtractExpr` with an optional **type-guarded** `::numeric` cast
  (guarded by `jsonb_typeof`/`json_type`), so numeric comparisons are numeric and
  total: a non-number stored value yields NULL (no match) instead of aborting the
  query with `invalid input syntax for type numeric`.
- Add `pluginDataOrderExpr` that orders over the jsonb-native value (`->`), whose
  btree ordering is numeric among numbers and total across heterogeneous data.
- `buildCondition` picks numeric-vs-text per operand from its JS type; `orderBy`
  uses the jsonb-native expression. SQLite is unchanged (`json_extract` is typed).

Builds on #1898 (jsonb cast + boolean/`= 1` fix); this is the remaining numeric
piece. Regression suite runs on both dialects (Postgres via EMDASH_TEST_PG).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
swissky pushed a commit to swissky/emdash that referenced this pull request Jul 21, 2026
…n Postgres (emdash-cms#2152)

* test(core): plugin-storage numeric filters/order compare lexically on Postgres

`_plugin_storage.data` is a `text` column. emdash-cms#1898 added the `(data)::jsonb->>'…'`
cast so extraction no longer errors and boolean/equality filters work, but `->>`
still yields text and the query/count/order paths compare it directly. On
Postgres that means numeric guards compare lexically — `'9' >= '10'` is TRUE —
so a `{ stock: { gte: 10 } }` filter over 9/10/100 silently includes 9
(over-count / oversell) and numeric orderBy returns [10, 100, 9].

Adds a dialect-parametric suite (describeEachDialect) pinning the remaining
failure modes. SQLite's json_extract is typed, so it stays green; the Postgres
dialect fails on the range/count/order cases on current main. Postgres runs only
when EMDASH_TEST_PG is set.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

* fix(core): compare plugin-storage numeric filters and ordering numerically on Postgres

`_plugin_storage.data` is a `text` column. After the `(data)::jsonb->>'field'`
cast (emdash-cms#1898) the extracted value is still text, so numeric guards compared
lexically on Postgres — `'9' >= '10'` is TRUE — and `{ stock: { gte: 10 } }`
over 9/10/100 matched 9 (silent over-count / oversell), while numeric `orderBy`
sorted `[10, 100, 9]`.

- Add `pluginDataExtractExpr` with an optional **type-guarded** `::numeric` cast
  (guarded by `jsonb_typeof`/`json_type`), so numeric comparisons are numeric and
  total: a non-number stored value yields NULL (no match) instead of aborting the
  query with `invalid input syntax for type numeric`.
- Add `pluginDataOrderExpr` that orders over the jsonb-native value (`->`), whose
  btree ordering is numeric among numbers and total across heterogeneous data.
- `buildCondition` picks numeric-vs-text per operand from its JS type; `orderBy`
  uses the jsonb-native expression. SQLite is unchanged (`json_extract` is typed).

Builds on emdash-cms#1898 (jsonb cast + boolean/`= 1` fix); this is the remaining numeric
piece. Regression suite runs on both dialects (Postgres via EMDASH_TEST_PG).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01TX9YciGFRZX9aF2UcQ6rUW

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
@swissky
swissky deleted the fix/plugin-storage-pg-boolean branch September 10, 2026 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Postgres: plugin-storage list/count fail with "operator does not exist: boolean = integer" when filtering

2 participants