Skip to content

fix(catalog): INSERT missing nemar_catalog rows from datasets + enrichment_json - #543

Merged
neuromechanist merged 1 commit into
devfrom
fix/nemar-catalog-insert-missing
May 18, 2026
Merged

neuromechanist merged 1 commit into
devfrom
fix/nemar-catalog-insert-missing

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Follow-up to #541 (which UPDATE-only backfilled). 103 active rows on production were missing from nemar_catalog entirely, so the UPDATEs in migration 0023 couldn't reach them and the discover card kept rendering empty fields for them.

Why they were missing

catalog-sync.ts is the only pre-existing path that inserts into nemar_catalog, and it pulls from https://nemar.org/api/dataexplorer/datapipeline (legacy nemar.org). Datasets created via the new in-process pipeline (like nm000166) never feed back into legacy nemar.org, so catalog-sync never sees them, so they never get an initial nemar_catalog row.

Fix

Migration 0024: one-shot INSERT OR IGNORE that projects from the local source of truth (datasets.* typed columns + enrichment_json) for every active public dataset that doesn't yet have a catalog row. Handles both author shapes the enrichment emits (object-keyed and legacy array). After it runs, every active dataset has a matching nemar_catalog row and the discover card reads consistent data.

Idempotent (INSERT OR IGNORE no-ops on a fully-populated catalog).

Verification

Pre-migration: SELECT COUNT(*) FROM datasets d LEFT JOIN nemar_catalog c ON c.id = d.dataset_id WHERE c.id IS NULL AND d.status = 'active' → 103 missing.

Post-migration (expected): 0 missing. Live verify will assert nm000166's catalog row exists and has authors/modalities/etc. populated.

Follow-up tracked separately

The root architectural issue — catalog-sync.ts depends on legacy nemar.org as its source — should be addressed by an in-process catalog generator that builds nemar_catalog from datasets + enrichment_json directly. Without that, the same gap will recur for every dataset created via the new pipeline that hasn't reached the legacy nemar.org datapipeline. I'll file a tracking issue.

…hment_json

Migration 0023 UPDATE-only backfilled columns on existing nemar_catalog
rows but couldn't fill rows that didn't exist. catalog-sync.ts (the
legacy-nemar.org pull) was the only path that inserted them, so any
dataset created via the new in-process pipeline -- which doesn't feed
back into legacy nemar.org -- never got a catalog row. 103 active rows
were in this state on production today.

This migration creates the missing rows by projecting from the local
source-of-truth: datasets.* (typed columns) + enrichment_json (LLM
output, including authors as object-keyed or legacy array form). After
it runs, every active public dataset has a nemar_catalog row and the
list endpoint returns consistent values across the whole corpus.

INSERT OR IGNORE keeps the migration idempotent: re-running it on a
fully-populated catalog is a no-op.

Follow-up tracked separately: replace catalog-sync.ts's legacy-pull
with an in-process catalog generator so this gap doesn't recur for
future datasets.

Refs nemarOrg/website#1 (discover-card empty fields)
Refs #541 (UPDATE-only backfill).
@neuromechanist
neuromechanist merged commit e830770 into dev May 18, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the fix/nemar-catalog-insert-missing branch May 18, 2026 12:01
neuromechanist added a commit that referenced this pull request May 18, 2026
… pull (#546)

* feat(catalog): build nemar_catalog from local D1, no legacy nemar.org pull

Closes the architectural gap that #541 / #543 closed for historical
rows: catalog-sync.ts only knows how to pull from
https://nemar.org/api/dataexplorer/datapipeline, so any dataset
created via the new in-process pipeline never gets a catalog row
unless an operator runs a migration. Two changes make the local
path the source going forward.

1. Hot path: syncNemarCatalogFromEnrichment now UPSERTs.
   Was an UPDATE-only that silently logged 'no row exists' when a
   freshly-enriched dataset didn't have a catalog entry. New behavior:
   INSERT ... ON CONFLICT(id) DO UPDATE so the first enrichment of any
   new dataset auto-creates its catalog row. Requires a non-null
   'name' on the call site (nemar_catalog.name is NOT NULL); both
   callers (enrich-dataset.ts, dataset-reindex.ts) now thread name
   from the d1 row.

2. Bulk path: new backend/src/services/catalog-from-local.ts.
   - buildCatalogRecordFromLocal() pure projector: D1 row + parsed
     enrichment_json -> NemarCatalogRecord shape. Tested for every
     observed shape (object/array authors, null enrichment, on*
     mirrors, name/description fallback chain, search_text).
   - syncCatalogFromLocal() walks every active public dataset and
     INSERT-OR-REPLACEs the catalog row from local data. Batched to
     stay under D1 bound-param limits.
   - New POST /admin/catalog/sync-local endpoint triggers the sweep.

Legacy catalog-sync.ts stays in place; #544 tracks the cron switch
and eventual retirement. Migration 0023 + 0024 already healed the
historical state, and the new hot-path UPSERT + bulk endpoint mean
no future dataset can land in the empty-catalog state.

10 new unit tests for buildCatalogRecordFromLocal cover all the
shape branches plus null defaults. Typecheck clean.

Refs #544.

* review: address findings on #546

- syncCatalogFromLocal: switch INSERT OR REPLACE -> UPSERT
  (INSERT ... ON CONFLICT(id) DO UPDATE SET ...). INSERT OR REPLACE
  in SQLite is DELETE + INSERT which silently clobbered columns this
  path doesn't manage (readme, bids_version, sessions_count,
  latest_version, is_processed). Those are populated by the legacy
  catalog-sync.ts pull for ~70 datasets and would have gone NULL on
  every sweep. UPSERT preserves them.
- Add datasets.published_at to the SELECT, prefer it for publish_date
  on the LocalCatalogRecord. Was using created_at, which would push
  freshly-published rows to the top of newest-first ordering only by
  upload timestamp, ignoring the months-long gap between upload and
  publish for legacy datasets.
- New structural test test/catalog-from-local-sql.unit.test.ts pins
  the UPSERT choice and the deliberate column omission set so a
  future refactor can't silently regress to INSERT OR REPLACE or add
  a column without a real source for the value.
- New structural test test/catalog-upsert-sql.unit.test.ts pins
  syncNemarCatalogFromEnrichment's UPSERT + COALESCE-preserve on
  every updatable column. Catches any revert that would re-introduce
  the silent-no-op on missing rows (#544 root cause).
- Test fixture: add published_at + publish_date preference test.

18 unit tests pass.

This branch was previously deployed

1 inactive deployment
npm — fddbf324 Deployed May 18, 2026 by neuromechanist via publish #991
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant