Repository navigation
fix(driver-sql): a column's literal DEFAULT follows the field's declaration on an existing table - #36564
Conversation
…ration on an existing table Schema drift now reports a literal column DEFAULT the field no longer declares (drop_column_default) and one whose declared literal changed (set_column_default), both safe: applied by dev auto-reconcile, a publish, the artifact-pinned boot and os migrate apply, in place on Postgres and MySQL and by the table rebuild on SQLite. Claude-Session: https://claude.ai/code/session_01JQwxbY6wSVQZ9QBM4hLeYw Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbY6wSVQZ9QBM4hLeYw Co-authored-by: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JQwxbY6wSVQZ9QBM4hLeYw Co-authored-by: Claude <noreply@anthropic.com>
📓 Docs Drift CheckThis PR changes 2 package(s): 9 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 — 18 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 c2d02f9dd16befb38edc51bf477a065cb66da646 && git checkout c2d02f9dd16befb38edc51bf477a065cb66da646
# afterwards, rebuild it from the two parents, which stay fetchable
git fetch origin ad50d611a168c035e468fc41b5a9e550c9d8e265 3862c8b173bc737a3ed2fe8a9926ed5400d2a885 && git checkout -B drift-repro ad50d611a168c035e468fc41b5a9e550c9d8e265 && git merge --no-ff 3862c8b173bc737a3ed2fe8a9926ed5400d2a885
node scripts/docs-audit/affected-docs.mjs --json ad50d611a168c035e468fc41b5a9e550c9d8e265
|
|
repo:objectstack#1 · os-warren · session_01JQwxbZeHNsF3RWcWpj7rGj 维护者速读删掉或改掉字段默认值后,老库的列现在会跟着改:迁移计划里会出现「撤掉旧默认」或「改成新默认」两类安全项,三种数据库都实测修好,已有记录一行不动。 席位核对 — ACCEPT (L1) · repo:objectstack#1 · head
|
… never leaves a NOT NULL column refusing writes (objectstack-ai#36600) <!-- claude-projects-attribution: {"github_login":"os-warren"} --> _Requested by **Warren** · [project thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8Jy8bxCRdEndf2kW8g6WXtd)_ Fixes objectstack-ai#36596 Clause-②: no Metadata-protocol: none ## 维护者速读 - 修复 main 变红:字段删掉默认值后,MySQL 8 上不填该字段的新记录会被数据库拒绝,现在改为存空值,和 Postgres、SQLite 一致。 - 对「不允许为空」的列,删默认值不再单独执行:字段允许为空时和「放开非空」一起执行;字段仍要求非空时原样保留,只报出。 - 已有记录一行不改;改动可直接回退。 ## The cause (premise correction) The card says the dropped DEFAULT "leaves the column NOT NULL". Measured on MySQL 8.0.46, it does not: `digest` is `IS_NULLABLE = YES` throughout. The cause is MySQL 8's `ALTER COLUMN … DROP DEFAULT`, which leaves even a **nullable** column with no default at all, so a strict-mode insert that omits it fails with ER 1364 "Field 'digest' doesn't have a default value". `SET DEFAULT NULL` stores NULL. Round 1 (objectstack-ai#36564) was measured on MariaDB 10.11, which stores NULL after `DROP DEFAULT`, and the live MySQL leg did not run on that PR. ``` mysql> CREATE TABLE p (id varchar(36) PRIMARY KEY, digest varchar(255) DEFAULT 'none'); mysql> ALTER TABLE p ALTER COLUMN digest DROP DEFAULT; mysql> INSERT INTO p (id) VALUES ('a'); ERROR 1364 (HY000): Field 'digest' doesn't have a default value mysql> ALTER TABLE p ALTER COLUMN digest SET DEFAULT NULL; mysql> INSERT INTO p (id) VALUES ('a'); -- stores NULL ``` ## What changes `packages/drivers/driver-sql/**` only, plus this changeset. - **MySQL `drop_column_default`** (`sql-driver-schema-sync.ts`): on a nullable column it is now `ALTER COLUMN … SET DEFAULT NULL`; a NOT NULL column keeps `DROP DEFAULT`. This also covers the objectstack-ai#4560 token arm, which used the same statement. - **A NOT NULL column with a stale literal DEFAULT** (`schema-drift.ts`): the drop never travels alone, because an omitting insert would then be refused. - When the declaration does not hold the column NOT NULL (the differ already proposes `relax_not_null`: `storage: { notNull: false }` in words, or no storage declaration on a non-`required` field), the drop is reported **with** that relax at the relax's category, `needs_confirm`. `os migrate apply` applies both; the publish and the managed package install (objectstack-ai#33724) apply both when the field says `storage: { notNull: false }`. An unset insert then stores NULL. Dev auto-reconcile applies neither (ADR-0113 D5), so the column keeps storing the old default and is never left refusing writes. - Otherwise (`storage: { notNull: true }`, a `required` field, the ceremony-owned tenant column), the column is left alone. The entry is `destructive`, so no automatic or non-destructive path applies it, and it is warned at boot and listed by `os migrate plan`. - `isPublishedColumnChange` / `isInstallRelaxation` select the paired drop alongside the declared relax (`isDeclaredRelaxPair`). ## Tests `sql-driver-36245-stale-literal-default.test.ts`: §1 and §2 unchanged (not skipped, quarantined or narrowed); §2 now passes on live MySQL 8. New §3, per dialect cell (SQLite, live PG, live MySQL), on a NOT NULL column with `DEFAULT 'none'`: 1. No default, no storage declaration, not required: drift is `drop_column_default` + `relax_not_null`, both `needs_confirm`; dev boot leaves the column (unset insert stores `'none'`); `applyMigrationEntries` applies both; unset insert stores NULL; no drift after. 2. `storage: { notNull: false }`: `applyPublishedColumnChanges` applies both; unset insert stores NULL. 3. `storage: { notNull: true }`: one `destructive` entry; boot, publish and non-destructive apply leave the column NOT NULL with its default. 4. `required: true`, no storage: same as 3. ## Live PG + MySQL evidence (in-container, CI's own setup) Servers: MySQL 8.0.46 (`STRICT_TRANS_TABLES`, `innodb_strict_mode=1`, `SET GLOBAL time_zone='+08:00'`) and PostgreSQL 16 (`ALTER SYSTEM SET timezone='Asia/Shanghai'`). Run exactly as the "Temporal Conformance (live PG + MySQL)" step runs it: `TZ=America/New_York OS_EXPECT_LIVE_DIALECT_MATRIX=1 OS_TEST_POSTGRES_URL=… OS_TEST_MYSQL_URL=… pnpm --filter @objectstack/driver-sql test`. **Before (origin/main 79d9c14), the red reproduced on live MySQL 8:** ``` FAIL sql-driver-36245-stale-literal-default.test.ts > … objectstack-ai#36245 §2 … (live mysql) > the second boot drops the removed default … Error: insert into `os36245_pref` (`id`, `title`) values ('after', 'after') - Field 'digest' doesn't have a default value FAIL sql-driver-36245-stale-literal-default.test.ts > … objectstack-ai#36245 §2 … (live mysql) > without auto-reconcile the divergence is reported `safe` … Error: insert into `os36245_pref` (`id`, `title`) values ('repaired', 'repaired') - Field 'digest' doesn't have a default value ``` **After (head f972c24), the file, verbose:** 51 passed (51). Every §2 and §3 case passes on `sqlite`, `live postgres` and `live mysql` (18 two-boot cases, 6 per dialect); `driver-sql live-dialect coverage: all 3 dialects were exercised (sqlite, live postgres, live mysql)`. **After, the whole driver-sql suite under the CI step's setup:** ``` Test Files 1 failed | 280 passed | 1 skipped (282) Tests 1 failed | 7007 passed | 10 skipped (7018) FAIL sql-driver-11565-row-byte-budget.test.ts > … row byte budget on live MySQL (objectstack-ai#11565) > reports InnoDB page-limit refusals with the page limit, not the row budget ``` The one failure is not this PR's: it fails identically at origin/main 79d9c14 against the same local server (same command, main's `src/`), and passes on main's CI `mysql:8.0` image. The difference is this container's Ubuntu MySQL build, not the code: the case expects the server to refuse a new 40-column table, and this server accepts it. This PR touches no create path. **Ablations** (`scripts/ablation-replace.mjs`, each restored to HEAD, run on all three cells): | Mutation | Result | |---|---| | MySQL drop back to `DROP DEFAULT` on a nullable column | 4 fail: both §2 MySQL cases (ER 1364, the main-red failure), and the two MySQL §3 cases that relax then drop | | NOT NULL column drop emitted `safe` and unpaired, as on main | 9 fail across all three dialects | | Publish/install no longer select the paired drop | 3 fail: the `storage: { notNull: false }` publish case on each dialect | | Pair the drop even when the declaration holds NOT NULL | 6 fail: the `notNull: true` and `required` cases on each dialect | ## Follow-ups (not in this PR) - `content/docs/deployment/cli.mdx` (the publish paragraph under the drift-category table) says the publish applies "every `safe` column change, and a `relax_not_null` whose field declares `storage: { notNull: false }`". It now also applies the paired `drop_column_default`. That page is outside this card's file surface, so it is not edited here. - On MySQL, `relax_not_null` runs `MODIFY … NULL`, which also drops a column's declared literal DEFAULT (measured: `NOT NULL DEFAULT 'x'` becomes `DEFAULT NULL`); the objectstack-ai#36245 `set_column_default` arm restates it on the next sync. Pre-existing and unchanged here. - The "Temporal Conformance (live PG + MySQL)" job is path-filtered and skipped on PRs, which is how round 1 reached main. Making it run on a `driver-sql` PR is a `.github/workflows/ci.yml` change: governed (Tier H) and outside this card's file surface, so it is not made here. Evidence from in-container live servers is pasted instead. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01JQwxbY6wSVQZ9QBM4hLeYw <!-- signed: repo:objectstack#1 · os-warren · session_01JQwxbY6wSVQZ9QBM4hLeYw --> --------- Co-authored-by: Claude <noreply@anthropic.com>
Requested by Warren · project thread
Fixes #36245
Clause-②: no
Metadata-protocol: none
维护者速读
What changes
createColumnemits a field's literaldefaultValueas the columnDEFAULTonce, when it builds the column; the additive sync never revisits an existing column, and the differ readdefaultValueonly for runtime tokens (#4560). So a table an earlier release created kept the oldDEFAULTafter the field dropped or changed it, anddetectManagedDriftanswered[].schema-drift.ts:diffManagedTablegains a literal-default arm. A column carrying a literalDEFAULTunder a field that declares none reportsdrop_column_default; a literal that differs from the declared one reports the new opset_column_default. Bothsafe, kinddefault_mismatch, message ending with the sharedSAFE_COLUMN_REMEDY. Judged only wherecreateColumnitself emits a literal: tokens, object defaults, multi-value columns and physical expressions (NOW()defaults, sequences) are never touched, and a temporal literal is never compared (the dialect renders it its own way). Two pure helpers say what a physicalDEFAULTis (physicalDefaultLiteral) and when it already matches (physicalLiteralMatches); the spellings they read were measured on SQLite, Postgres 16 and MariaDB 10.11.sql-driver-schema-sync.ts:applyDriftOpInPlaceappliesset_column_defaultwithALTER TABLE … ALTER COLUMN … SET DEFAULTon Postgres and MySQL (the MODIFY-free form, likedrop_column_default), reading the declared literal at apply time and inlining it as the client escapes it; the SQLite rebuild honours it (it already re-emits every column's declared default).driver-turso:REMOTE_DRIFT_OP_ANSWERSanswersset_column_defaultasneeds-table-rebuild, as it doesdrop_column_default.Who applies the entries is unchanged: dev auto-reconcile, a publish of the object (
applyPublishedColumnChanges, which takes everysafecolumn entry), the artifact-pinned boot andos migrate apply. A plain production boot alters no existing column and logs the entry.Tests
src/sql-driver-36245-stale-literal-default.test.ts, throughdeclareDialectCellon every cell:safeandapplyMigrationEntriesapplies them.Readings (head 3862c8b unless noted)
SqlDriverboots over one database,digestdroppingdefaultValue: 'none'andchangedgoing'a'to'b': SQLite file, Postgres 16, MariaDB 10.11, with and withoutautoMigrate: 'safe'— every cell storeddigest: "none",changed: "a", anddetectManagedDriftanswered[].drift=[set_column_default changed, drop_column_default digest]without auto-reconcile; with it,digest: null,changed: "b",drift=[]on all three.initObjectswith 27 fields carrying string / quoted / empty / number / decimal / negative / boolean / select / text / date / time / datetime /NOW()/ json / multi-value /current_user/ URL-with-parentheses defaults reports no default entry on SQLite, Postgres or MariaDB.OS_TEST_POSTGRES_URL,OS_TEST_MYSQL_URLset): 39/39 passed.pnpm --filter @objectstack/driver-sql test(SQLite cells): Test Files 269 passed, 13 skipped; Tests 4539 passed, 262 skipped. With a live Postgres too: 4 failures, all the three-way timezone-skew guards (this local server runs in UTC; CI's is not), none in a default or drift suite.pnpm --filter @objectstack/driver-sql typecheck,pnpm --filter @objectstack/driver-turso typecheck: exit 0.pnpm --filter @objectstack/driver-turso test: 103 files passed, 2647 tests passed.if (dv === undefined || dv === null)→if (false as boolean)), committed fix first, on-disk mutation proven byscripts/ablation-replace.mjs(anchor 1 → 0, blob c1c48d28 → cb129bf6): 5 of the §2 cases red (expected 'none' to be null, the drift list missingdigest), restored blob == HEAD.return true→return falseon the text compare; blob c1c48d28 → 36666e6a): 9 red, including the kept-default assertiondetectManagedDrift(...) toEqual([])on every cell; restored blob == HEAD.Acceptance notes
os migrate apply; a plain production boot never alters an existing column (reconcileAndWarnDrift: "schema is never auto-altered in production").sys_notification_preferenceis a service's own object, so no publish or package install applies it either. Removing the hook now would bring back, on every upgraded production database where nobody runsos migrate apply, the silent digest opt-out ruling 7073270426 rejected; the only new thing is that the boot now logs the entry. Upgraded production databases get this fix throughos migrate apply, or through any of the drift-applying paths above (dev auto-reconcile, a publish, the artifact-pinned boot). The hook's removal, and repointing its comment, are carried by follow-up Remove the #35123 digest stopgap hook once column-default drift reaches upgraded production databases #36570 (Blocked-by this PR).DEFAULTon a column whose field declares none is now reported and, through the doors above, dropped — the card's ruling reverses the old "a hand-edited DEFAULT is a DBA's business" note onFieldDef.defaultValue, rewritten here.defaultValueafter its table exists (the column has noDEFAULT; restating one is outside the ruling's two verbs, and on MySQL a TEXT column cannot carry it); aNOW()default the field no longer declares (a physical expression, never touched); a temporal literal that changed (never compared). MySQL 8 reports a string literal bare; a bare all-capitals or multi-word value is read as an expression and left alone. MySQL 8 itself was not measured here (MariaDB 10.11 was); CI's live MySQL cell runs the new file.Verification
Two minutes:
OS_TEST_POSTGRES_URL=… pnpm --filter @objectstack/driver-sql exec vitest run src/sql-driver-36245-stale-literal-default.test.ts— expect 39 passed with both live URLs set, 35 on SQLite alone with the two live cells reported NOT RUN by name.Read first:
packages/drivers/driver-sql/src/schema-drift.ts(the literal-default arm indiffManagedTableandphysicalDefaultLiteral, where a misread spelling would drop a DEFAULT nobody asked to drop), thensetDeclaredColumnDefaultinsql-driver-schema-sync.ts(the inlined literal).Generated by Claude Code