fix(import): make a real pg_dump file importable (closes #852) - #1115
Open
ntdatt812 wants to merge 1 commit into
Open
fix(import): make a real pg_dump file importable (closes #852)#1115ntdatt812 wants to merge 1 commit into
ntdatt812 wants to merge 1 commit into
Conversation
Importing a file produced by pg_dump fails with a syntax error even when every CREATE TABLE in it is valid. Three constructs in an ordinary dump each abort the parse before any table is read: COPY public.users (id, email) FROM stdin; -- + raw rows, terminated by \. ALTER TABLE t ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (...); CREATE FUNCTION f() ... AS $$ ... $$; The COPY block is not SQL at all — it is tab-separated data, emitted once per table unless the dump was taken with --schema-only. The identity clause and the dollar-quoted routine body are likewise unreadable by node-sql-parser. None of the three describes a table, a column or a relationship, so the diagram loses nothing by dropping them, while every CREATE TABLE and ALTER TABLE ... ADD CONSTRAINT in the same file becomes importable. stripPostgresDumpArtifacts() drops exactly those three, dollar-quote aware so a COPY line that is really text inside a routine body is not mistaken for a data block. It runs only on the Postgres path; every other dialect reaches the parser byte for byte as before, and Postgres SQL with none of these constructs is returned unchanged. Refs drawdb-io#852.
|
@ntdatt812 is attempting to deploy a commit to the dottle's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Closes #852.
Defect
Importing a file produced by
pg_dumpfails with a syntax error even though everyCREATE TABLEin it is valid. Three constructs in an ordinary dump each abort the parse before a single table is read:The
COPYblock is the worst of the three: it is not SQL at all — tab-separated rows terminated by a lone\.— and pg_dump emits one per table unless the dump was taken with--schema-only. So the defaultpg_dump mydb > dump.sqloutput can never be imported.The user sees only "Syntax Error due to token…", with nothing to indicate the schema itself was fine.
Fix
stripPostgresDumpArtifacts()removes exactly those three constructs before the source reachesnode-sql-parser. None of them describes a table, a column or a relationship, so the diagram loses nothing — while everyCREATE TABLEandALTER TABLE … ADD CONSTRAINTin the same file becomes importable.The scan is dollar-quote aware, so a
COPYline that is really text inside a routine body is not mistaken for a data block, and aDO $$ … $$block (which is not a routine definition) is preserved.Scope is deliberately narrow:
Verification
There is no test harness in the repo, so I drove the real functions —
stripPostgresDumpArtifacts→Parser.astify→importSQL— over 20 assertions. All pass.End to end, on the dump shown above:
astifyInvariants
strip(strip(x)) === strip(x)"",null,undefinedand non-strings pass through untouchedAdversarial cases
x<TAB>COPY z (q) FROM stdin;does not restart the skipCOPY …line inside a$$ … $$routine body is not treated as a data block$body$) close correctlyAS $$ SELECT 1 $$;) opens no blockDO $$ … $$;is kept — it is not a routine definitionADD GENERATED …is dropped in full, not just its first lineALTER TABLE … ADD CONSTRAINT …is not confused withADD GENERATEDcopyis untouchednpx eslintclean on both files;npx vite buildsucceeds.