Skip to content

fix(restore): drop USE/CREATE DATABASE statements so mysql/mariadb restores target the selected database - #4967

Open
Siumauricio wants to merge 1 commit into
canaryfrom
fix/restore-drop-use-statements
Open

fix(restore): drop USE/CREATE DATABASE statements so mysql/mariadb restores target the selected database#4967
Siumauricio wants to merge 1 commit into
canaryfrom
fix/restore-drop-use-statements

Conversation

@Siumauricio

@Siumauricio Siumauricio commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Closes #3436

Dumps taken with --databases (or brought from another instance) contain USE \source_db`;— the restore pipeline piped them verbatim intomariadb/mysql <target_db>, so every statement after the USE` executed against the source database instead of the one selected in the restore dialog. In the reported case this overwrote a production database.

The fix inserts a line filter between gunzip and the client for mysql/mariadb restores that drops USE/CREATE DATABASE statements, so the whole dump applies to the database passed on the command line. String values are safe: mysqldump/mariadb-dump escape newlines inside values, so data lines can never start with these keywords.

Verified against a real MariaDB 11 container: before the fix, restoring a --databases dump into dev_db overwrote production_db and left dev_db empty; after the fix, production_db is untouched and dev_db receives the data.

Greptile Summary

This PR prevents MySQL and MariaDB restores from redirecting writes to a database named inside the dump.

  • Adds a case-insensitive line filter that removes USE and CREATE DATABASE statements from MySQL/MariaDB restore streams.
  • Adds tests covering statement removal, preservation of ordinary SQL and string values, and engine-specific pipeline wiring.

Confidence Score: 5/5

The PR appears safe to merge, with no actionable changed-code defects identified.

The filter covers the database-selection statements emitted by the supported MariaDB dump path, remains a harmless pass-through for ordinary SQL, and is limited to MySQL and MariaDB restores.

Reviews (1): Last reviewed commit: "fix(restore): drop USE/CREATE DATABASE s..." | Re-trigger Greptile

Context used:

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. bug Something isn't working labels Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database restore overwrites wrong database when SQL dump contains USE statement

1 participant