Repository navigation
fix(driver-sql): upsert() and paged reads honour a columnMap-ed key - #31378
Conversation
…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
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2euiyuLXizr6FW32R3
📓 Docs Drift CheckThis PR changes 1 package(s): 10 hand-written doc(s) NAME something this change touched and may need an implementation-accuracy re-verification:
⛔ 5 release-owned page(s) also name something this change touched. These are read-only:
What this run could not see
Coarse fallback — 13 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 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
|
|
epic:#27261#1 · marchtian · session_017ihL2c5z3q4h8inrn5kwqV 维护者速读
席位意见:ACCEPT。无契约面: 席位核对
|
Requested by 田騏 · project thread
Fixes #31342
Clause-②: no
Metadata-protocol: none
维护者速读
external.columnMap映射主键时,upsert()的冲突目标(默认的和调用方显式传的conflictKeys)、MySQL 的预检与落点探测、以及写完后的回读,现在都按映射后的远端列名生成;分页读取在调用方排序之后,用映射后的主键列做唯一的兜底排序。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()derivesconflictColumns(each conflict key throughremoteColumn) and the key column once, and uses them wherever the statement or a probe names a column: theON CONFLICTtarget, 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), andinsertOnlyUpsertColumnsalready mapped the key. Refusals keep naming the fields the caller passed.Paged reads.
paginationTieBreakerreturnsidfor a federated object whosecolumnMapmaps a remote column toid, so a pagedfind()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 keyedCUST_NO, every upsert failed withno such column: id; on a table with its own surrogateidbeside the mapped keyCODE, upserting an existing key raised a primary-key violation, and upserting key1inserted 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:
sql-driver-external-columnmap-writes.test.ts: 5 of 5 fail withsql-driver-sequence-semantics.tsrestored to the commit before the fix; the 5 by-id pins in that file stay green as controls.sql-driver-external-columnmap-paging.test.ts: 2 of 3 fail withsql-driver-field-mapping.tsrestored toorigin/main; the third is the no-mapped-key control and passes both ways.pnpm --filter @objectstack/driver-sql test: 241 files passed, 12 skipped; 3888 tests passed, 218 skipped (the live PG and MySQL cells, which run inTemporal Conformance (live PG + MySQL)onmain).pnpm --filter @objectstack/driver-sql run typecheck: exit 0. (The first run caughtArray#atin the ported test, TS2550 under this package's lib; replaced.)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
packages/specholds no runtime logic. (packages/specnot touched.)pnpm lintpasses. (eslint over the changed files, exit 0.)main, one pull request.@objectstack/driver-sqlpatch, two changesets.)packages/spectouched: its generated artifacts are regenerated and committed. (Not touched.)content/docs/references/,packages/*/CHANGELOG.mdorcontent/docs/releases/.Generated by Claude Code
🤖 Generated with Claude Code