Skip to content

feat(backup): stream the dump and the restore, in the backend - #652

Merged
EVWorth merged 2 commits into
mainfrom
fix/backup-streaming
Sep 12, 2026
Merged

EVWorth merged 2 commits into
mainfrom
fix/backup-streaming

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Closes #358, #357, #359, #360, #361.

The dump moved to Rust (#358)

It was built in the renderer: every CREATE and every INSERT concatenated into one JavaScript string, handed across IPC in a single call, written to disk at the end. A ten-million-row table meant gigabytes held in the WebView before the file existed, and cancelling could not give the memory back. Rows were read with LIMIT/OFFSET against the pool — quadratic, and with no ORDER BY and no shared session, free to return a row twice or not at all.

mas-core::backup writes straight to the file as it reads, streaming rows over one connection. Memory is bounded by a 256 KB buffer.

What checking the output found

Every backup was losing data. Values went through the grid's value type on the way out, which formats DATETIME as %Y-%m-%d %H:%M:%S and rounds through f64. So DATETIME(6) lost its microseconds, and BIGINT and DECIMAL went through a float — in a backup. Cells are now formatted from the bytes the server printed over the text protocol: what mysqldump sees, and the only form that round-trips.

Views restored into the wrong database. SHOW CREATE VIEW names the view, and every table in its body, with the database it came from. Restoring such a dump into a second database recreated the view in the first one — or failed with "already exists" against the object it was copied from.

New in the dump

  • A consistent snapshot by default — START TRANSACTION WITH CONSISTENT SNAPSHOT on the reading connection, what mysqldump --single-transaction does. Without it a dump of a live database can contain a child row whose parent was written later and is not in the file. When the server refuses (MyISAM) the dump says so, in the file and in the summary.
  • Progress that moves ([P1][ux] BackupDialog progress UI silent on long dumps; no ETA, no rows/sec #361) — at row-batch boundaries rather than once per table, with rows/sec over the last interval and the server's row estimate, so a single large table no longer looks identical to a hung one. The dialog shows rate, bytes, elapsed and "~4s left in this table".
  • A warnings list — a view whose definer is gone should not cost you the other nine hundred tables, but it must not pass silently either.
  • Cancel removes the file. A partial dump that looks like a dump is how someone restores half a database a month later.

The restore, same shape (#359, #360)

It read the whole file into the renderer, split it there, and sent back one statement per call — each on whichever pooled connection it landed on, so the USE that opened the database was not guaranteed to apply to the next statement.

mas-core::restore reads in chunks, splits as it goes (quotes, --/#//* */, backticks, DELIMITER), and runs on one connection. A file ending inside a string, a comment, or an unterminated stored program is an error naming what was cut short — #360's hang was the splitter waiting for a terminator that never came.

On the transaction #359 asked for. It is there, and it makes a data-only file all-or-nothing. It cannot make a dump with DDL atomic, and no client can: MySQL commits before every CREATE, DROP and ALTER. Verified on 8.0.46 — ROLLBACK after CREATE TABLE + INSERT left both the table and the row. So the acceptance criterion as written ("zero tables created") is not reachable. What the restore does instead is report partiallyApplied, and the dialog says which of "the database is as it was" and "part of this already applied — MySQL commits before every CREATE, DROP or ALTER" is true. The old dialog said neither.

Foreign-key and uniqueness checks go off for the restoring session and back on when it ends, so a file whose tables are in the wrong order loads, and no pooled connection is left with them off (there is a test for exactly that).

Docs (#357)

ARCHITECTURE 3.6 promised a mysqldump wrapper that detects the binary on PATH. There is none, and there should not be: requiring a binary the user may not have, at a version that may not match the server, is a worse guarantee than a native dump that formats from the server's own bytes. §5.11 describes what exists; §5.10's event table — nine rows, six of them for a streaming architecture that was never built — is down to the three events that exist. FR-7.3.3/7.3.4 rewritten.

Tests

  • 19 Rust unit tests (escaping, cell shapes, AUTO_INCREMENT stripping, the splitter incl. chunk-boundary escapes and delimiters)
  • 19 live-server integration tests, passing against MySQL 8.0.46 and MariaDB 11.8, including a hostile row value that restores as data under NO_BACKSLASH_ESCAPES with a victim table present to prove it
  • 28 frontend tests; the 757-line TS generator and its tests are gone

2669 unit, 344 browser, clippy and eslint clean.

The whole backup was built in the renderer: every CREATE and every
INSERT concatenated into one JavaScript string, handed across IPC in a
single call, and written to disk at the end. A ten-million-row table
meant gigabytes held in the WebView before the file existed, and
cancelling could not give the memory back (#358). Rows were read with
LIMIT/OFFSET against the pool, which is quadratic, and with no ORDER BY
and no shared session is free to return a row twice or not at all.

`mas-core::backup` writes straight to the file as it reads, on one
connection, streaming rows. Memory is bounded by the buffer.

Checking the output found something worse than the memory: every value
went through the grid's value type on the way out, which formats
DATETIME as `%Y-%m-%d %H:%M:%S` and rounds through f64. Every backup was
silently dropping the fractional seconds from DATETIME(6), and putting
BIGINTs and DECIMALs through a float. The dump now formats each cell
from the bytes the server printed over the text protocol, which is what
mysqldump sees and the only form that round-trips.

Also found: `SHOW CREATE VIEW` names the view — and every table in its
body — with the database it came from, so restoring a dump into a
second database recreated the view in the first one, or failed with
"already exists" against the object it was copied from.

New: a consistent snapshot by default (what `mysqldump
--single-transaction` does), so a dump of a live database cannot contain
a child row whose parent was written later; progress at row-batch
boundaries with rows/sec and an estimate, rather than one report per
table, so a single large table no longer looks identical to a hung one
(#361); a warning list for what could not be read, in the file and in
the summary; and a cancel that removes the partial file, because a
partial dump that looks like a dump is how someone restores half a
database a month later.

The restore is the same shape. It used to read the whole file into the
renderer, split it there, and send back one statement per call — each on
whichever pooled connection it landed on, so `USE` and any session
setting applied to a session the next statement might not get (#359).
`mas-core::restore` reads in chunks, splits as it goes with a state
machine that handles quotes, both comment forms and DELIMITER, and runs
the statements on one connection. A file that ends inside a string, a
comment or an unterminated stored program is now an error naming what
was cut short, rather than a splitter waiting forever for a terminator
that never comes (#360).

On the transaction #359 asked for: it is there, and it makes a data-only
file all or nothing, but it cannot make a dump with DDL atomic. MySQL
commits before every CREATE, DROP and ALTER — verified on 8.0.46, where
ROLLBACK after CREATE TABLE + INSERT left both the table and the row.
So the summary reports `partiallyApplied` and the dialog says which of
"the database is as it was" and "part of this already applied" is true,
which is the thing the old dialog never said either way.

Foreign-key and uniqueness checks are turned off for the restoring
session and back on when it ends, so a file whose tables are in the
wrong order loads — and no connection is left in the pool with them off.

Docs: ARCHITECTURE 3.6 promised a mysqldump wrapper that detects the
binary on PATH (#357). There is none and there should not be: requiring
a binary the user may not have, at a version that may not match the
server, is a worse guarantee than a native dump that formats from the
server's own bytes. §5.11 now describes what is there, and §5.10's event
table — nine rows, six of them for a streaming architecture that was
never built — is down to the three events that exist.

19 Rust unit tests, 19 live-server integration tests (passing against
MySQL 8.0.46 and MariaDB 11.8), 28 frontend tests. The TS generator and
its 757 lines of tests are gone.
…back

The round-trip test the audit asked for — dump a database, restore it
elsewhere, compare — found this on its first run: the dump opened
START TRANSACTION WITH CONSISTENT SNAPSHOT and never closed it, so the
connection returned to the pool still reading the database as it was.
The next caller to get that connection saw a stale schema, which
surfaces later and somewhere else as "Table definition has changed,
please retry transaction".

Two tests: the round trip itself, which compares a checksum over every
column of every row rather than a row count, so a value that came back
subtly different fails there rather than looking identical; and a direct
one that alters the table after a dump and reads the schema back on
every pooled connection.

Closes the backup audit's F7 and F9.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P0][bug] generateBackup buffers entire dump in single string; large DBs OOM renderer

1 participant