Repository navigation
Commit c71da13
fix(db): export every MySQL table and restore with foreign keys off
Bug: `void db export` in an app with "database": "mysql" fails on
MySQL 8 with "Table 'app.undefined' doesn't exist" (with or without
--output, --no-schema, --no-data). Once the table names are read
correctly, the dump still cannot be restored when a child table's
name sorts before its parent's: a full dump fails with "Failed to
open the referenced table", and a --no-schema dump fails with
"Cannot add or update a child row: a foreign key constraint fails".
Repro: on MySQL 8, create posts(id PK) and comments(post_id FK ->
posts.id) with one row each, then run `void db export --output
dump.sql`. It throws. With the name read fixed, load the dump into an
empty database: comments is created (or filled) before posts and the
foreign key check rejects it.
Cause: runExport in packages/void/src/cli/db.ts lists tables with an
unaliased `SELECT table_name FROM information_schema.tables`. MySQL 8
labels that column `TABLE_NAME`, so `row.table_name` is undefined and
every table becomes "undefined". The loop then writes tables in name
order, SHOW CREATE TABLE keeps foreign keys inline, and the dump never
turns FOREIGN_KEY_CHECKS off, so restore order breaks on parent/child
pairs.
Fix: alias the column (`table_name AS table_name`), the same fix
`void db reset` already uses, and wrap the MySQL dump in
`SET FOREIGN_KEY_CHECKS = 0;` ... `SET FOREIGN_KEY_CHECKS = 1;`. The
wrapper is smaller than sorting tables by dependency and also covers
foreign key cycles. Only the MySQL branch of runExport changes.
Test: packages/void/test/unit/db-commands.test.ts, "void db export >
MySQL: exports every table with foreign key checks off". Without the
fix the export queries `undefined` and has no SET lines.
Note: every MySQL export (full, --table, --no-schema, --no-data) now
starts with FOREIGN_KEY_CHECKS = 0 and ends with = 1, the MySQL
default. A restore into a session that already had the checks off
ends with them on.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0123r5HnanmW2VUrZ7VWDufH1 parent e17f70f commit c71da13
2 files changed
Lines changed: 47 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1832 | 1832 | | |
1833 | 1833 | | |
1834 | 1834 | | |
| 1835 | + | |
1835 | 1836 | | |
1836 | | - | |
| 1837 | + | |
1837 | 1838 | | |
1838 | 1839 | | |
1839 | 1840 | | |
1840 | 1841 | | |
| 1842 | + | |
| 1843 | + | |
| 1844 | + | |
1841 | 1845 | | |
1842 | 1846 | | |
1843 | 1847 | | |
| |||
1863 | 1867 | | |
1864 | 1868 | | |
1865 | 1869 | | |
| 1870 | + | |
1866 | 1871 | | |
1867 | 1872 | | |
1868 | 1873 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
451 | 451 | | |
452 | 452 | | |
453 | 453 | | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
454 | 495 | | |
455 | 496 | | |
456 | 497 | | |
| |||
0 commit comments