Skip to content

restore: rename the database when CREATE DATABASE follows header comments - #560

Open
jayhemnani9910 wants to merge 1 commit into
databacker:mainfrom
jayhemnani9910:fix/restore-rename-after-comments
Open

jayhemnani9910 wants to merge 1 commit into
databacker:mainfrom
jayhemnani9910:fix/restore-rename-after-comments

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

restore --database FOO:BAR is documented to restore FOO into BAR and make sure BAR exists. On the tool's own dump files it fails every time:

mysql-backup restore ... --database shop:shop2 db_backup_<ts>.tar
Error: error restoring: failed to restore database: ... Error 1049 (42000): Unknown database 'shop2'

The dump header puts comment lines right before CREATE DATABASE (--\n-- Current Database: \shop`\n--). Restorecollects them into the same statement, socreateRegex, which is anchored with ^, never matches, and CREATE DATABASEkeeps the old name. TheUSEline that follows has no comments, so it is rewritten toshop2, and MySQL is told to use a database that was never created. If shop` was missing, it also gets created, empty.

-- comment lines are now skipped between statements, as the mysql client does. Lines inside a statement are untouched.

Testing: added TestRestoreRenamesDatabaseAfterHeaderComments in pkg/database. It records the statements Restore executes through a small test driver. On main it gets the header comments plus CREATE DATABASE ... `shop` , and with the change CREATE DATABASE ... `shop2` and USE `shop2` . go test ./... passes. Against MySQL 8.4 in Docker, I dumped a database and restored it with --database shop:shop_fix: main fails with the error above, and the change restores all rows. A plain restore without --database gives the same CHECKSUM TABLE values as before the dump.

…ents

Signed-off-by: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com>
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.

1 participant