fix(restore): strip database-redirect directives from plain SQL before restore#43
Merged
eduardozaniboni merged 1 commit intomasterfrom Mar 10, 2026
Merged
Conversation
…e restore (v0.9.1) pg_dump -C, pgAdmin and mysqldump --databases embed directives (\connect, CREATE DATABASE, USE) that redirect the client to the source database, overriding the --db target provided by the user. The result was that the data was restored to the source database instead of the intended one. Fix: read the plain SQL file into memory, strip the offending directives via filterSqlDirectives(), then pipe the sanitised content via stdin. This works for both internal herdux backups (which never include these directives) and for external SQL files. Affects: PostgreSQL plain restore (psql), MySQL restore. New helper: src/infra/engines/sql-filter.ts (10 unit tests).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Type
fix— bug fixDescription
Plain SQL files produced by
pg_dump -C, pgAdmin, DBeaver, ormysqldump --databasesembed directives that redirect the database client to the source database, overriding the--dbtarget:\connect <db>/\c <db>meta-commandsUSE <db>;statementCREATE DATABASE <db>;/DROP DATABASE <db>;blocksWhen a user ran
hdx restore sateus.sql --db nfse_ce_eusebio, the CLI correctly creatednfse_ce_eusebio, but the data was restored intosateus(the database named in the SQL file).Related issues: N/A
Changes
src/infra/engines/sql-filter.ts— new helperfilterSqlDirectives()that strips the offending directives line by line, handling both single-line and multi-line blockssrc/infra/command-runner.ts— addedstdinContent?: stringtoRunOptions(maps to execainputoption)src/infra/engines/postgres/postgres.engine.ts— plain SQL restore: filter content and pipe via stdin instead of-f filesrc/infra/engines/mysql/mysql.engine.ts— restore: filter content and pipe via stdin instead ofinputFiletests/unit/infra/sql-filter.test.ts— 10 unit tests forfilterSqlDirectives()package.json,README.md,README.pt-BR.md,CHANGELOG.md— version 0.9.0 → 0.9.1How to Test
pg_dump -C mydb > mydb.sqlor pgAdmin exporthdx restore mydb.sql --db other_dbother_db, not inmydbnpm run test:unit— all 520 tests passQuality Checklist
Always required:
AGENTS.md)npm run lint:fixmaster, not a direct commit to itRequired for
feat/fix/refactor:psql,mysql, or binary names insrc/commands/)npm run test:unitnpm run test:integrationRequired if engines or commands were changed:
test:e2e:pgsqlRequired if user-facing behavior changed:
README.mdandREADME.pt-BR.mdupdated accordingly