Skip to content

fix(driver-sql): upsert() and paged reads honour a columnMap-ed key - #31378

Merged
objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-on4xyn
Nov 8, 2026
Merged

objectstack-fleet[bot] merged 4 commits into
mainfrom
claude/project-thread-on4xyn

Conversation

@objectstack-fleet

Copy link
Copy Markdown
Contributor

Requested by 田騏 · project thread

Fixes #31342

Clause-②: no
Metadata-protocol: none

维护者速读

  • 改了什么:外部对象用 external.columnMap 映射主键时,upsert() 的冲突目标(默认的和调用方显式传的 conflictKeys)、MySQL 的预检与落点探测、以及写完后的回读,现在都按映射后的远端列名生成;分页读取在调用方排序之后,用映射后的主键列做唯一的兜底排序。
  • 为什么改:以前 upsert 写的是 ON CONFLICT ("id")。远端表没有 id 列时每次都报错;远端表另有一个不是主键的 id 列时,已存在的键会报主键冲突而不是合并,新插入的行回读时会读到另一行,而且不报错。分页没有兜底排序时,并列的行在页与页之间可能重复或丢失。
  • 风险与回滚:没有 columnMap 的对象生成的 SQL 与之前完全一致;没有映射主键的外部对象分页行为不变,不猜列。只改 packages/drivers/driver-sql,两个 patch changeset。回滚就是还原这个 PR。
  • 先看:sql-driver-sequence-semantics.ts 里 upsert 的 conflictColumns;sql-driver-field-mapping.ts 的 paginationTieBreaker;sql-driver-external-columnmap-writes.test.ts 末尾两个 upsert 的 describe;sql-driver-external-columnmap-paging.test.ts。
  • 两分钟验证:pnpm --filter @objectstack/driver-sql exec vitest run src/sql-driver-external-columnmap-writes.test.ts src/sql-driver-external-columnmap-paging.test.ts,预期 13 个用例全部通过。
  • 需要你做的:无,席位落地。

What changes

Two defects, one commit and one patch changeset each, following #31349 on the same federated-key mapping. Nothing Oracle-specific: the paging fix is a port of the Oracle card's branch commit with its Oracle prose removed.

Upsert. upsert() derives conflictColumns (each conflict key through remoteColumn) and the key column once, and uses them wherever the statement or a probe names a column: the ON CONFLICT target, the no-op merge fallback, the plain-INSERT backing check, the MySQL pre-flight (assertConflictTargetHonoured, which compares against physical indexes), the MySQL fence and identity probes (assertMergeLandedInWrittenOrganization, assertMergeLandedOnSuppliedIdentity), and the read-back. The payload was already in column form (applyWriteColumnMap), and insertOnlyUpsertColumns already mapped the key. Refusals keep naming the fields the caller passed.

Paged reads. paginationTieBreaker returns id for a federated object whose columnMap maps a remote column to id, so a paged find() breaks ties on the mapped key after the caller's keys, as a managed table does. A federated object with no mapped key is unchanged: no column is guessed.

Why

ADR-0015 federation maps a field to a remote column. After #31349 the by-id doors and the analytic reads honoured it; upsert() and the pagination tie-breaker still did not. Measured on SQLite before the fix: on a table keyed CUST_NO, every upsert failed with no such column: id; on a table with its own surrogate id beside the mapped key CODE, upserting an existing key raised a primary-key violation, and upserting key 1 inserted it but answered the row whose surrogate is 1.

Verification

Measured on this branch (sqlite, the in-tree driver) at a6b16b4, and the pins re-run on the merge head cd627ef:

  • New pins red with the fix reverted, green with it applied:
    • upsert, 5 pins in sql-driver-external-columnmap-writes.test.ts: 5 of 5 fail with sql-driver-sequence-semantics.ts restored to the commit before the fix; the 5 by-id pins in that file stay green as controls.
    • paging, sql-driver-external-columnmap-paging.test.ts: 2 of 3 fail with sql-driver-field-mapping.ts restored to origin/main; the third is the no-mapped-key control and passes both ways.
    • All 21 columnMap pins (writes, paging, aggregate) pass with the fix.
  • pnpm --filter @objectstack/driver-sql test: 241 files passed, 12 skipped; 3888 tests passed, 218 skipped (the live PG and MySQL cells, which run in Temporal Conformance (live PG + MySQL) on main).
  • pnpm --filter @objectstack/driver-sql run typecheck: exit 0. (The first run caught Array#at in the ported test, TS2550 under this package's lib; replaced.)
  • eslint over the five changed source and test files: exit 0.
  • dispatch-gates --commands: 68 derived; all 68 run, every one exit 0; dispatch-gates --ran: 68 accounted, 0 NOT-MEASURED, 0 UNRUN.

Not measured locally: the MySQL-only probes now name the mapped key column; no MySQL server here, and the existing MySQL cells run on main.

Human Minimum

  • 1. Schemas start as Zod; packages/spec holds no runtime logic. (packages/spec not touched.)
  • 2. Every name I added uses the casing for its kind.
  • 3. No formatter ran; pnpm lint passes. (eslint over the changed files, exit 0.)
  • 4. One issue, one branch off an up-to-date main, one pull request.
  • 5. A change that publishes carries a changeset, and both declaration lines are filled in here and in the changeset. (@objectstack/driver-sql patch, two changesets.)
  • 6. packages/spec touched: its generated artifacts are regenerated and committed. (Not touched.)
  • 7. No hand edits to content/docs/references/, packages/*/CHANGELOG.md or content/docs/releases/.
  • 8. Title, body and comments are in English.
  • The Before Submitting list in CONTRIBUTING.md is done.

Generated by Claude Code

🤖 Generated with Claude Code

…der by that key

A paged find() orders by a unique tie-breaker so its pages partition the
result set. The tie-breaker was imposed only on tables this driver
created. A federated object whose external.columnMap names its key column
got none, so ties within the caller's sort had no order and a page walk
could repeat some rows and lose others. SQLite mostly hides this behind
rowid order.

The mapped key now breaks ties, after the caller's keys. An object with
no mapped key is unchanged: nothing is guessed.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
…through external.columnMap

The payload was mapped, but the conflict target, the MySQL pre-flight and
identity probes, and the read-back named the fields: ON CONFLICT ("id")
on a remote table keyed by another column. Without an id column every
upsert failed; with an unmapped surrogate id, an existing key raised a
primary-key violation and an inserted row read back as a different row.
They now name the mapped columns; refusals keep naming the fields.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
…ay#at

driver-sql's tsconfig lib predates es2022, so .at() is TS2550 there.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
@github-actions github-actions Bot added size/m documentation Improvements or additions to documentation tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface labels Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

📓 Docs Drift Check

This PR changes 1 package(s): @objectstack/driver-sql, touching 30 documentable anchor(s).

10 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:

  • content/docs/data-modeling/drivers.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/data-modeling/external-datasources.mdx (via remoteColumn (literal, a string literal in assertMergeLandedInWrittenOrganization; a string literal in assertMergeLandedOnSuppliedIdentity; a string literal in upsert))
  • content/docs/data-modeling/index.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/data-modeling/queries.mdx (via SqlDriver (symbol, a top-level class), applyTenantScope (literal, a string literal in assertMergeLandedInWrittenOrganization; a string literal in assertMergeLandedOnSuppliedIdentity; a string literal in upsert))
  • content/docs/permissions/tenant-audit-census.mdx (via SqlDriver (symbol, a top-level class), auditMissingTenant (literal, a string literal in upsert))
  • content/docs/plugins/packages.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/protocol/kernel/index.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/protocol/kernel/lifecycle.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/protocol/objectql/query-syntax.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/protocol/objectql/types.mdx (via SqlDriver (symbol, a top-level class))

⛔ 5 release-owned page(s) also name something this change touched. These are read-only:

  • content/docs/releases/v17/17-0.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/releases/v17/17-1.mdx (via applyTenantScope (literal, a string literal in assertMergeLandedInWrittenOrganization; a string literal in assertMergeLandedOnSuppliedIdentity; a string literal in upsert), injectTenantOnInsert (literal, a string literal in upsert))
  • content/docs/releases/v17/17-5.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/releases/v17/17-6.mdx (via SqlDriver (symbol, a top-level class))
  • content/docs/releases/v18/18-1.mdx (via SqlDriver (symbol, a top-level class))

content/docs/releases/ is RELEASE-OWNED (AGENTS.md "Documentation Guardrails"): release
notes are written centrally at release time, and a code PR that edits them is the exact PR
that guardrail exists to stop. They are still audited — read-only. If one of them is actually
wrong, file an issue or open a dedicated docs-only PR; do not edit it here.

What this run could not see
  • 1 name(s) were too generic to anchor anything (single lowercase words)
  • the SDK route bridge reached 64 of 227 client-bound route-ledger rows — the other 163 have no registrar path: tail to select them, so pages documenting THEIR client methods cannot appear above, on this or any run. Of those 163: 0 are remediable by widening that discovery convention (an in-repo file declares the path; the convention did not scan it); 59 are structural — on a ledger where NOT ONE row is declared in-repo, so no discovery change reaches them at any price; 104 are undecided (no in-repo declaration, on a ledger that has other in-repo registrars — absence and an unreadable spelling are not distinguishable here). The rows themselves: node scripts/docs-audit/affected-docs.mjs --bridge-coverage
  • a page that states a rule by its inputs shares no identifier with the emitter that implements the rule, so an emitter-only diff cannot list it — not on this run and not on any run. Measured on fix(driver-sql): emit varchar(maxLength) for a text field a declared index keys on #11430: content/docs/protocol/objectql/types.mdx documents the text-family column mapping by the ObjectQL type names it maps FROM (text / textarea / html) while the diff changed createColumn; it went unlisted, and it was the page that diff falsified, in four places. No shared token exists to detect this on, so a rule your change carries has to be re-read by hand in the pages that restate it.
  • a key NAME is not a key, so the hand re-read the line above prescribes can land on the wrong schema. The same spelling is authorable on one governed type and a [REMOVED] tombstone on another for each of active, aria, joins, objects, template, tools and version (censused on [finding] tools is a key on BOTH AgentSchema (tombstoned, dead) and SkillSchema (live, cloud-attested), so a name-based search attributes skill examples to the agent key — it produced a false stop-the-line alarm on PR #19059 #19093 over the liveness ledger's governed types, top-level keys); nothing in a search result distinguishes the two, so a grep hit on a LIVE example reads as evidence about the DEAD key. Measured on fix(spec): the agent.tools liveness row says dead — it claimed live on a key the schema tombstoned #19059: content/docs/ai/agents.mdx was reported as contradicting the agent.tools tombstone over its tools: example at :161, which is inside the defineSkill({ block opened at :155 — the page was already correct. Settle ownership by PARSING the value against both schemas, never by the name: that literal PASSES SkillSchema, and as an AgentSchema it FAILS at tools with the tombstone prescription. ⛔ These names are not the whole class — a key retired through a .strict() guidance map leaves no tombstone in the walked shape and none of them here (tool.category, live as AIToolDefinition.category).

Coarse fallback — 13 page(s) merely mention a changed package (the pre-#9192 predicate, kept for the deliberately-wide backstop): node scripts/docs-audit/affected-docs.mjs --json f4b4c8aeb567d1a47b56a1682679962e1add50b6 → packageMentionDocs.

Which tree this was computed on

This run read content/docs from 6b970ccd766567e666b8aa0e58c83b5555561591 — the merge of head cd627ef9eee887928951aff5a7c1529c0b8767f2 into base f4b4c8aeb567d1a47b56a1682679962e1add50b6, which is what actions/checkout gives a pull_request run. Not the PR head.

A worktree cut from an older main holds a different content/docs, so re-deriving there can legitimately return a different list — that is a different tree, not a wrong row. To answer on the same tree:

# while this PR is open — GitHub drops the merge commit once it closes
git fetch origin 6b970ccd766567e666b8aa0e58c83b5555561591 && git checkout 6b970ccd766567e666b8aa0e58c83b5555561591
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin f4b4c8aeb567d1a47b56a1682679962e1add50b6 cd627ef9eee887928951aff5a7c1529c0b8767f2 && git checkout -B drift-repro f4b4c8aeb567d1a47b56a1682679962e1add50b6 && git merge --no-ff cd627ef9eee887928951aff5a7c1529c0b8767f2

node scripts/docs-audit/affected-docs.mjs --json f4b4c8aeb567d1a47b56a1682679962e1add50b6

⚠️ That checkout carried uncommitted changes, so the commit above does not fully identify what was read.

Advisory only, and a precision-first one (#9192): a page is listed because it names a
symbol, wire route or SDK method this diff touched — not because it mentions a changed
package. Each row says which anchor put it there, so a wrong row is reportable rather than
merely annoying. To re-verify, run the docs-accuracy-audit workflow scoped to these files:
node scripts/docs-audit/affected-docs.mjs f4b4c8aeb567d1a47b56a1682679962e1add50b6 → pass the list as
args.docs, on the commit named under Which tree this was computed on.

@objectstack-fleet

Copy link
Copy Markdown
Contributor Author

epic:#27261#1 · marchtian · session_017ihL2c5z3q4h8inrn5kwqV
ACCEPT · PR #31378 @ cd627ef9 · 2026-11-08T05:03Z

维护者速读

席位意见:ACCEPT。无契约面:Clause-②: no,不碰 packages/spec/src/**,也不是 governed 路径。按 contract-review.md,这种情况的复核记录是 CI 加席位自读,本条即席位自读。无 must-fix。

席位核对

  1. Shape: +204/−18 across 7 files.
    • sql-driver-sequence-semantics.ts: upsert computes conflictColumns and idColumn once and uses them for the ON CONFLICT target, the no-op merge fallback, the plain-INSERT backing check, the MySQL pre-flight and fence probes, and the read-back. mergeKeys stays the field list for refusals.
    • sql-driver-field-mapping.ts: paginationTieBreaker returns id for a federated object whose map names id. It is a logical field, so the call site maps it.
    • A docblock in sql-driver.ts, two sqlite pin files, and one patch changeset per defect.
  2. Premise measured:
    • Upsert: 5/5 red with the fix reverted. The 5 by-id pins in the same file stay green as controls.
    • Paging: 2/3 red; the third is the no-mapped-key control.
    • 21/21 green on the merge head (os-dev-report 6717519239).
  3. Gate check-runs on cd627ef9: 37 runs at ACCEPT time: 9 success, 13 skipped, 15 in progress, none failed. Automerge waits for the required runs. Local: 68 derived gates, all exit 0. driver-sql suite: 3888 passed, 218 skipped.
  4. Seat read:
    • assertConflictTargetHonoured compares against introspected physical indexes, so passing columns is the right input. Nit, not blocking: on a mapped object its refusal text now names the remote column rather than the field.
    • The MySQL probe changes are compile-checked only, because no server was available. The existing MySQL cells on CI exercise unmapped objects, where both lists are identical.
  5. Out of scope: none. This closes the columnMap family that driver-sql: Oracle as a federation dialect — an external datasource over Oracle Database reads (and, double opt-in, writes) live, with type-compat and the predicate emitters modelling oracle #31328 surfaced.
  6. Landing: not governed (check-governed-merges --pr 31378; 222 lines ≤ 3000). The seat lands it through the relay.

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

Labels

documentation Improvements or additions to documentation size/m tests tooling Fix lands in tooling, not a product package — enters pm:queue only with Unblocks: or a named surface

Projects

None yet

2 participants