Repository navigation
Commit b572420
fix(driver-sql): a column's literal DEFAULT follows the field's declaration on an existing table (#36564)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8Jy8bxCRdEndf2kW8g6WXtd)_
Fixes #36245
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 字段删掉或改掉默认值后,老数据库的这一列也跟着改:开发环境重启、发布对象、制品启动、执行迁移命令时自动修好;生产环境普通启动只报出、不改。
- 属于数据层的修复,也是给通知偏好「摘要」字段拆掉临时补丁铺路。
- 修错了只影响以后不填该字段的新记录存什么,已有记录一行不改;改动可直接回退。
- 要你做的一件事:定夺通知偏好那个临时补丁现在拆还是等生产启动也能自动改列再拆(见下方 Acceptance notes,席位会转决策)。
## What changes
`createColumn` emits a field's literal `defaultValue` as the column
`DEFAULT` once, when it builds the column; the additive sync never
revisits an existing column, and the differ read `defaultValue` only for
runtime tokens (#4560). So a table an earlier release created kept the
old `DEFAULT` after the field dropped or changed it, and
`detectManagedDrift` answered `[]`.
- `schema-drift.ts`: `diffManagedTable` gains a literal-default arm. A
column carrying a literal `DEFAULT` under a field that declares none
reports `drop_column_default`; a literal that differs from the declared
one reports the new op `set_column_default`. Both `safe`, kind
`default_mismatch`, message ending with the shared `SAFE_COLUMN_REMEDY`.
Judged only where `createColumn` itself 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 physical `DEFAULT` is (`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`: `applyDriftOpInPlace` applies
`set_column_default` with `ALTER TABLE … ALTER COLUMN … SET DEFAULT` on
Postgres and MySQL (the MODIFY-free form, like `drop_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_ANSWERS` answers `set_column_default`
as `needs-table-rebuild`, as it does `drop_column_default`.
Who applies the entries is unchanged: dev auto-reconcile, a publish of
the object (`applyPublishedColumnChanges`, which takes every `safe`
column entry), the artifact-pinned boot and `os migrate apply`. A plain
production boot alters no existing column and logs the entry.
## Tests
`src/sql-driver-36245-stale-literal-default.test.ts`, through
`declareDialectCell` on every cell:
- §1 (no server): 20 physical spellings read as literal / none /
expression; 13 literal-vs-declared pairs.
- §2 per dialect, two boots over one database: the removed default
stores NULL on an unset insert after the second boot; the changed
literal stores the new value; the kept default is stored and never
reported; a row stored before keeps its value; with no auto-reconcile
both entries are reported `safe` and `applyMigrationEntries` applies
them.
<details><summary>Readings (head 3862c8b unless noted)</summary>
- Premise on main d463c46, two `SqlDriver` boots over one database,
`digest` dropping `defaultValue: 'none'` and `changed` going `'a'` to
`'b'`: SQLite file, Postgres 16, MariaDB 10.11, with and without
`autoMigrate: 'safe'` — every cell stored `digest: "none"`, `changed:
"a"`, and `detectManagedDrift` answered `[]`.
- Same probe on the fix: `drift=[set_column_default changed,
drop_column_default digest]` without auto-reconcile; with it, `digest:
null`, `changed: "b"`, `drift=[]` on all three.
- False-positive sweep on the fix: a table built by `initObjects` with
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.
- New file, all three cells (`OS_TEST_POSTGRES_URL`, `OS_TEST_MYSQL_URL`
set): 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.
- Ablation 1, the drop arm disabled (`if (dv === undefined || dv ===
null)` → `if (false as boolean)`), committed fix first, on-disk mutation
proven by `scripts/ablation-replace.mjs` (anchor 1 → 0, blob c1c48d2 →
cb129bf6): 5 of the §2 cases red (`expected 'none' to be null`, the
drift list missing `digest`), restored blob == HEAD.
- Ablation 2, a kept literal read as different (`return true` → `return
false` on the text compare; blob c1c48d2 → 36666e6a): 9 red, including
the kept-default assertion `detectManagedDrift(...) toEqual([])` on
every cell; restored blob == HEAD.
</details>
## Acceptance notes
- **The #35123 stopgap hook is NOT removed in this PR**, against the
card's "remove that hook and its pin in the same PR", which also allows
"a follow-up named here". Measured reason: the repair reaches a database
through dev auto-reconcile, a publish, the artifact-pinned boot or `os
migrate apply`; a plain production boot never alters an existing column
(`reconcileAndWarnDrift`: "schema is never auto-altered in production").
`sys_notification_preference` is 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 runs `os
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 through `os 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 #36570 (Blocked-by this PR).
- A hand-set literal `DEFAULT` on 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 on
`FieldDef.defaultValue`, rewritten here.
- Not covered, same family: a field that GAINS a `defaultValue` after
its table exists (the column has no `DEFAULT`; restating one is outside
the ruling's two verbs, and on MySQL a TEXT column cannot carry it); a
`NOW()` 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 in `diffManagedTable` and `physicalDefaultLiteral`,
where a misread spelling would drop a DEFAULT nobody asked to drop),
then `setDeclaredColumnDefault` in `sql-driver-schema-sync.ts` (the
inlined literal).
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbY6wSVQZ9QBM4hLeYw)_
<!-- signed: repo:objectstack#1 · os-warren ·
session_01JQwxbY6wSVQZ9QBM4hLeYw -->
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 06c3e5a commit b572420
6 files changed
Lines changed: 444 additions & 20 deletions
File tree
- .changeset
- packages/drivers
- driver-sql/src
- driver-turso/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
149 | 150 | | |
150 | 151 | | |
151 | 152 | | |
152 | | - | |
| 153 | + | |
153 | 154 | | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
162 | 169 | | |
163 | 170 | | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
164 | 183 | | |
165 | 184 | | |
166 | 185 | | |
| |||
509 | 528 | | |
510 | 529 | | |
511 | 530 | | |
512 | | - | |
| 531 | + | |
| 532 | + | |
513 | 533 | | |
514 | 534 | | |
515 | 535 | | |
516 | 536 | | |
517 | 537 | | |
518 | 538 | | |
519 | | - | |
| 539 | + | |
520 | 540 | | |
521 | 541 | | |
522 | 542 | | |
| |||
845 | 865 | | |
846 | 866 | | |
847 | 867 | | |
848 | | - | |
| 868 | + | |
849 | 869 | | |
850 | | - | |
851 | | - | |
852 | | - | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
853 | 876 | | |
854 | 877 | | |
855 | 878 | | |
856 | 879 | | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
857 | 973 | | |
858 | 974 | | |
859 | 975 | | |
| |||
1354 | 1470 | | |
1355 | 1471 | | |
1356 | 1472 | | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
| 1497 | + | |
| 1498 | + | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
1357 | 1534 | | |
1358 | 1535 | | |
1359 | 1536 | | |
| |||
0 commit comments