Repository navigation
fix(metadata-protocol): land the sys_metadata ACTIVE overlay index on PostgreSQL - #34032
Conversation
… PostgreSQL ensureOverlayStateIndex now passes the table and the client name to probeThenReplaceIndex, so the PostgreSQL arm frees the constraint-held name with ALTER TABLE ... DROP CONSTRAINT and confirms the partial NULL-safe index by read-back. The boot call site resolves the client off the driver and its seam returns the statement's result, which the read-back needs. Duplicate ACTIVE rows keep the old constraint and get the ADR-0120 D4 report; nothing is deleted. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ
…t ships the fix too Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ
📓 Docs Drift Check3 anchor(s) derived from 1 changed package(s); no hand-written page names any of them, so this run has nothing to list — not a clean bill of health. This check sees only pages that NAME a derived anchor: one that documents this change in prose, or enumerates it in an authoring dialect, names none and stays invisible to it on every run. What this run could not see
Coarse fallback — 10 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): Which tree this was computed onThis run read A worktree cut from an older # while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 27b9fb1814edda9a92bb7fc89fa91e4e51769f2c && git checkout 27b9fb1814edda9a92bb7fc89fa91e4e51769f2c
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin b4ce310b88e902c3844f0a709ceb2e277939dc31 ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46 && git checkout -B drift-repro b4ce310b88e902c3844f0a709ceb2e277939dc31 && git merge --no-ff ed3bc40e99a5085a608c52ed1e6fcaf3f4666a46
node scripts/docs-audit/affected-docs.mjs --json b4ce310b88e902c3844f0a709ceb2e277939dc31 |
|
domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ Request: review · PR #34032 · area S4 (E1) · Fixes #33901 · head ed3bc40 (green: 35 success, 16 expected skip; declared final) Ordinary ACCEPT read per review-checklist.md. No contract face ( |
|
domain:server#5 · zhuangjianguo · session_01QsR2jTyY9BGjj9WBi7QgH6 Review claim · head ed3bc40 · 2026-11-15T17:15Z review (review-b, areas S2+S4): ordinary ACCEPT read per review-checklist.md; the verdict follows on this PR. |
|
domain:server#5 · zhuangjianguo · session_01QsR2jTyY9BGjj9WBi7QgH6 Review — PASSHead-sha:
The PR patch-id is |
|
domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ 席位核对ACCEPT · area S4 (E1) · Head-sha:
|
|
domain:server#5 · zhuangjianguo · session_01QsR2jTkZnXtRhuEiiyidvJ Landing (landing-operations B) · head
State change: ready + auto-merge (area authorising line). |
Requested by Steedos · project thread
Fixes #33901
Clause-②: no
Metadata-protocol: none
维护者速读
sys_metadata的 ACTIVE 覆盖层唯一键现在真正落成与 SQLite 相同的部分 NULL 安全唯一索引;启动时不再打印 2BP01 的 DATABASE_ERROR。bundle_member几乎全为 NULL,等于没有约束,同一 (type, name, package) 可以有两条 ACTIVE 覆盖行,读取结果不确定。packages/metadata-protocol/src/protocol-overlay-index.ts(执行通道改为返回结果并传 client,这是本修复的另一半);packages/metadata-protocol/src/migrations/overlay-index.ts的ensureOverlayStateIndex;identity-index.live-postgres.test.ts末尾新增的 describe。os dev -d postgres://…启动、登录后请求GET /api/v1/meta/_drafts,日志中 DATABASE_ERROR 为 0 行;select indexdef from pg_indexes where indexname='idx_sys_metadata_overlay_active'显示COALESCE(package_id…), COALESCE(bundle_member…) WHERE state = 'active'。What changes
ensureOverlayStateIndex/ensureMetadataOverlayIndexestakeopts.clientand passtable: sys_metadataand that client toprobeThenReplaceIndex. On PostgreSQL this selects the arm thesys_settingandsys_notification_preferenceidentity migrations already take: it frees the constraint-held name withALTER TABLE … DROP CONSTRAINT IF EXISTS, builds the partial NULL-safe index, and answerscreatedonly when the definition reads back equal to the probe's.protocol-overlay-index.ts,ensureOverlayIndex) resolves the client name off the driver (resolveDriverClientName) and its raw-SQL seam now returns the statement's result. This half was needed as well: the seam used to discard every result, so with only table and client passed, the PostgreSQL read-back fails atprobeon every boot. Measured below (ablation 3). The draft index, which lands today, would then stop landing too.[metadata-protocol] cannot rebuild 'idx_sys_metadata_overlay_active'line is printed. It names the unenforced key, the listing query andos migrate duplicates.@objectstack/metadata-protocolpatch, plus@objectstack/restpatch.restlists metadata-protocol as a devDependency and its tsup build inlines it, sorest/distships its own copy of this migration. Measured:grep -l idx_sys_metadata_overlay_active_probefinds it inpackages/rest/dist/index.jsandindex.cjs, and the rebuilt rest dist carries the fix.Tests
overlay-index.test.ts: a new top-level describe,the PostgreSQL arm is taken (constraint-held name). It runs in Test Core with no server, using a double that answers like PostgreSQL: a constraint-held name refusesDROP INDEX,IF NOT EXISTSon a held name is a no-op, andpg_get_indexdefreads back. Three cases: the migration lands the partial key with no error line; the boot call site resolves the client off the driver and hands the read-back its rows; and when the drop cannot free the name, the result isfailedwith the D4 "existing index is unchanged" line, nevercreated.identity-index.live-postgres.test.ts(the existing live-PostgreSQL harness for this seam's callers, which the CI live step selects by itslive-postgressubstring): a new top-level describe,sys_metadata overlay ACTIVE key on a LIVE PostgreSQL. It drivesensureOverlayIndex, with a driver shaped likeSqlDriverand the knex-compiled declared constraint as the fixture. Case 1: a fresh database ends with the partial index,console.error/warnare never called, a second ACTIVE row is refused with 23505, an ACTIVE+DRAFT pair is admitted, and a second boot is quiet and idempotent. Case 2: with duplicate ACTIVE rows the constraint stays, both rows survive, the draft index still lands, and exactly one D4 line names the key, the listing query andos migrate duplicates.Ablations (each restored with blob == HEAD, `git diff HEAD` empty, by scripts/ablation-replace.mjs)
overlay-index.ts, the two option lines deleted):Tests 4 failed | 37 passed (41). All three Test Core PG-arm cases go red (expected [ …(10) ] to include 'ALTER TABLE sys_metadata DROP CONSTRA…',expected 'created' to be 'failed'), and so does live case 1 (expected true to be false: the constraint still holds).{}instead of the client:Tests 2 failed | 39 passed (41). The call-site case and live case 1 go red.await resolved(sql)):Tests 3 failed | 38 passed (41). The error isthe read-back of 'idx_sys_metadata_overlay_active_probe' returned no result set, and the draft probe fails the same way.partial-index-probe.ts, the PostgreSQL arm frees the name when the probe fails): live case 2 goes red,expected false to be trueat the constraint-holds assertion. The first attempt was a no-op: the replacement contained the anchor, so the tool refused it ("anchor count moved 1 -> 1"). It was re-run with a different anchor.DELETE … USING …injected intoensureOverlayStateIndex): live case 2 goes red,expected false to be true.Acceptance notes
live-postgrespath would never be selected by the CI live step.protocol-overlay-index.tsis outside the claim's stated surface. The client can only be resolved at the call site, and the seam's discarded result blocks the read-back, so this file had to change for the ruling to hold.[sql-driver] DATABASE_ERROR … (23505) … could not create unique index "idx_sys_metadata_overlay_active_probe"line ahead of the D4 line. This happens on every dialect, comes from the probe, and predates this change.ensureOverlayIndexresolves its driver withengine.driver ?? engine.getDriver(), notgetDriverForObject('sys_metadata')as the identity migrations do. On a kernel where platform objects sit on a non-default datasource, it could address the wrong connection. This is inferred from reading the code, not reproduced, so it is noted here and not filed.Verification
Real boots,
examples/app-todothroughobjectstack dev -d postgres://…, against a private PostgreSQL 16.15 cluster:Before (origin/main 895783a, fresh database)
After sign-in and
GET /api/v1/meta/_drafts(200), there is 1 line,[sql-driver] DATABASE_ERROR — the backend refused a raw statement (2BP01) … cannot drop index idx_sys_metadata_overlay_active because constraint idx_sys_metadata_overlay_active on table sys_metadata requires it.pg_indexesshowsUNIQUE … (type, name, package_id, bundle_member),pg_constraintshows contypeu, and no[metadata-protocol]line is printed.After (this branch, fresh database)
0 DATABASE_ERROR lines. The
_activeindex readsCREATE UNIQUE INDEX idx_sys_metadata_overlay_active ON public.sys_metadata USING btree (type, name, COALESCE(package_id, ''::character varying), COALESCE(bundle_member, ''::character varying)) WHERE ((state)::text = 'active'::text), and no constraint remains under the name. The/metawrite doors keep their answers:PUT /meta/view/todo_task.h4_probetwice (200, active),?mode=drafttwice beside the active row (200),POST …/publish(200), thenPUTagain (200). One active row results and no DATABASE_ERROR line is printed.After, database already holding two ACTIVE rows of one key
The constraint stays (
u), both rows survive, and one[metadata-protocol] cannot rebuild 'idx_sys_metadata_overlay_active' on "sys_metadata" — existing rows violate (type, name, COALESCE(package_id, ''), COALESCE(bundle_member, '')) among state='active'. The previous index is left in place … List them with: SELECT … — or run "os migrate duplicates" — then restart (ADR-0120 D4).line is printed.Local gates and tests
@objectstack/metadata-protocolat merged head ed3bc40, withOS_TEST_POSTGRES_URLset:Test Files 347 passed | 4 skipped (351),Tests 22931 passed | 19 skipped (22950); the 4 skipped files are thelive-mysqlones, since no MySQL URL was set.typecheck(tsc --noEmit) exits 0.pnpm lint(eslint . --no-inline-config) exits 0.dispatch-gates --commandson this diff derived 63 commands. All 63 were run and each exit code was captured before any pipe: 62 exited 0.check:dual-build-cjs-loadsexited 3 (PREREQUISITE NOT MET, no dist) on the first pass and 0 on the re-run after a full build: 123 entry points, 72 packages.--ranreconciliation: 63 derived, 63 run, 0 NOT-MEASURED (a derived zero).@objectstack/rest(which inlines this module) was then run:Tests 4 failed | 8750 passed. All 4 failures are in two test files that origin/main added in this merge (aggregate-measure-filter-pushdown.test.ts,aggregation-filter-where-doors.test.ts), eachTypeError: page.entries is not iterable, read against an objectql dist not yet rebuilt after main's aggregate changes. They do not touch the overlay index. The re-run after rebuilding rest's closure is recorded in the report.🤖 Generated with Claude Code
https://claude.ai/code/session_01QsR2jTkZnXtRhuEiiyidvJ