Skip to content

schema-registry: publishing a schema draft doesn't backfill new-field defaults into existing records #57

Done

Description

@jherr

Summary

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:

tests/schema-registry.test.ts:368
expect(migrated.doc.loyalty).toBe(10)   // Received: undefined

Lives in app/base/src/schema-registry.server.ts (publishSchemaDraft migration path).

Status

Deferred. The rest of the post-public CI backlog is fixed in #56; this cluster is the remaining red and needs a focused migration-logic pass.

Activity

  1. jherr commented on Nov 30, 2026

    @jherr
    ContributorAuthor

    Investigation notes (deep-dive, not yet fixed)

    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.

  2. jherr commented on Nov 30, 2026

    @jherr
    ContributorAuthor

    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 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_data after doc:, 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):

    1. schema-registry.server.ts — pass an explicit DocumentIndex (no record fields).
    2. 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.
    3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions