Repository navigation
Commit a2cb3aa
fix(driver-sql, objectql, metadata-protocol): publishing a field change on a live object brings its table to the declaration (#32848)
<!-- claude-projects-attribution: {"github_login":"os-sales"} -->
_Requested by **Sales** · [project
thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4Uu1SdwSJeacEmsneiyCVnM74)_
Fixes #32590
Clause-②: yes (widening) — new public
`SqlDriver.applyPublishedColumnChanges` and
`ObjectQL.applyPublishedColumnChanges`, a new `widen_date_to_datetime`
drift op, an optional `MetadataHostEngine.applyPublishedColumnChanges`
member and the new `object-column-changes-not-applied` advisory rule
Metadata-protocol: none
## 维护者速读
- 改了什么:对已上线对象发布字段变更时,表现在会跟着声明一起改。`date` → `datetime` 会把列扩宽(Postgres 改成
`timestamptz`,MySQL 改成 `DATETIME(3)`),声明 `storage: { notNull: false }`
会放开 NOT NULL。表跟不上的列变更会在发布回答里返回一条 warning advisory。
- 为什么改:以前发布只改元数据、不改表。`date` → `datetime`
之后写进去的时间会被丢掉(数据丢失);字段改成非必填之后,不带这个字段的新建仍然被数据库拒绝。
- 风险与回滚:只有发布对象时才会改表,而且只做不会丢数据的列变更(扩宽、放开约束、varchar
加宽、去掉运行时默认值)。启动、`syncSchema` 和生产环境的 boot 姿态都不变。Postgres
改列类型时,会在发布请求内持表锁重写整张表;等锁最多 5 秒,等不到就跳过这项变更,发布返回 advisory。回滚就是 revert 本
PR,已经扩宽的列保持扩宽,读写不受影响。
-
先看:`packages/drivers/driver-sql/src/sql-driver-schema-sync.ts`(`isPublishedColumnChange`
选哪些变更、`widenDateColumnToDatetime` 的 PG/MySQL
DDL);`packages/drivers/driver-sql/src/schema-drift.ts`(新的 `safe` 扩宽项、显式
`notNull: false`
项);`packages/metadata-protocol/src/protocol-registry-hydration.ts`(`ensureObjectStorage`
里的发布门调用和 advisory)。
- 两分钟验证:运行 `pnpm --filter @objectstack/runtime exec vitest run
src/domains/meta-object-publish-column-changes.test.ts`,应看到 3
passed(时间保留、不带标题的新建成功、number→text 返回 409)。
- 需要你做的:无,席位落地。
Branch: `claude/project-thread-i1qy0g` (the harness-assigned card-thread
branch).
## What changes
Publishing a changed object used to update its metadata and leave its
table as it was. Two changes that the field-type table
(`content/docs/data-modeling/field-type-decision-tree.mdx`) says "pass
cleanly" did not pass at the table. Both were reproduced on
better-sqlite3 and Postgres 16 through `syncSchema`, the call the
publish makes:
- **`date` → `datetime` on a table with rows.** Postgres and MySQL kept
the `date` column, so every time written after the publish was dropped
at the column. On SQLite the field stayed registered as a date, so the
read cut the value to its day. That registry was only ever added to, and
a retyped field ended up in both sets.
- **`required: true` → `false` with `storage: { notNull: false }`.** The
column kept its NOT NULL, so every create that omitted the field was
refused by the database.
The fix, per change kind as the table reads:
- **`@objectstack/driver-sql`.**
- `registerManagedObjectMetadata` and `registerExternalObject` now
rebuild the date, datetime and time registries from the declaration on
every registration.
- The differ has a new `widen_date_to_datetime` op, category `safe`, on
Postgres and MySQL only. SQLite has no temporal column type to widen.
- Its in-place arm re-reads the column first and alters it only while it
is still `date`:
- Postgres, in one transaction that bounds the lock wait (`SET LOCAL
lock_timeout`, 5 s), takes the table lock and re-reads the column, so a
second server applying the same publish finds nothing left to do: `ALTER
COLUMN … TYPE timestamptz USING (col::timestamp AT TIME ZONE 'UTC')`,
with the declared default restated. Behind a lock held longer, the entry
is skipped with a `warn` line naming the reason.
- MySQL, with no transaction (MySQL DDL commits implicitly): `MODIFY …
DATETIME(3)`, keeping nullability and the default.
- A NOT NULL column whose field declares `storage: { notNull: false }`
in words is now reported whatever `required` says, and its message names
the declaration. It stays `needs_confirm` (ADR-0113 D5).
- The new public `SqlDriver.applyPublishedColumnChanges(object)` diffs
one object and applies its `safe` column entries plus that explicit
relax. It never applies index entries, any other `needs_confirm` entry,
or a `destructive` one. It returns `{ applied, skipped }`.
- **`@objectstack/objectql`.** The new
`ObjectQL.applyPublishedColumnChanges(objectName)` calls that driver
door. Its body sits in `engine-compatibility-api.ts`, beside
`syncObjectSchema`. It answers `undefined` for a federated object or a
driver without the door. `syncObjectSchema` is unchanged, as is its spec
contract (`IObjectQLEngine.syncObjectSchema?(name): Promise<void>`,
"only create what is absent").
- **`@objectstack/metadata-protocol`.** `ensureObjectStorage` calls it
after the additive sync, so every door that publishes an object gets the
column half. `MetadataHostEngine` declares the member as optional. A
change the datasource cannot apply, or a failure while applying, has its
own `catch`: it is logged at `error` and answered as one
`object-column-changes-not-applied` warning on `advisories`, in the
shape of `object-storage-sync-refused`. The publish still succeeds.
- **`@objectstack/driver-turso`.** The REMOTE face's
`REMOTE_FACE_ANSWERS` row for the new member is `inherited`, since the
body reaches the face's own `detectManagedDrift` /
`applyMigrationEntries`. `REMOTE_DRIFT_OP_ANSWERS` answers the new op
`needs-table-rebuild`; the SQLite differ never emits it.
**SQLite path used:** there is no `ALTER COLUMN` on SQLite. Nothing
needs altering for `date` → `datetime`, because both store canonical
text; the registry rebuild is the fix there. The NOT NULL relax goes
through `applyMigrationEntries` → `rebuildSqliteTablePatched`, the
existing copy-into-a-patched-table rebuild that `os migrate apply`
already uses on SQLite.
**What the table says needs a migration is still refused at the save:**
`number` → `text` answers `409 DESTRUCTIVE_CHANGE` (`field_type_change`)
from `detectDestructiveObjectChanges`, unchanged and now pinned end to
end. `text` → `textarea` / `markdown` / `html` / `code` needs no column
change, because the text family shares one unkeyed `TEXT` column.
## Tests
-
`packages/drivers/driver-sql/src/sql-driver-published-column-changes.test.ts`
(36 cases). Differ half: every dialect's spelling, including the silent
controls (no SQLite widen, no false positive over `timestamptz`, the
undeclared NOT NULL shapes unchanged). Live half: SQLite, live Postgres
and live MySQL cells through `declareDialectCell`. Each cell pins:
- the additive sync alone changes nothing (non-vacuity);
- exactly the expected entries are applied and nothing is skipped;
- the columns follow the declaration;
- an ISO datetime written after the publish reads back with its time;
- a day stored before the publish keeps its day;
- a create without the field succeeds;
- the undeclared NOT NULL is left alone;
- a second publish finds nothing to apply.
A live Postgres case holds the table from another session: the publish
gives up after the 5 s bound, reports the widening skipped, logs the
reason and leaves the column `date`; once the lock is released, the next
publish applies it.
-
`packages/runtime/src/domains/meta-object-publish-column-changes.test.ts`
(3 cases). Draft save, then `publishMetaItem`, over the real protocol,
engine and SQL driver:
- `date` → `datetime` keeps the time;
- `required` → `false` with `storage.notNull: false` lets a create omit
the field;
- `number` → `text` is refused 409 at the save, with the table and the
served object unchanged.
- `packages/objectql/src/federated-injected-column-readers.test.ts`: one
census row for the new body's `isFederatedObject()` use.
## Acceptance notes
- **ADR-0113 D5 reading.** The explicit `storage: { notNull: false }`
entry keeps D5's `needs_confirm`, so dev `autoMigrate: 'safe'` still
never applies it at boot. Only the publish of that declaration (the
deliberate relax D5 names) and `os migrate apply` apply it. `os migrate
apply` already applied `needs_confirm` together with `safe`, which is
unchanged. A NOT NULL with no `storage.notNull` written either way is
NOT relaxed by the publish. Example: v1 `storage: { notNull: true }`,
then v2 omits `storage`. That column keeps its NOT NULL and the boot
keeps warning `needs_confirm` as before; the publish answer does not
mention it.
- **Same class, applied by the same door:** the publish also applies the
existing `safe` column entries for the published object, `widen_varchar`
and `drop_column_default`.
- **Lock and time.** On Postgres the type change rewrites the table
inside the publish request under `ACCESS EXCLUSIVE`, so a large table
makes that publish as slow as the rewrite. The wait for that lock is
bounded at 5 s (`SET LOCAL lock_timeout`), because a waiting ACCESS
EXCLUSIVE request queues every later statement on the table behind it.
Behind a session holding the table longer, the widening is skipped with
a `warn` line naming the reason, the publish answers its
`object-column-changes-not-applied` advisory, and the next publish or
`os migrate apply` applies it. The door goes through the public
`applyMigrationEntries` so the Turso REMOTE face answers for itself.
Like any explicit run, that closes the concurrent-index deferral window
(#31901), so the next concurrent build waits its bound once more.
- **Stored days become midnight UTC.** This is explicit on Postgres (`AT
TIME ZONE 'UTC'`, so the session zone cannot move it) and native on
MySQL's zone-naive `DATETIME`. Measured with the process in
`America/New_York` and both servers at `+08:00`.
- **Turso REMOTE face:** an entry that needs a table rebuild (a SQLite
`relax_not_null`) comes back `skipped` and reaches the advisory and the
`error` line. The face's row is pinned by
`turso-remote-inherited-members.test.ts`; that end-to-end outcome on a
remote transport is not pinned by a new test.
- **Boot order:** a boot that runs before the publish still prints the
previous state's drift warning once. The publish then applies the
change.
- **Out of scope, reported to the seat (not fixed here):** `datetime` →
`date`, the reverse change the table also says passes cleanly, reads a
day early on Postgres when the server zone is ahead of UTC. A value
written `2026-11-12` after the change reads back `2026-11-11`; the
column stays `timestamptz`. The result is identical on the base commit,
so it predates this diff. SQLite and MySQL read correctly.
## Verification
Head `038057cd`, the review round's commit on top of `58501571`. The
branch was merged with `origin/main` at `2f80f289` before this PR and
has not chased main since; #32690 is in that base.
**Reproduced before the fix.** Base `ee230a25` (two commits after the
claim's `9af07668ed`), through `syncSchema` v1 → v2, the storage call
the publish makes:
- better-sqlite3: an update to `2026-11-13T01:00:00.000Z` read back
`2026-11-13`. A create without `title` was refused
`SQLITE_CONSTRAINT_NOTNULL`.
- Postgres 16: `start_date` was still `date` after the v2 sync, and the
same update read back `2026-11-13`. The create was refused `23502`
(not-null violation on `title`).
**Rig.** Live Postgres 16 and MySQL 8.0.46 on this box, both servers at
`+08:00`. The test process ran with `TZ=America/New_York` and
`OS_EXPECT_LIVE_DIALECT_MATRIX=1`, so a live cell cannot skip.
**Pins on this head.**
- `pnpm --filter @objectstack/driver-sql exec vitest run
src/sql-driver-published-column-changes.test.ts`: 36 passed (sqlite,
live pg and live mysql cells, plus the live pg lock case, which waits
out the 5 s bound).
- `pnpm --filter @objectstack/runtime exec vitest run
src/domains/meta-object-publish-column-changes.test.ts`: 3 passed. The
same three cases also passed against live Postgres through a temporary,
uncommitted copy of the file that pointed its driver at Postgres; the
copy was deleted and is not in this diff.
**Full suites,** after the main merge and a full `pnpm build` (119 of
119):
- driver-sql with live pg and mysql: 6375 passed, 1 failed, 5 skipped on
`038057cd`. The one red is `sql-driver-11565-row-byte-budget` "reports
InnoDB page-limit refusals with the page limit, not the row budget" on
live MySQL. It fails the same way on the base `ee230a25` source with
this box's Ubuntu MySQL 8.0.46, so it is environmental and not this
diff.
- On `58501571`: objectql 12591 passed. metadata-protocol 22786 passed,
36 skipped. runtime 10250 passed, 19 skipped. The review round changes
only driver-sql source, its pin and the changeset, so on `038057cd`
driver-sql was rebuilt and its suite above, driver-turso (2523 passed,
34 skipped) and the runtime pin (3 passed) were run again.
- `typecheck` for driver-sql, driver-turso, objectql, metadata-protocol
and runtime (runtime's includes `check:test-typecheck`): 0 errors;
driver-sql again on `038057cd`: 0 errors.
**Ablations.** Each one put the old behaviour back with
`scripts/ablation-replace.mjs`, ran the pin and saw it go red, then
restored. Each restore printed blob == HEAD with `git diff HEAD` empty.
A1 to A4 and B1 ran on `58501571`; A5 ran on `038057cd`.
| # | Old behaviour put back | File | Red |
|---|---|---|---|
| A1 | the explicit `storage.notNull: false` relax is not selected
(`return false && …`) | `sql-driver-schema-sync.ts`
`isPublishedColumnChange` | 12 of 34 |
| A2 | the Postgres / MySQL widen arm never runs |
`sql-driver-schema-sync.ts` `applyDriftOpInPlace` | 10 of 34 |
| A3 | the date registry accumulates again | `sql-driver-rotation.ts` |
6 of 34 |
| A4 | the differ's widen branch never matches | `schema-drift.ts` | 10
of 34 |
| B1 | the publish never calls the door (`columns = undefined;`) |
`protocol-registry-hydration.ts` | 1 of 3 (runtime) |
| A5 | the Postgres lock wait is unbounded again (`lock_timeout` set to
`'0'`) | `sql-driver-schema-sync.ts` `widenPostgresDateColumn` | 2 of 36
|
B1 went through the built package. `ablation-dist-preflight.mjs` proved
the call absent from all 22 files in `metadata-protocol/dist`. After the
restore and rebuild it proved the call present again, and the three
cases were green. Only the NOT NULL case goes red there. On SQLite the
`date` → `datetime` half is the registry rebuild, which runs on the
additive sync and not through the door. A3 holds that half on all three
dialects, and A2 and A4 hold the column widening on Postgres and MySQL.
**Driver conformance ledger.** `node
scripts/check-driver-conformance.mjs`, before the change and on this
head: `66 covered cell(s), 6 in the DEBT ledger, 0 exempt`, and dialect
axis `11 of 11`. Nothing moved. This diff adds no case-set and touches
no ledger row.
**Gates.**
- On `038057cd`, for the review round's three files: `node
scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack <those paths>` derived 66 families. All 66
exited 0, and `--ran` answered `66 derived famil(ies) accounted for — 66
run, 0 NOT-MEASURED`.
- On `58501571`, for the whole diff: 73 derived families, all exited 0,
`--ran` `73 run, 0 NOT-MEASURED`.
- `pnpm lint`: exit 0, and `node scripts/check-nul-bytes.mjs`: exit 0,
both on `58501571`.
- `check-plugin-teardown-shape --self-test` needs its pinned fixture
commit in the shallow clone. It was fetched first (`git fetch --depth=1
origin 621a487`), so the family is a reading, not an exit 3.
**Merge before the queue.** `origin/main` at `5fb79bc0` was merged once
more as `b67fa33a` (first-parent K = 18 since `2f80f289`, with
language-pack changes in between), with no conflict. On `b67fa33a`: a
full `pnpm build` (119 of 119). `dispatch-gates --commands` for this
PR's six files in the packages main also moved (metadata-protocol,
objectql, runtime) derived 62 families; all 62 exited 0, and `--ran`
answered `62 run, 0 NOT-MEASURED`. metadata-protocol 22790 passed, 36
skipped; objectql 12615 passed; typecheck for metadata-protocol,
objectql and runtime 0 errors; pins 36 of 36 (driver-sql, live pg and
mysql) and 3 of 3 (runtime). CI on `b67fa33a` finished with no failure.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01L6bhgWwN7doasrBSHNAPGB)_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_01L6bhgWwN7doasrBSHNAPGB
<!-- signed: domain:server#4 · os-sales ·
session_01L6bhgWwN7doasrBSHNAPGB -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent b811f6a commit a2cb3aa
14 files changed
Lines changed: 1057 additions & 17 deletions
File tree
- .changeset
- packages
- drivers
- driver-sql/src
- driver-turso/src
- metadata-protocol/src
- objectql/src
- runtime/src/domains
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
191 | 212 | | |
192 | 213 | | |
193 | 214 | | |
| |||
1087 | 1108 | | |
1088 | 1109 | | |
1089 | 1110 | | |
1090 | | - | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1091 | 1144 | | |
1092 | 1145 | | |
1093 | 1146 | | |
| |||
1342 | 1395 | | |
1343 | 1396 | | |
1344 | 1397 | | |
| 1398 | + | |
| 1399 | + | |
| 1400 | + | |
| 1401 | + | |
| 1402 | + | |
| 1403 | + | |
| 1404 | + | |
| 1405 | + | |
| 1406 | + | |
| 1407 | + | |
| 1408 | + | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
| 1419 | + | |
| 1420 | + | |
| 1421 | + | |
| 1422 | + | |
| 1423 | + | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
1345 | 1438 | | |
1346 | 1439 | | |
1347 | 1440 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
0 commit comments