Repository navigation
Commit 958f029
Normalize zero defaults of temporal columns (#542)
## Summary
A numeric zero default on a temporal column was stored verbatim and
emitted as a quoted string:
```sql
CREATE TABLE t (created_at TIMESTAMP NOT NULL DEFAULT 0);
SHOW CREATE TABLE t;
-- `created_at` timestamp NOT NULL DEFAULT '0' (MySQL: '0000-00-00 00:00:00')
```
MySQL rejects `DEFAULT '0'` in strict SQL mode (`ERROR 1067: Invalid
default value`), so dumps of these tables don't import. The H5P plugin
creates its tables with this pattern, so exports of sites that use it
fail.
## Changes
Zero defaults of temporal columns now get MySQL's zero values, both in
new tables and in existing databases:
- **Normalize on write.** `CREATE TABLE` and `ALTER TABLE` store zero
defaults of `DATE`, `TIME`, `DATETIME`, `TIMESTAMP`, and `YEAR` columns
as MySQL zero values, including fractional seconds precision
(`DATETIME(3)` → `'0000-00-00 00:00:00.000'`). This covers all numeric
forms, such as `0`, `-0`, `0.0`, `0e0`, and `FALSE`, and it fixes `SHOW
CREATE TABLE`, `INFORMATION_SCHEMA.COLUMNS`, and the default that new
rows receive.
- **Normalize `ALTER COLUMN ... SET DEFAULT`.** Defaults set with `SET
DEFAULT`, added in #555, are normalized the same way. This includes a
parenthesized literal, such as `SET DEFAULT (0)`, which MySQL also
stores as a literal.
- **Normalize quoted zeros, except for `YEAR`.** Quoted zeros, such as
`DEFAULT '0'`, are normalized the same way, as older driver versions
stored them the same as numbers. For `YEAR` columns, MySQL reads the
strings `'0'` and `'00'` as the year 2000, so they stay as written.
- **Fix `DATETIME_PRECISION`.** `INFORMATION_SCHEMA.COLUMNS` reported
`0` for all `TIME(n)`, `DATETIME(n)`, and `TIMESTAMP(n)` columns. It now
reports the precision of the column type.
- **Repair existing databases.** For databases last configured by driver
3.1.1 or older, the configuration corrects the stored
`DATETIME_PRECISION` values, and it changes zero defaults of temporal
columns, such as `0`, to the zero values with `ALTER TABLE ... ALTER
COLUMN ... SET DEFAULT`. This also recreates the tables, so that new
rows get the zero value. This includes tables created by the legacy
SQLite driver. The repair runs once, within the configuration
transaction, so it either fully succeeds or changes nothing.
- **Repair existing rows.** Rows that got such a zero default from older
driver versions still store it, such as `'0'`. The repair replaces it
with the zero value, so that dumps of these rows import into MySQL in
strict SQL mode. The update doesn't run the table's triggers, such as
the emulation of `ON UPDATE CURRENT_TIMESTAMP`.
- **Report why a table rebuild fails.** `ANALYZE`, `CHECK`, `OPTIMIZE`,
and `REPAIR TABLE` reported every general SQLite error as a missing
table, so a failed table rebuild could say `Table '…' doesn't exist` for
a table that exists. They now report only SQLite's "no such table" error
that way, and other errors as SQLite returns them.
Values match MySQL 8 output for the same default in `CREATE TABLE` or
`ALTER COLUMN ... SET DEFAULT`, in `STRICT_TRANS_TABLES` mode:
| Column | `COLUMN_DEFAULT` |
| --- | --- |
| `TIMESTAMP` / `DATETIME DEFAULT 0` | `0000-00-00 00:00:00` |
| `DATE DEFAULT 0` | `0000-00-00` |
| `TIME DEFAULT 0` | `00:00:00` |
| `YEAR DEFAULT 0` | `0000` |
| `DATETIME(3) DEFAULT 0` | `0000-00-00 00:00:00.000` |
| `TIMESTAMP(6) DEFAULT 0` | `0000-00-00 00:00:00.000000` |
| `TIME(2) DEFAULT 0` | `00:00:00.00` |
## Scope
- **Non-zero temporal defaults**, such as `DEFAULT 20200102`, are still
stored verbatim. The existing `@TODO` covers them.
- **`YEAR` string zeros**, such as `'0'`, stay as written. MySQL reads
them as the year 2000, but the driver also stores an inserted `'0'` as
`0000`, so both need a separate fix, tracked in #545. Older driver
versions stored `YEAR DEFAULT 0` and `YEAR DEFAULT '0'` the same way, so
the repair can't tell them apart and leaves them unchanged.
- **Recreated tables reset their `AUTO_INCREMENT` counter** to the
highest existing ID. This is an existing problem of `ALTER TABLE` and
`OPTIMIZE TABLE`, tracked in #544.
- **Recreated legacy tables lose `ON UPDATE CURRENT_TIMESTAMP`.** The
legacy SQLite driver implemented it with triggers, which a table rebuild
drops without replacing them. This is an existing problem of `ALTER
TABLE` and `OPTIMIZE TABLE`, tracked in #547. The repair also rebuilds
legacy tables that have a temporal zero default, so they lose it as
well.
## Testing
End to end, the `SHOW CREATE TABLE` output for H5P's `wp_h5p_contents`
and `wp_h5p_contents_user_data` imports into MySQL with
`sql_mode='STRICT_TRANS_TABLES'`. Before the change, it fails with
`ERROR 1067`. See the [verification on a WordPress Studio
site](#542 (comment)).
---
AI assistance: Claude (Anthropic) via the Pi coding agent, with an
initial candidate from a Homeboy cook. Claude traced the failure,
reworked and tested the implementation, verified it against MySQL, and
drafted this PR at Chris Huber's direction.
Fixes #541.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Temporal columns with numeric-zero defaults now use MySQL-compatible
zero dates or times, including fractional seconds.
* Temporal defaults are consistently represented in table definitions,
metadata, and inserted or altered rows.
* Existing databases are normalized when reopened, including temporal
precision corrections for older driver versions.
* Non-numeric defaults and string `YEAR` zero values remain unchanged.
* Foreign-key relationships and active transactions remain intact during
reconfiguration.
* Failed table repairs now roll back cleanly and preserve the underlying
error details.
* Administrative errors distinguish missing tables from other database
errors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Jan Jakeš <jan@jakes.pro>1 parent 503958e commit 958f029
5 files changed
Lines changed: 1134 additions & 33 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4155 | 4155 | | |
4156 | 4156 | | |
4157 | 4157 | | |
4158 | | - | |
| 4158 | + | |
| 4159 | + | |
| 4160 | + | |
| 4161 | + | |
| 4162 | + | |
4159 | 4163 | | |
4160 | 4164 | | |
4161 | 4165 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
80 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
81 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
82 | 99 | | |
83 | 100 | | |
84 | 101 | | |
85 | | - | |
86 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
87 | 106 | | |
88 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
89 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
90 | 120 | | |
91 | | - | |
92 | 121 | | |
93 | 122 | | |
94 | 123 | | |
| |||
252 | 281 | | |
253 | 282 | | |
254 | 283 | | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
255 | 307 | | |
256 | 308 | | |
257 | 309 | | |
| |||
272 | 324 | | |
273 | 325 | | |
274 | 326 | | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
275 | 476 | | |
0 commit comments