Repository navigation
fix(objectql): a server boot's schema sync takes the schema-migration lock, so replicas starting together create each table once - #31747
Conversation
A server boot, an OS_MIGRATE_AND_EXIT=1 job and os migrate apply now exclude one another on one database. ObjectQLPlugin.start() takes the lock on the default datasource's driver before Phase 1 and releases it in finally after Phase 3; never for a skipSchemaSync boot, or for a driver whose DDL os migrate holds back (SqlDriver#schemaDdlDeferred), which takes the lock itself. The lock module moves from the CLI to @objectstack/objectql, the lowest package holding a caller; the CLI re-exports it. Claude-Session: https://claude.ai/code/session_01ADHkmRuoKNTbmDhQPaG2vW Co-authored-by: Claude <noreply@anthropic.com>
…gration lock on live Postgres Also classifies SqlDriver#schemaDdlDeferred on the Turso remote face's member ledger (inherited: arming is refused there, so it reads false). Claude-Session: https://claude.ai/code/session_01ADHkmRuoKNTbmDhQPaG2vW Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift CheckThis PR changes 4 package(s): 13 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 — 56 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 ee71a0f3d639314dfb908adf4d01d739c25a08b9 && git checkout ee71a0f3d639314dfb908adf4d01d739c25a08b9
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin f65c36fcca8c33d38c62104c0888c950a315089a ab5e59107ea32f637f691e576af7a7d139af4311 && git checkout -B drift-repro f65c36fcca8c33d38c62104c0888c950a315089a && git merge --no-ff ab5e59107ea32f637f691e576af7a7d139af4311
node scripts/docs-audit/affected-docs.mjs --json f65c36fcca8c33d38c62104c0888c950a315089a
|
Move acquireBootSchemaMigrationLock and its JSDoc above the existing JSDoc of installRegisteredSchemas, so neither block is orphaned. Comment placement only; no behaviour change. Claude-Session: https://claude.ai/code/session_01ADHkmRuoKNTbmDhQPaG2vW Co-authored-by: Claude <noreply@anthropic.com>
The schema-migration lock asked every driver for its Knex to learn its dialect. The Turso remote transport refuses getKnex() by throwing, so a remote Turso boot failed in ObjectQLPlugin.start() before its schema sync. The lock now reads SqlDriver#dialectName first and calls getKnex() only for postgres or mysql; a Postgres or MySQL driver whose Knex cannot be handed out still fails loudly. Pins cover a SQLite face whose getKnex() throws (no lock, no throw, boot syncs) and the loud failure on postgres/mysql. The Turso ledger comment is corrected. Claude-Session: https://claude.ai/code/session_01ADHkmRuoKNTbmDhQPaG2vW Co-authored-by: Claude <noreply@anthropic.com>
|
domain:server#2 · os-steve · session_01ADHkmQKZzp4DLtqiCkZZRM 维护者速读
席位核对 — ACCEPT · domain:server#2 · Head-sha
|
Requested by Steve · project thread
Fixes #31715
Clause-②: no
Metadata-protocol: none
维护者速读
OS_MIGRATE_AND_EXIT=1)现在和os migrate apply拿同一把数据库迁移锁,只在建表期间持有,同步完立即释放。pg_type冲突,那个启动记下Schema sync FAILED却仍以 0 退出;实测可复现。packages/objectql/src/plugin.ts(acquireBootSchemaMigrationLock与 Phase 1–3 的 try/finally)、packages/objectql/src/schema-migration-lock.ts(从 cli 挪来)、packages/drivers/driver-sql/src/sql-driver.ts(新只读 getterschemaDdlDeferred)。OS_TEST_POSTGRES_URL=postgres://… pnpm --filter @objectstack/cli exec vitest run --project integration src/utils/schema-migration-lock.integration.test.ts,预期 8/8 通过,三个启动各打印一行等待、每张表只建一次。#31598 card item 2 (quoted)
As there: this PR changes the single-node server boot by one uncontended lock call (
pg_try_advisory_lock/GET_LOCKon a connection of its own, then the unlock), on Postgres and MySQL only. Nothing is printed when it is uncontended; the single-boot output was diffed byte for byte below.What changes
ObjectQLPlugin.start()Phase 1 (installRegisteredSchemas), Phase 2 (hydrate fromsys_metadata) and Phase 3 (sync what Phase 2 added).OS_MIGRATE_AND_EXIT=1is that same boot followed by an exit inserve.ts, so the lock goes into the engine's boot, not intoserve.ts.serve.tsis untouched.start()takes it right before Phase 1 and releases it infinallyafter Phase 3 (a boot that fails between the phases releases it too). It is never held while the server runs. It is taken even when nothing turns out to be pending, so the "read what is missing, then create it" race is closed. It is taken on the default datasource's driver, the database the tables live in (as fix(cli): os migrate apply takes a database lock so concurrent migrators run one at a time #31714 locks the primary).skipSchemaSync/OS_SKIP_SCHEMA_SYNC=1, and a default driver whose DDLos migrate plan/applyholds back. That command takes the lock itself before it reads what is pending; taking it in the boot too would make one migrator wait twice and spend a second uncontended statement. The engine reads this from a new read-only getter,SqlDriver#schemaDdlDeferred.schema-migration-lock.tsand its unit test moved frompackages/cli/src/utils/topackages/objectql/src/:@objectstack/objectqlcannot import@objectstack/cli(the CLI depends on it), and objectql is the lowest package that holds a caller of the lock (@objectstack/core/typesare driver-agnostic and call nothing here). It is exported from the objectql barrel (not the lean./coreentry).packages/cli/src/utils/schema-migration-lock.tsis now a re-export, sobootSchemaStackand the CLI tests import exactly what they did. Same nameobjectstack:schema-migrate, same Postgres key (its unit pin moved with it, unchanged).ab5e5910). The lock readsSqlDriver#dialectName(the driver's one public "which SQL do I speak") and callsgetKnex()only forpostgres/mysql. At86ea712bit asked every driver for its Knex first, and the Turso REMOTE transport refusesgetKnex()by throwing, so a remote Turso boot failed inObjectQLPlugin.start()(CITest Core (3/8),organization-ownership-boot-guard.turso-remote.kernel.test.ts). A Postgres or MySQL driver whose Knex cannot be handed out still throws; nothing is swallowed.bootSchemaStackpasses the sameSqlDriver, so the CLI path reads the same answer.ObjectQLPlugin.startupTimeout(60 s by default), so a boot queued behind a long migration refuses with the lock's own message (it names the lock and says nothing was changed) instead of the kernel's generic start timeout, and keeps the other half for its own sync.os migrate applykeeps its 10-minute bound.packages/drivers/driver-turso/src/turso-driver.tsgains one row inREMOTE_FACE_ANSWERS(schemaDdlDeferred: 'inherited'). That ledger is typedRecord<keyof SqlDriver, …>, so the new getter fails the Turso DTS build until it is classified. It isinheritedbecause the remote face refuses arming, so it always readsfalse.Tests
packages/objectql/src/plugin-boot-schema-migration-lock.test.ts(new, 6 pins): a realObjectKernelboot with a stub default driver whose knex andsyncSchemawrite into one ordered log. The lock comes before the first DDL and the unlock after the last; uncontended it is one lock statement and no line logged; contended it is one wait line, then DDL; a boot failing between the phases still unlocks; no lock forskipSchemaSyncor a deferring driver; control: SQLite and memory drivers run no statement and sync as before.packages/cli/src/utils/schema-migration-lock.integration.test.ts: a second top-level describe in fix(cli): os migrate apply takes a database lock so concurrent migrators run one at a time #31714's live-Postgres harness (ruling record 6583487846: one harness file per component). It holds the lock, starts threeos serveboots withOS_MIGRATE_AND_EXIT=1, waits until Postgres shows all three queued, then lets go. Asserted: all three queued; each exits 0 and prints exactly one wait line; none logsSchema sync FAILED; an event trigger sees each table created once; control: one more boot prints no wait line, creates nothing, exits 0. A named skip withoutOS_TEST_POSTGRES_URL, as the apply cell; the event trigger needs a superuser URL.schema-migration-lock.test.ts(header text updated for its second caller). Its fake drivers now carrydialectName; the dialect pins assertgetKnex()is never called forsqlite/mssql/oracle/unknown/ none; new pins: a SQLite face whosegetKnex()throws answersnullwithout calling it, and apostgres/mysqldriver whosegetKnex()throws rejects with that error.plugin-boot-schema-migration-lock.test.tsgains a 7th pin: a SQLite-dialect default driver whosegetKnex()throws (Turso remote) boots, is never asked for Knex, takes no lock and syncs.Acceptance notes
kernel:readyplatform-table repairs (metadata-protocolidentity-index migrations,IF [NOT] EXISTSindex DDL) run after the lock is released. In the 5 barrier-aligned runs and 2 staged integration runs with the fix, no boot logged a failure from them. The runtime resyncs (metadata:reloaded,ObjectQL.syncSchemason install) are not boot DDL and are not touched.Verification
Concurrency measurements at
3fa9769b(main4013e308merged in), Postgres 16 in the container.86ea712bmoved a JSDoc (no code change). Headab5e5910fixes the Turso remote boot:Red on main (b126c13), green with the fix: concurrent boots on one fresh database
DDL counted from the Postgres log (
log_statement = ddl); boots aligned by the barrier.Control: single-boot output unchanged
Timestamps, durations, ports, temp paths and database names normalized; nothing else.
Ablation: the lock call removed from ObjectQLPlugin.start()
scripts/ablation-replace.mjsreplacedconst migrationLock = await this.acquireBootSchemaMigrationLock(ctx);withawait Promise.resolve(null)(anchor 1 → 0, planted 1). Rebuilt objectql: the build exits 1 because the DTS step rejects the now-unused private method, but the JS was written:ablation-dist-preflight.mjs … --absentshows the call absent from all 8 built files.Packages, lint, gates