You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 7291c6f
Browse filesBrowse the repository at this point in the historyBrowse files
fix(driver-turso): the remote face gives a declared field's column the local face's DEFAULT (#37425)
<!-- claude-projects-attribution: {"github_login":"os-bill"} -->
_Requested by **Bill** · [project
thread](https://claude.ai/code/project/chan_016iMXWSZDzqWRWvWzdNo8wg?thread=cmsg_016iMXWSZDzqWRWvWzdNo8wg8ZBQe4UHadd2fGHGeWRW9V)_
repo:objectstack#5 · os-bill · session_01Utp6CbdHk3orAeensH6EzS
Fixes#37345
Clause-②: yes (widening)
Metadata-protocol: none (no key or value an author can write changes,
and `defaultValue` means what it meant: the remote face now writes the
column DEFAULT the local face already writes for the same declaration)
## 维护者速读
- 用户能看到什么变化:在托管的 Turso
环境里,给已有数据的对象新增一个"必填且带固定默认值"的字段,同步不再失败,老数据自动填上默认值;新建的表也和本地一样带上字段默认值。
- 为什么做:数据层同一驱动的本地与托管两种连接方式对同一字段定义给出不同结果,这次让托管一侧与本地一致。
-
出错会怎样:最坏情况是托管环境中某个对象的结构同步失败或建出的列默认值与本地不同,影响新增字段的那类同步;改动只在驱动建表语句里,可直接撤回。
- 要你做什么:无
## What changes
- **Remote face emits the declared column DEFAULT**
(`packages/drivers/driver-turso/src/remote-transport.ts`,
`columnDeclarationSQL`). Every `CREATE TABLE` and `ALTER TABLE … ADD
COLUMN` this transport sends now carries ` DEFAULT …` for a field
declaring a literal or `NOW()` `defaultValue`, nullable fields included,
exactly where the local face's `createColumn` →
`applyDeclaredColumnDefault` emits one (`sql-driver-column-creation.ts`,
the unconditional emitter call at the end of `createColumn`; none on the
multi-value short-circuit). So a `storage.notNull` field with a literal
default, added to a populated table, is accepted remotely with its NOT
NULL, as it is locally, and the existing rows take the default.
- **One source in `driver-sql`, no third copy.** New exports:
`emitsDeclaredColumnDefault` (the local predicate, already used by
`createColumn`; the remote mirror `declaresEmittedColumnDefault` is
deleted), `sqliteNowColumnDefaultSql` (the SQLite `NOW()` text, now what
`nowColumnDefault`'s SQLite branch wraps in `knex.raw`, byte-identical)
and `sqliteDeclaredColumnDefaultSql` (the DEFAULT text for a field, or
`null`). The literal rendering still runs through knex on the local face
(`col.defaultTo`); the shared renderer mirrors knex's SQLite output and
a new test pins the two faces to one `dflt_value` per kind.
- **Deliberate exception (scope amendment on the card): a `NOW()`
DEFAULT on `ADD COLUMN` to a populated table is never emitted.** SQLite
refuses a non-constant DEFAULT there; the local face sends it and fails
the sync (#37413, the local defect and this face's `NOW()` half). The
remote face keeps its earlier answer for that case: nullable is added
with no DEFAULT, `storage.notNull` is refused. The populated-table probe
now also fires when an added column declares a `NOW()` default
(`addsRowSensitiveColumn`, was `addsNotNullColumn`); otherwise a sync
adding only a nullable `NOW()` field would not know the table holds
rows.
- **A nullable field with a literal default, added to a populated remote
table, now fills the existing rows with the default**, as on the local
face (they read NULL before). Pinned by the parity test's populated
case.
- **Known limit, follow-up #37430:** the remote face cannot change a
column DEFAULT once written. An edited or removed literal `defaultValue`
gives `set_column_default` / `drop_column_default`, which
`REMOTE_DRIFT_OP_ANSWERS` answers `needs-table-rebuild`, and the remote
transport runs no rebuild; the stale DEFAULT stays and the finding stays
reported. The changeset says so.
- Docblocks: the recorded-asymmetry notes in `columnDeclarationSQL` and
in `turso-remote-not-null.test.ts` case 5 are removed and replaced by
the current contract and the `NOW()` pointer.
## Tests
- `turso-remote-not-null.test.ts` case 5 inverted: a `storage.notNull` +
literal default on a populated table gives one outcome on the local face
and through `syncSchemasBatch` and `syncSchema` (notnull 1, the row
reads the default, `tighten_not_null` empty, `detectManagedDrift`
empty). The `NOW()` half is recorded, pointing at #37413: the local face
refuses (nullable and `storage.notNull`); the remote face, through both
doors, accepts nullable with no DEFAULT and refuses `storage.notNull`.
- New `turso-remote-default-parity.test.ts` (#37345): per-kind reference
(string, embedded quote, number, negative, integer, boolean both ways,
date literal, select, `storage.notNull` literal, `NOW()` on datetime /
date / time / text, and none for multi-value, `current_user`, object);
the shared renderer gives each local `dflt_value`; `CREATE TABLE` parity
and zero drift (read by both faces) through `syncSchemasBatch`,
`syncSchema` and `initObjects`; `ADD COLUMN` to an empty table, `NOW()`
included, same; `ADD COLUMN` to a populated table: every literal emitted
and taken by the existing row, `NOW()` columns get none, zero drift. L1
rework added the renderer branches the first head left unpinned:
`toggle`, the empty string, a JSON-typed column with a scalar literal
(all in the per-kind set, so through every door and the drift read), and
a boolean declaring the string `'false'` in its own describe (`'0'` on
both faces and from the renderer, through all three doors; kept out of
the drift reads, see Acceptance notes).
<details><summary>Run readings</summary>
- Targeted: `pnpm --filter @objectstack/driver-turso exec vitest run
--maxWorkers=2 src/turso-remote-default-parity.test.ts
src/turso-remote-not-null.test.ts` → Test Files 2 passed, Tests 29
passed.
- `@objectstack/driver-turso` full suite (lock): Test Files 105 passed,
1 skipped; Tests 2671 passed, 36 skipped; VERDICT command-exit 0.
- `@objectstack/driver-sql` full suite + `typecheck` of both packages
(lock): Test Files 270 passed, 14 skipped; Tests 4551 passed, 265
skipped; both typechecks clean; VERDICT command-exit 0.
- Ablation 1, remote DEFAULT emission removed (`columnDeclarationSQL`
returns the declaration without ` DEFAULT …`, via
`scripts/ablation-replace.mjs`, anchor 1 → 0, blob f571509 → bb885d44):
10 failed / 19 passed, the inverted case 5 red through both doors and
every parity case red; restored, blob == HEAD, `git diff HEAD` empty.
- Ablation 2, the populated-table `NOW()` withholding removed: 4 failed
(parity populated case through both doors, case 5 `NOW()` record through
both doors); restored, `git diff HEAD` empty.
- Ablation 3, the probe trigger narrowed back to `storage.notNull` only:
2 failed (case 5 `NOW()` record through both doors); restored, `git diff
HEAD` empty.
- No `dist/` involved in ablations 1 to 3: the mutated file is
`driver-turso` source, which the suite imports directly.
- Rework head `f438ff8c`: targeted pair 32 passed. Ablation 4, the
`toggle` branch of the shared renderer removed in `driver-sql` source
(`ablation-replace.mjs --hold`, anchor 1 -> 0), `driver-sql` rebuilt,
`ablation-dist-preflight.mjs` found the marker in `dist/`: 9 failed / 3
passed (the reference row and every door case). Restored (blob == HEAD,
`git diff HEAD` empty), rebuilt, preflight `--absent` clean, targeted
pair 32 passed again.
</details>
## Acceptance notes
- A test file over a shared harness: the remote/local harness is copied
per file across the `turso-remote-*` suites (no shared harness file
exists), so the new pins live in their own file, as the dispatch order
names.
- `NOW()` on a remote `CREATE TABLE` and on `ADD COLUMN` to an empty
table now carries the expression DEFAULT, as locally; only the populated
`ADD COLUMN` withholds it. The drift differ never acts on an expression
DEFAULT, so neither shape reports drift.
- A boolean field declaring the STRING default `'false'`: both faces
write `'0'` (knex reads `'false'` as false), but the shared differ
compares a string declaration as text and reports `set_column_default`
drift on either face, which re-applying cannot settle. Pre-existing on
the local face, not introduced here; reported to the seat with this
round.
- An existing remote table created before this change keeps no DEFAULT
on its columns; the differ already leaves a column with no DEFAULT under
a declared one alone, so nothing new is reported for those databases.
## Verification
Read first: `packages/drivers/driver-turso/src/remote-transport.ts`
`columnDeclarationSQL` and `addsRowSensitiveColumn` (the `NOW()`
withholding and the probe trigger are the risk hunks);
`packages/drivers/driver-sql/src/sql-driver-schema-sync.ts`
`sqliteDeclaredColumnDefaultSql` / `sqliteNowColumnDefaultSql` (the
`nowColumnDefault` SQLite branch must stay byte-identical).
Two-minute check: `pnpm --filter @objectstack/driver-turso exec vitest
run src/turso-remote-default-parity.test.ts
src/turso-remote-not-null.test.ts` after building
`@objectstack/driver-sql`; expect 29 passed.
<details><summary>Local gates (os-dev scope: the derived list, CI runs
the farm)</summary>
- `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack`: 72 families derived for this diff, run at
`4084e2ee`. `--ran` with exit codes recorded: 72 derived, 69 run (all
exit 0), 3 NOT MEASURED, 0 unrun.
- NOT MEASURED: `check-plugin-teardown-shape.mjs --self-test` (reason: a
shallow clone, its pinned fixture commit is unreachable),
`check:dual-build-cjs-loads` and `check:lean-entry-closure` (reason:
they read built output of packages this diff does not touch; not built
locally). All three are left to CI.
- After merging `origin/main` once (head `2687d387`; main brought one
`packages/spec` constant): `driver-turso` closure rebuilt, `pnpm
--filter @objectstack/spec check:generated` reported all 14 artifacts up
to date, the targeted pair of files passed again (29 tests), and these
were re-run on the final head with exit 0: `check:type-check-coverage`,
`check:type-check-debt`, `check:driver-memory-census`,
`check:platform-object-tenancy-census`, `check:system-context-census`,
`check:nul-bytes`, `check:issue-citations`, `check:doc-authoring`.
- No exported symbol was renamed or removed (three added), so the
platform-checklist rename check does not apply. Rework head `f438ff8c`
(changeset and one test file): the families that read those files re-run
with exit 0: `check-adr-0087-registration --base origin/main`,
`check-changeset-no-major --base origin/main`, `check-empty-changeset
--base origin/main`, `check:changeset-gate-self-tests`,
`check:issue-citations`, `check:doc-authoring`, `check:nul-bytes`,
`check:cross-package-test-inputs`, `check:test-source-alias`,
`check-closing-keyword-parity`, plus `check:type-check-coverage` and
`check:type-check-debt`; `readClause2Line` on the changeset reads `yes`
/ `widening`. Full dogfood: not run (no `!` in the title and no
`(narrowing)` arm).
</details>
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- signed: repo:objectstack#5 · os-bill ·
session_01Utp6CbdHk3orAeensH6EzS -->
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(driver-turso): the remote face gives a declared field's column the DEFAULT the local face gives it
7
+
8
+
Clause-②: yes (widening)
9
+
Metadata-protocol: none (no key or value an author can write changes, and `defaultValue` means what it meant: the remote face now writes the column DEFAULT the local face already writes for the same declaration)
10
+
11
+
-**A `storage.notNull` field with a literal `defaultValue`, added to a table that already holds rows, now syncs on a hosted Turso/libSQL environment.** The remote face emitted no column DEFAULT for any field, so SQLite refused `ADD COLUMN … NOT NULL` there (`Cannot add a NOT NULL column with default value NULL`), and that object's whole sync failed through `syncSchema` and `syncSchemasBatch`; the local face accepted the same declaration. Both faces now add the column NOT NULL with the DEFAULT, and the existing rows take it.
12
+
-**A fresh remote table carries the same column DEFAULTs as a fresh local one**, on `CREATE TABLE` and on `ADD COLUMN`, for every field that declares a literal or `NOW()` default, nullable fields included. A multi-value column, a runtime token other than `NOW()` (such as `current_user`) and an object default get none, as on the local face. The shared drift differ reads a remote column as no drift.
13
+
-**A nullable field with a literal `defaultValue`, added to a remote table that already holds rows, now fills those rows with the default**, as on the local face; before, they read NULL.
14
+
-**Known limit: the remote face cannot change a column DEFAULT once it has written one.** If an author later edits or removes a literal `defaultValue`, the drift differ reports it (`set_column_default` / `drop_column_default`), but on the remote face both need a table rebuild, which the remote transport does not run. The column keeps the old DEFAULT, so an insert that omits the field stores the old value, and the finding stays reported. Tracked in #37430.
15
+
-**Not changed: a `NOW()` default on a column added to a table that already holds rows.** SQLite refuses a non-constant DEFAULT on that statement, so the remote face leaves it out and keeps its earlier answer: a nullable field is added with no DEFAULT, and a `storage.notNull` field is still refused. The local face currently refuses both; that defect is tracked separately.
16
+
- New `@objectstack/driver-sql` exports for drivers that write SQLite DDL as text: `emitsDeclaredColumnDefault` (which fields get a column DEFAULT), `sqliteDeclaredColumnDefaultSql` (that DEFAULT's SQLite text, or `null`) and `sqliteNowColumnDefaultSql` (the SQLite `NOW()` expression for a column type, the one the knex path uses).
0 commit comments