Commit 7923c8e
fix(driver-sql): a MySQL NOW() datetime default carries its column's precision (#21252)
Fixes #21241
Clause-②: no
## What was wrong
On MySQL a declared `Field.datetime` is built as `DATETIME(3)`. For its
`defaultValue: 'NOW()'` default,
`SqlDriver.nowColumnDefault('datetime')` fell through to a bare
`knex.fn.now()`, which is `CURRENT_TIMESTAMP` at precision 0. MySQL 8.0
refuses a `CURRENT_TIMESTAMP` default whose precision differs from its
column's, so the whole `CREATE TABLE` failed and so did `ALTER TABLE …
ADD`. The builtin `created_at` / `updated_at` columns beside it carried
their own literal `now(3)` and were accepted.
Measured at the base `be5a83cf` against a throwaway MySQL 8.0.46 (server
zone `+08:00`):
| boot | schema sync | refused DDL | data door |
|:--|:--|:--|:--|
| `pnpm dev:crm -- --fresh --database mysql://…` | `synced 80, skipped
0, failed 1` | `sys_activity`: `` `timestamp` datetime(3) default
CURRENT_TIMESTAMP `` → `Invalid default value for 'timestamp'` | `GET
/api/v1/data/sys_activity?limit=1` → `500 DATABASE_ERROR` |
| `pnpm dev:showcase -- --fresh --database mysql://…` | `synced 102,
skipped 2, failed 2` | `sys_activity` as above, and `sys_presence`: ``
`last_seen` datetime(3) default CURRENT_TIMESTAMP `` → `Invalid default
value for 'last_seen'` | `GET /api/v1/data/sys_presence?limit=1` →
`500`, `sys_activity` → `500` |
## The fix
- **One source for the precision.** `MYSQL_DATETIME_PRECISION` is a
module constant in `sql-driver.ts`. Five sites read it: the declared
datetime column (`createColumn`), the builtin audit columns
(`createAuditTimestampColumn`), the `NOW()` default
(`nowColumnDefault`), the UPDATE stamp (`updatedAtStamp`) and the legacy
`TIMESTAMP` widening (`migrateMysqlDatetimeColumns`). Before this PR,
five literal `3`s lived at those sites. Now there is one, and no new
literal was added.
- **`nowColumnDefault('datetime')` on MySQL** returns
`knex.fn.now(MYSQL_DATETIME_PRECISION)`, which renders
`CURRENT_TIMESTAMP(3)`.
- **The builtin audit column's MySQL default is routed through
`nowColumnDefault('datetime')`**, the way the SQLite branch already is.
The declared column and the builtin column now share one definition. The
emitted DDL for the audit columns is byte-identical: `datetime(3)
default CURRENT_TIMESTAMP(3)`.
- PostgreSQL and SQLite emit unchanged SQL. On PostgreSQL the driver
builds `timestamptz`, and `CURRENT_TIMESTAMP` on it was measured
accepted on PostgreSQL 16.14. `timestamp(3) default CURRENT_TIMESTAMP`
is also accepted there, rounding silently. So the mismatch does not
exist on that dialect.
### Bounded in-place fix in the same statement: the `TIMESTAMP` widening
dropped a declared `NOW()` default
The widening's `ALTER … MODIFY` line now reads the constant, so this PR
touches it. That same statement restated the default of `created_at` /
`updated_at` and dropped the default of a declared `NOW()` column. The
`TIME` twin (`migrateMysqlTimeColumns`) already restates it.
Measured on MySQL 8.0.46 with the driver built at this branch before the
change: a legacy `stamped_at timestamp null default current_timestamp`
column came out of schema sync as `datetime(3)` with `COLUMN_DEFAULT`
`null`, and `create` without the field answered `stamped_at = null`. The
widening now restates `nowColumnDefault('datetime')` for a declared
`NOW()` column as well. All four conditions for an in-place fix hold:
- same family (a MySQL `NOW()` datetime default that is not honoured);
- mechanical, with the shape fixed by the `TIME` twin;
- same file and claim;
- same gate family.
The changed lines are `sql-driver.ts` `migrateMysqlDatetimeColumns`, and
a MySQL-only pin, §3.
## Pins:
`packages/drivers/driver-sql/src/sql-driver-21241-mysql-now-default-precision.test.ts`
- **§1**, run on every runner with no server: for `mysql2`, `pg` and
`better-sqlite3`, the compiled DDL of a declared `NOW()` datetime column
is byte-identical to the builtin audit column's. A second case reads the
`mysql2` DDL of the required `sys_activity.timestamp` shape and checks
that its `CURRENT_TIMESTAMP(n)` names the column's own `datetime(n)`.
This compares against the column, not against a literal.
- **§2**, one cell per dialect through `declareDialectCell`:
- the table syncs, both on create and on add-column for a table that
already exists;
- `create` without the field answers the instant the column `DEFAULT`
stored. The check uses a window and confirms that the answer equals
`findOne`;
- a raw insert that never names the column is filled by the `DEFAULT`
alone;
- the server's own catalogue (`information_schema` / `pragma
table_info`) reports the declared column's type and default equal to
`created_at`'s.
- **§3**, MySQL only: a legacy `TIMESTAMP` `NOW()` column keeps a
default through the widening, equal to `created_at`'s.
## Verification (HEAD `d334fe314b`)
Every reading below was re-run at `d334fe314b`: the branch plus one
merge of `origin/main` `4727fcb22a`, which touches no `driver-sql` file
and no lockfile. The readings match the earlier head `e5cab5f20b`,
except the `driver-turso` count, which moved because #21226 landed on
`main`. (Seat edit.)
The live servers were local: MySQL 8.0.46 at `+08:00` and PostgreSQL
16.14 at `Asia/Shanghai`, with `TZ=America/New_York`.
- Pins: `vitest run --reporter=verbose
src/sql-driver-21241-mysql-now-default-precision.test.ts` with both URLs
set → **17 passed (17)**.
- **Reverse verification.** `sql-driver.ts` was written back to the base
blob `e65a0f08` while the HEAD blob `533a790b` stayed committed. On-disk
hash equal to the base blob, `grep -c MYSQL_DATETIME_PRECISION` → 0.
Result: **7 failed | 10 passed (17)**. Every failure is MySQL: §1
`mysql2` ×2, §2 live mysql ×4 (`Invalid default value for 'stamped_at'`
at create and at add-column), and §3. The SQLite and PostgreSQL cells
stayed green (the control). The restore was checked: hash equal to the
HEAD blob and `git diff HEAD` empty.
- **Ablation of the widening half.** Through
`scripts/ablation-replace.mjs`: anchor 1 → 0, blob `533a790b` →
`16057202`. Only §3 went red, `dflt: null` against
`CURRENT_TIMESTAMP(3)`; the other 16 stayed green. Restored: blob equal
to HEAD and `git diff HEAD` empty.
- `@objectstack/driver-sql`, the whole suite against both live servers
with `OS_EXPECT_LIVE_DIALECT_MATRIX=1`: **224 files passed, 5386 passed
| 1 skipped**. The live-dialect reporter printed "all 3 dialects were
exercised".
- `@objectstack/driver-sql`, the SQLite tier (`pnpm --filter
@objectstack/driver-sql test`, no URLs, at `04b23d2791` before the
widening commit): 213 files passed | 11 skipped, 3570 passed | 200
skipped.
- Typecheck of `@objectstack/driver-sql`,
`@objectstack/driver-sqlite-wasm` and `@objectstack/driver-turso` (the
inheritors): all three `Done`. `tsc --listFiles` includes the new test
file once.
- Tests of the inheritors: `driver-sqlite-wasm` 36 files / 675 passed;
`driver-turso` 87 files / 2349 passed | 33 skipped.
- `pnpm check:driver-conformance` gave the same reading before the first
edit and after the last commit: `OK — 50 covered cell(s), 0 in the DEBT
ledger, 0 exempt`, with 0 in the DIALECT ledger.
- Gates: `node scripts/pm/dispatch-gates.mjs --repo
objectstack-ai/objectstack --commands` derived 63 families. All 63 ran
with exit 0. `--ran` reconciliation: `63 derived, 63 run, 0
NOT-MEASURED, 0 UNRUN`, with every line recording its exit code. The
derivation warned that the tree is one `main` commit behind
(`3dc33b2d13`, file-disjoint from this diff, and it touches
`check-route-envelope.mjs` / `engine-double-contract.pinned.json`). CI
re-derives on the merge ref.
- Lint, narrowed:
- `eslint --no-inline-config --format json` over the two changed `.ts`
files: 2 files, 0 errors, 0 warnings.
- Population: both files are linted, not ignored.
- Invariance: the config's `parserOptions` carry no `project` /
`projectService`, so linting is not type-aware and this diff cannot move
a verdict on an untouched file.
- Repo-wide `pnpm lint` is left to CI.
### After the fix, same boot
`pnpm dev:showcase -- --fresh --database mysql://…` at `e5cab5f20b`:
- no `Schema sync FAILED`;
- `GET /api/v1/data/sys_presence` → 200 and `sys_activity` → 200;
- `information_schema` reports `sys_activity.timestamp` and
`sys_presence.last_seen` as `datetime(3)` / `CURRENT_TIMESTAMP(3)`,
equal to their `created_at`;
- a `POST` / `PATCH` / `DELETE` on `showcase_account` left three
`sys_activity` rows (`created`, `updated`, `deleted`).
## Raise-rule reading (triage)
At the base on MySQL, with `sys_activity` absent, a missing table does
not break a record write. `POST /api/v1/data/crm_account` → 201, `PATCH`
→ 200 (a read-back showed the new name), `DELETE` → 200 (a read-back
answered 404). `sys_audit_log` holds 3 rows for the record. Each
mutation lost its activity row: the server logged `Insert operation
failed {object: sys_activity …}` at `warn` and `Audit write FAILED
(ER_NO_SUCH_TABLE …)` at `error`. The API answers carried nothing about
it. Applying the rule is the seat's decision.
## Acceptance notes
- **Existing tables / migration (H5).** On MySQL no table could have
been created with the refused default, so a fresh boot after this change
creates the missing tables and no migration is owed. A column that an
earlier release's `TIMESTAMP` widening already left without a default
does not get one back from this change. The widening only touches
columns that are still `timestamp`. This is a read-only inference beyond
the measurement above.
- **MariaDB not measured.** No MariaDB server here. `client: 'mariadb'`
is not in the driver's MySQL family, but `mysql2` pointed at a MariaDB
server is. Whether MariaDB accepted the bare default, and so holds
tables with a precision-0 default, is NOT MEASURED.
- **Operator text.** At the base, the `Audit write FAILED` line for the
`sys_activity` insert names `sys_audit_log` as the row that "never
landed" and as the table to check. It printed 4 times despite "reported
ONCE". Observation only, nothing filed.
- **`sys_packages`.** Its raw DDL (`created_at TEXT DEFAULT
CURRENT_TIMESTAMP`) is still refused on MySQL in both boots. This was
already recorded in PR #21239's acceptance notes; it is
`domain:services`, and no door was measured.
- **Not filed from this PR, handed to the seat in the report.** At the
base on MySQL, `GET /api/v1/auth/jwks` and `GET /api/v1/auth/token`
answered 500. An insert into `sys_jwks` is refused with `Incorrect
datetime value … for column 'updated_at'`, and the server logs `JWT
signing failed with alg "EdDSA"`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_017xfMoEjKUuSh2xYB8sCozp)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent d2bc644 commit 7923c8e
3 files changed
Lines changed: 358 additions & 9 deletions
File tree
- .changeset
- packages/drivers/driver-sql/src
| 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 | + | |
Lines changed: 275 additions & 0 deletions
| 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 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
0 commit comments