Repository navigation
fix(core): plugin storage where-filters fail on Postgres with boolean = integer (#920) - #1898
Conversation
) 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 detectedLatest commit: f2922fb The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
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 |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
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.
|
CI's PG job did its job and caught a second, deeper layer of the same bug — updated in 604d00c: The Also swapped two 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.
There was a problem hiding this comment.
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()andcount()now call the sharedrawWhereExprhelper instead of buildingeb(predicate, "=", 1). This keeps SQLite behavior identical while resolving the Postgres type error. - SQL safety. Identifiers are validated through
validateIdentifier/validateJsonFieldNamebefore interpolation, and values remain parameterized through?placeholders. No raw string interpolation of untrusted input. - Index discipline. Expression indexes are created via the same
jsonExtractExprhelper used in queries/order-by (storage-indexes.ts), so index and query expressions stay identical after the::jsonbcast. - Tests & changeset. A new
describeEachDialectintegration test exercises filteredquery()/count()on both SQLite and Postgres, and a changeset is present for theemdashpackage. - 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.
… 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
…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]>
…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]>
What does this PR do?
Fixes #920: on PostgreSQL, every plugin storage
query()/count()call with awherefilter failed withoperator does not exist: boolean = integer(reported viaemdash-smtp, whose provider lookups filter storage).Root cause:
PluginStorageRepositoryfit the raw JSON-extraction predicate into Kysely's binaryeb(lhs, "=", rhs)by wrapping it as(json_extract(...) = ?) = 1. SQLite coerces booleans to integers so the extra= 1was 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 (rawWhereExprhelper, shared byquery()andcount()), keeping the existing?-placeholder parameterization intact. No SQL semantics change on SQLite.Tests: new
storage-dialect.test.tsruns the filtered query/count paths end-to-end viadescribeEachDialect, 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
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change) — plugin storage unit + integration suitespnpm formathas been runAI-generated code disclosure
Screenshots / test output