fix(restore): replace USE statements with target database during restore #3437
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #3436
When restoring a MySQL/MariaDB backup to a different database, the SQL dump may contain
USE \original_database`` statements that override the target database specified in the restore dialog. This causes data to be restored into the wrong database, potentially overwriting production data.Problem
SQL dumps created by
mysqldump/mariadb-dumpinclude explicit database selection:When a user restores this backup into a different database (e.g.,
dev_db):mariadb -u user -p dev_db < backup.sqlThe
USE \production_db`statement inside the dump switches the context toproduction_db, and all subsequentDROP TABLE,CREATE TABLE, andINSERTcommands execute against the **production database** instead of the intendeddev_db`.Result: Production database is overwritten with old backup data. Critical data loss.
Solution
Added a
sedfilter to replace allUSE \...`` statements with the target database name before piping to mysql/mariadb:This ensures all
USEstatements in the dump are rewritten to point to the user-specified target database.Changes
packages/server/src/utils/restore/utils.ts:Updated
getMariadbRestoreCommand()- added sed filter for USE statementsUpdated
getMysqlRestoreCommand()- added sed filter for USE statementsTesting
-- Database: xyz) are not affectedChecklist
canarybranch.Issues related
closes #3436