Skip to content

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

Merged
narcisonunez merged 1 commit into
canaryfrom
fix/restore-drop-use-statements
Aug 11, 2026
Merged

fix(restore): drop USE/CREATE DATABASE statements so mysql/mariadb restores target the selected database#4967
narcisonunez merged 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

@narcisonunez narcisonunez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving — works correctly for all dokploy-generated backups. One edge case: if an externally-created dump uses --add-drop-database, the DROP DATABASE statement won't get filtered and could still hit the wrong target. Not blocking, but happy to extend the regex to cover drop database in a follow-up PR if it ever comes up.

grep -viE '^[[:space:]]*(use|(create|drop)[[:space:]]+database)[[:space:]]'

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 11, 2026
@narcisonunez
narcisonunez merged commit ce0fe63 into canary Aug 11, 2026
6 checks passed
@narcisonunez narcisonunez added the hotfix Cherry-pick this fix to main for next patch release label Aug 11, 2026
pull Bot pushed a commit to tqa24/dokploy that referenced this pull request Aug 14, 2026
…tements

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

(cherry picked from commit ce0fe63)

[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hotfix Cherry-pick this fix to main for next patch release lgtm This PR has been approved by a maintainer 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

2 participants