Repository navigation
Commit e312a20
authored
Fix
The driver emulated `ON UPDATE CURRENT_TIMESTAMP` with a trigger that
ran after every `UPDATE`. It **overwrote explicit timestamps and changed
timestamps on unchanged rows**:
```sql
CREATE TABLE t (id INT PRIMARY KEY, n INT, ts TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP);
INSERT INTO t VALUES (1, 1, '2000-01-01 00:00:00');
UPDATE t SET ts = '2001-01-01 00:00:00'; -- MySQL: keeps the explicit value
UPDATE t SET n = n; -- MySQL: keeps the timestamp
UPDATE t SET n = 2, ts = ts; -- MySQL: keeps the timestamp
-- Previously, all three set ts to the current time.
```
Automatic updates were also missing in these cases:
- Columns with `DEFAULT CURRENT_TIMESTAMP` never got an `ON UPDATE`
trigger.
- `ALTER COLUMN ... SET DEFAULT (expr)` could remove the trigger.
- Rebuilding a legacy table could drop its triggers and lose its `ON
UPDATE` behavior.
## Changes
This PR handles automatic timestamps in the translated `UPDATE` and
`INSERT ... ON DUPLICATE KEY UPDATE` statements:
- **Update timestamps only when the row changes.** Compare the new
values with the stored ones after type conversion.
- **Evaluate assignments once per row.** Reuse the new values for change
detection, keeping `RAND()` results consistent and avoiding repeated
subqueries.
- **Keep explicitly assigned timestamps.** Assigning a value, the column
itself (`ts = ts`), or `DEFAULT` prevents the automatic update, as
[MySQL
documents](https://dev.mysql.com/doc/refman/8.4/en/timestamp-initialization.html).
- **Handle columns with defaults.** Automatic updates work with `DEFAULT
CURRENT_TIMESTAMP`, expression defaults, and defaults changed by `ALTER
COLUMN`.
- **Migrate old triggers.** Restore missing `ON UPDATE` metadata before
other migrations can rebuild tables, then remove the legacy and AST
driver triggers. Match exact trigger names and their tables to preserve
unrelated user triggers.
As in MySQL, changing `'a'` to `'A'` counts as a change. Assigning `'1'`
to an `INT` column already containing `1` does not. Expected results
were checked against MySQL 8.4 and 8.0.
Fixes #554.
Fixes #547.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* `ON UPDATE CURRENT_TIMESTAMP` now updates automatically when another
value in the row changes, while preserving the timestamp when assigned
values remain unchanged. Explicit timestamp assignments continue to be
respected.
* Timestamp behavior is more consistent across updates involving
aliases, joined tables, multiple timestamp columns, and duplicate-key
updates.
* **Compatibility**
* Existing databases are migrated to preserve automatic timestamp
behavior while removing legacy timestamp triggers. User-created triggers
are retained.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->ON UPDATE CURRENT_TIMESTAMP for explicit values and unchanged rows (#560)1 parent 958f029 commit e312a20
4 files changed
Lines changed: 888 additions & 99 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2724 | 2724 | | |
2725 | 2725 | | |
2726 | 2726 | | |
2727 | | - | |
| 2727 | + | |
2728 | 2728 | | |
2729 | 2729 | | |
2730 | 2730 | | |
| |||
6275 | 6275 | | |
6276 | 6276 | | |
6277 | 6277 | | |
| 6278 | + | |
| 6279 | + | |
| 6280 | + | |
| 6281 | + | |
| 6282 | + | |
| 6283 | + | |
| 6284 | + | |
6278 | 6285 | | |
6279 | 6286 | | |
| 6287 | + | |
6280 | 6288 | | |
6281 | 6289 | | |
6282 | | - | |
| 6290 | + | |
6283 | 6291 | | |
6284 | 6292 | | |
6285 | 6293 | | |
| |||
6296 | 6304 | | |
6297 | 6305 | | |
6298 | 6306 | | |
6299 | | - | |
| 6307 | + | |
6300 | 6308 | | |
6301 | 6309 | | |
6302 | 6310 | | |
| |||
6312 | 6320 | | |
6313 | 6321 | | |
6314 | 6322 | | |
6315 | | - | |
| 6323 | + | |
| 6324 | + | |
6316 | 6325 | | |
6317 | 6326 | | |
6318 | 6327 | | |
| |||
6364 | 6373 | | |
6365 | 6374 | | |
6366 | 6375 | | |
| 6376 | + | |
| 6377 | + | |
6367 | 6378 | | |
6368 | | - | |
| 6379 | + | |
| 6380 | + | |
| 6381 | + | |
| 6382 | + | |
| 6383 | + | |
| 6384 | + | |
| 6385 | + | |
| 6386 | + | |
| 6387 | + | |
| 6388 | + | |
| 6389 | + | |
| 6390 | + | |
| 6391 | + | |
| 6392 | + | |
| 6393 | + | |
| 6394 | + | |
| 6395 | + | |
| 6396 | + | |
| 6397 | + | |
| 6398 | + | |
| 6399 | + | |
| 6400 | + | |
| 6401 | + | |
| 6402 | + | |
| 6403 | + | |
| 6404 | + | |
| 6405 | + | |
| 6406 | + | |
| 6407 | + | |
| 6408 | + | |
| 6409 | + | |
| 6410 | + | |
| 6411 | + | |
| 6412 | + | |
| 6413 | + | |
| 6414 | + | |
| 6415 | + | |
| 6416 | + | |
| 6417 | + | |
| 6418 | + | |
| 6419 | + | |
| 6420 | + | |
| 6421 | + | |
| 6422 | + | |
| 6423 | + | |
| 6424 | + | |
| 6425 | + | |
| 6426 | + | |
| 6427 | + | |
| 6428 | + | |
| 6429 | + | |
| 6430 | + | |
| 6431 | + | |
| 6432 | + | |
| 6433 | + | |
| 6434 | + | |
| 6435 | + | |
| 6436 | + | |
| 6437 | + | |
| 6438 | + | |
| 6439 | + | |
| 6440 | + | |
| 6441 | + | |
| 6442 | + | |
| 6443 | + | |
| 6444 | + | |
| 6445 | + | |
| 6446 | + | |
| 6447 | + | |
| 6448 | + | |
| 6449 | + | |
6369 | 6450 | | |
6370 | 6451 | | |
6371 | 6452 | | |
| |||
7159 | 7240 | | |
7160 | 7241 | | |
7161 | 7242 | | |
7162 | | - | |
7163 | 7243 | | |
7164 | 7244 | | |
7165 | 7245 | | |
| |||
7230 | 7310 | | |
7231 | 7311 | | |
7232 | 7312 | | |
7233 | | - | |
7234 | | - | |
7235 | | - | |
7236 | | - | |
7237 | | - | |
7238 | | - | |
7239 | | - | |
7240 | 7313 | | |
7241 | 7314 | | |
7242 | 7315 | | |
| |||
7372 | 7445 | | |
7373 | 7446 | | |
7374 | 7447 | | |
7375 | | - | |
| 7448 | + | |
7376 | 7449 | | |
7377 | 7450 | | |
7378 | 7451 | | |
| |||
7705 | 7778 | | |
7706 | 7779 | | |
7707 | 7780 | | |
7708 | | - | |
7709 | | - | |
7710 | | - | |
7711 | | - | |
7712 | | - | |
7713 | | - | |
7714 | | - | |
7715 | | - | |
7716 | | - | |
7717 | | - | |
7718 | | - | |
7719 | | - | |
7720 | | - | |
7721 | | - | |
7722 | | - | |
7723 | | - | |
7724 | | - | |
7725 | | - | |
7726 | | - | |
7727 | | - | |
7728 | | - | |
7729 | | - | |
7730 | | - | |
7731 | | - | |
7732 | | - | |
7733 | | - | |
7734 | | - | |
7735 | | - | |
7736 | | - | |
7737 | | - | |
7738 | 7781 | | |
7739 | 7782 | | |
7740 | 7783 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
337 | 343 | | |
338 | 344 | | |
339 | 345 | | |
340 | 346 | | |
341 | 347 | | |
342 | 348 | | |
343 | 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 | + | |
344 | 395 | | |
345 | 396 | | |
346 | 397 | | |
| |||
422 | 473 | | |
423 | 474 | | |
424 | 475 | | |
425 | | - | |
| 476 | + | |
426 | 477 | | |
427 | 478 | | |
428 | 479 | | |
| |||
0 commit comments