Repository navigation
fix(core): compare plugin-storage numeric filters/order numerically on Postgres - #2152
Conversation
… 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
…cally 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
🦋 Changeset detectedLatest commit: 61a819c 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 |
There was a problem hiding this comment.
This is the right fix for the right problem. The Postgres plugin-storage numeric comparison/ordering bug is real and silently dangerous (over-count/oversell on gte/lte, wrong orderBy). The chosen approach is sound:
- It infers numeric-vs-text per condition from the JS type of the bound operand, which is the only signal available.
- It uses a type-guarded cast on Postgres (
jsonb_typeof(...) = 'number'→::numeric) so schemaless documents with non-numeric values don’t abort the query, matching SQLite’s tolerant behavior. - It orders by the jsonb-native value (
->) on Postgres, which sorts numbers numerically and stays total across mixed types. - It validates the interpolated field name before building SQL, consistent with AGENTS.md and existing helpers.
- It adds a dialect-parametric integration regression test and updates unit assertions to the new type-guarded SQLite output.
I checked the changed files in database/dialect-helpers.ts, database/repositories/plugin-storage.ts, plugins/storage-query.ts, the new storage-postgres-numeric-regression.test.ts, and the updated storage-query.test.ts. I also traced consumers (PluginStorageRepository.query/count, storage-indexes uses the untouched jsonExtractExpr, buildOrderByClause remains an exported helper not used by the repository). I did not find a logic bug, regression, SQL-injection surface, missing locale concern (plugin storage is not content-localized), or AGENTS.md violation.
One minority note: the PR description says “SQLite is unchanged,” but numeric conditions on SQLite now also emit the CASE WHEN json_type(...) IN ('integer', 'real') THEN json_extract(...) guard. For documents that store a number-looking value as a JSON string, old SQLite equality/range queries would match via SQLite’s cross-type comparison, whereas the new query will not. This is parity-correct with Postgres and arguably the correct behavior for a numeric filter, but it is a subtle behavior change on SQLite for non-conforming mixed-type data. This is not a blocker.
The changeset is present and well-written. I could not run pnpm test/typecheck/lint (no shell/tooling), so I’m taking the author’s report of green plugin-storage unit + integration runs on both dialects as unverified but plausible given the static shape of the diff.
LGTM.
@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/registry-verification
@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: |
…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 the remaining Postgres numeric bug in plugin storage.
_plugin_storage.datais atextcolumn. #1898 (fixing #920) added the(data)::jsonb->>'field'cast, which resolved thetext ->> unknownparse error and boolean/= 1filters — but the extracted value is still text, so numeric guards compared lexically:Consequences on Postgres (green on SQLite, whose
json_extractis typed):query({ where: { stock: { gte: 10 } } })over9 / 10 / 100matched 9 too — a silent over-count / oversell.count({ stock: { gte: 10 } })returned3instead of2.orderBy({ stock: 'asc' })returned[10, 100, 9]instead of[9, 10, 100].The fix
pluginDataExtractExpr(field, { numeric })— adds an optional type-guarded::numericcast (guarded byjsonb_typeof/json_type). Not a bare::numeric: a bare cast throwsinvalid input syntax for type numericthe moment one scanned row stores a non-number in that field (documents are schemaless). Guarded, a non-number yieldsNULL(no match) — total and parity-correct with SQLite.pluginDataOrderExpr(field)— orders over the jsonb-native value (->), whose btree ordering is numeric among numbers and total across heterogeneous data.buildConditionchooses numeric-vs-text per operand from its JS type;orderByuses the jsonb-native expression. SQLite is unchanged.The index path (text
->>, already fixed by #1898) is intentionally untouched — see the note ingenerateCreateIndexSql.Tests
Adds a dialect-parametric regression suite (
storage-postgres-numeric-regression.test.ts) and updates thebuildConditionunit assertions to the type-guarded SQL. Full run on both dialects:Related: #2150 (failing-test companion), #1898, #920.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change) — plugin-storage unit + integration suites, both dialectspnpm formathas been runAI-generated code disclosure
Screenshots / test output