You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
schema-registry: publishing a schema draft doesn't backfill new-field defaults into existing records #57
app/base/tests/schema-registry.test.ts has 4 pre-existing failures (red on main, now visible since CI runs after the repo went public). These are genuine migration bugs, not stale tests, and are known-hard (a prior pass took them from 8 → 4).
Repro
POSTGRES_TEST_URL=postgres://cms_test:cms_test@localhost:5432/postgres \
pnpm --filter base exec vitest run tests/schema-registry.test.ts
Failing cases (all under persisted runtime collection schemas / CMS nested schemas exposed by Base):
migrates committed and pending records, preserves revisions, and upgrades old revision restores
rejects transactions captured before a schema publish without overwriting migrated records
serializes schema publication with in-flight content mutations
migrates nested additions in committed/pending records and old revisions without losing quantities or item order
Root cause (first failure)
Publishing a schema draft that adds a field with a default does not backfill that default into existing committed/pending records:
Spent a focused session tracing the first failure (:368, migrated.doc.loyalty is undefined, expected 10). Findings, to save the next person the dead ends:
The migration logic is correct.publishSchemaDraft (app/base/src/schema-registry.server.ts) calls applyFieldAdditions(row.doc, beforeFields, newFields), which correctly produces { ...doc, loyalty: 10 } and stamps _contentVersion: 2. Verified in isolation — applyFieldAdditions and fieldDefault/emptyDoc honor defaultValue.
The write reaches the store and its transaction commits. Instrumenting the PGlite adapter (packages/store-postgres/src/index.ts):
putDocument receives and stores JSON.stringify(doc) = {…,_contentVersion:2,loyalty:10} (logged [PG-PUT]).
The enclosing transact() logs BEGIN→COMMIT with no ROLLBACK.
getDocument → toDocument(row) returns row.doc verbatim (no projection there).
But the next read returns the pre-migration v1 doc (_contentVersion:1, no loyalty) — on the same single PGlite instance (confirmed by tagging the adapter instance id; put and get share it) and the same table (cms_documents). So a committed write is invisible to a subsequent read on one instance.
Ruled out:
applyFieldAdditions (correct in isolation).
The read-side migration (upgradeStoredDoc returns the doc unchanged when from ≥ latest; it never strips/downgrades).
The PGlite pool: a standalone createPglitePool test doing BEGIN/parameterized INSERT/COMMIT/read persists correctly — even though the pool routes unparameterized SQL through exec() and parameterized through query().
Dual instances / wrong table / schema projection in toDocument.
Where it points: a transaction-visibility/persistence issue in the core composite transaction (packages/core/src/server/storage.ts, the txFor/nested-adapter wrapper) or its interaction with the embedded engine during publishSchemaDraft — not the migration code, the SQL, or the pool. Note the working path (test setup writes 'ada' via withSchemaRegistry → snapshotStorage → the CMS write path) persists, while publishSchemaDraft's raw getStorage().transaction(tx => tx.putDocument(...)) does not. The delta between those two write paths is the thing to chase next.
Repro: POSTGRES_TEST_URL=postgres://cms_test:cms_test@localhost:5432/postgres pnpm --filter base exec vitest run tests/schema-registry.test.ts.
Root cause — not the transaction/storage layer the earlier deep-dive suspected. publishSchemaDraft's migration loop passed the whole old row as putDocument's second argument, which must be a DocumentIndex. Because IndexedDocumentRecord extends DocumentIndex it type-checked, but row carried the pre-migration doc. store-postgres serialized the whole object into the index_data JSONB column, and toDocument spread index_dataafterdoc:, so on read the stale doc overwrote the freshly-migrated one. The write committed correctly every time; the read was clobbering itself — which is exactly why prior instrumentation saw correct v2 JSON on disk but got v1 back.
Fix (defense in depth):
schema-registry.server.ts — pass an explicit DocumentIndex (no record fields).
store-postgres — toDocument spreads index_data first so real columns always win (also repairs already-poisoned rows on read), and putDocument strips doc/collection/id/updatedAt out of index_data.
conformance.ts — new case asserting putDocument(record, fullRowAsIndex) round-trips the doc, so every adapter is guarded against this (the types invite the mistake).
Verified: schema-registry 4→16 pass; store-postgres 156 (pglite + real Postgres); cms-core 647; full base suite 42.
Activity
fix: work through the pre-existing CI backlog surfaced by going public (
jherr commentedon Nov 30, 2026
Investigation notes (deep-dive, not yet fixed)
Spent a focused session tracing the first failure (
:368,migrated.doc.loyaltyisundefined, expected10). Findings, to save the next person the dead ends:The migration logic is correct.
publishSchemaDraft(app/base/src/schema-registry.server.ts) callsapplyFieldAdditions(row.doc, beforeFields, newFields), which correctly produces{ ...doc, loyalty: 10 }and stamps_contentVersion: 2. Verified in isolation —applyFieldAdditionsandfieldDefault/emptyDochonordefaultValue.The write reaches the store and its transaction commits. Instrumenting the PGlite adapter (
packages/store-postgres/src/index.ts):putDocumentreceives and storesJSON.stringify(doc)={…,_contentVersion:2,loyalty:10}(logged[PG-PUT]).transact()logsBEGIN→COMMITwith noROLLBACK.getDocument→toDocument(row)returnsrow.docverbatim (no projection there).But the next read returns the pre-migration v1 doc (
_contentVersion:1, noloyalty) — on the same single PGlite instance (confirmed by tagging the adapter instance id; put and get share it) and the same table (cms_documents). So a committed write is invisible to a subsequent read on one instance.Ruled out:
applyFieldAdditions(correct in isolation).upgradeStoredDocreturns the doc unchanged whenfrom ≥ latest; it never strips/downgrades).createPglitePooltest doingBEGIN/parameterizedINSERT/COMMIT/read persists correctly — even though the pool routes unparameterized SQL throughexec()and parameterized throughquery().toDocument.Where it points: a transaction-visibility/persistence issue in the core composite
transaction(packages/core/src/server/storage.ts, thetxFor/nested-adapter wrapper) or its interaction with the embedded engine duringpublishSchemaDraft— not the migration code, the SQL, or the pool. Note the working path (test setup writes 'ada' viawithSchemaRegistry→snapshotStorage→ the CMS write path) persists, whilepublishSchemaDraft's rawgetStorage().transaction(tx => tx.putDocument(...))does not. The delta between those two write paths is the thing to chase next.Repro:
POSTGRES_TEST_URL=postgres://cms_test:cms_test@localhost:5432/postgres pnpm --filter base exec vitest run tests/schema-registry.test.ts.jherr commentedon Nov 30, 2026
Fixed in #58 (merged to main as de2b8e9).
Root cause — not the transaction/storage layer the earlier deep-dive suspected.
publishSchemaDraft's migration loop passed the whole oldrowasputDocument's second argument, which must be aDocumentIndex. BecauseIndexedDocumentRecord extends DocumentIndexit type-checked, butrowcarried the pre-migrationdoc. store-postgres serialized the whole object into theindex_dataJSONB column, andtoDocumentspreadindex_dataafterdoc:, so on read the stale doc overwrote the freshly-migrated one. The write committed correctly every time; the read was clobbering itself — which is exactly why prior instrumentation saw correct v2 JSON on disk but got v1 back.Fix (defense in depth):
schema-registry.server.ts— pass an explicitDocumentIndex(no record fields).store-postgres—toDocumentspreadsindex_datafirst so real columns always win (also repairs already-poisoned rows on read), andputDocumentstripsdoc/collection/id/updatedAtout ofindex_data.conformance.ts— new case assertingputDocument(record, fullRowAsIndex)round-trips the doc, so every adapter is guarded against this (the types invite the mistake).Verified: schema-registry 4→16 pass; store-postgres 156 (pglite + real Postgres); cms-core 647; full base suite 42.