Skip to content

fix(import): make a real pg_dump file importable (closes #852) - #1115

Open
ntdatt812 wants to merge 1 commit into
drawdb-io:mainfrom
ntdatt812:fix/postgres-import-pg-dump-artifacts
Open

fix(import): make a real pg_dump file importable (closes #852)#1115
ntdatt812 wants to merge 1 commit into
drawdb-io:mainfrom
ntdatt812:fix/postgres-import-pg-dump-artifacts

Conversation

@ntdatt812

Copy link
Copy Markdown

Closes #852.

Defect

Importing a file produced by pg_dump fails with a syntax error even though every CREATE TABLE in it is valid. Three constructs in an ordinary dump each abort the parse before a single table is read:

COPY public.users (id, email) FROM stdin;
1	a@b.com
\.

ALTER TABLE public.users ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
    SEQUENCE NAME public.users_id_seq START WITH 1
);

CREATE FUNCTION public.f() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN RETURN NEW; END;
$$;

The COPY block 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 default pg_dump mydb > dump.sql output 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 reaches node-sql-parser. None of them describes a table, a column or a relationship, so the diagram loses nothing — while every CREATE TABLE and ALTER TABLE … ADD CONSTRAINT in the same file becomes importable.

The scan is dollar-quote aware, so a COPY line that is really text inside a routine body is not mistaken for a data block, and a DO $$ … $$ block (which is not a routine definition) is preserved.

Scope is deliberately narrow:

  • runs only on the Postgres path — every other dialect reaches the parser byte for byte as before;
  • Postgres SQL containing none of these constructs is returned unchanged, verified byte for byte;
  • nothing is rewritten, only whole statements dropped.

Verification

There is no test harness in the repo, so I drove the real functions — stripPostgresDumpArtifactsParser.astifyimportSQL — over 20 assertions. All pass.

End to end, on the dump shown above:

before after
astify syntax error parses
tables imported 2
relationships imported 1 (the FK survives)

Invariants

  • ordinary Postgres SQL is returned byte-identical
  • idempotent: strip(strip(x)) === strip(x)
  • "", null, undefined and non-strings pass through untouched

Adversarial cases

  • a data row that itself reads x<TAB>COPY z (q) FROM stdin; does not restart the skip
  • a COPY … line inside a $$ … $$ routine body is not treated as a data block
  • custom dollar tags ($body$) close correctly
  • a one-line routine body (AS $$ SELECT 1 $$;) opens no block
  • DO $$ … $$; is kept — it is not a routine definition
  • multi-line ADD GENERATED … is dropped in full, not just its first line
  • ALTER TABLE … ADD CONSTRAINT … is not confused with ADD GENERATED
  • a table literally named copy is untouched

npx eslint clean on both files; npx vite build succeeds.

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.
@vercel

vercel Bot commented Aug 18, 2026

Copy link
Copy Markdown

@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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] PostgreSQL Import fails with Syntax Error on valid pg_dump files

1 participant