pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Request: CLI option to exclude pgschema comments from dump

Open isaacparker0 opened this issue 10 months ago • 7 comments

I have a use case where it's useful get a pure DDL schema dump with no commentary. Currently, I use a wrapper script that invokes pgschema and then strips out comments (pgschema header and object comments) using regex. It would be nice if there was a --no-comments CLI option, similar to pg_dump: https://www.postgresql.org/docs/current/app-pgdump.html.

--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 18.1
-- Dumped by pgschema version 1.6.1
--
-- Name: users; Type: TABLE; Schema: -; Owner: -
--

isaacparker0 avatar Jan 27 '26 18:01 isaacparker0

@isaacparker0 could you please share your use cases?

tianzhou avatar Jan 28 '26 14:01 tianzhou

@isaacparker0 could you please share your use cases?

Sure. We use pgschema solely for the dump command, in order to generate a canonical schema.sql file that is checked into the codebase. This schema file is generated automatically by running our sql migrations against a hermetic postgres binary, then using pgschema dump & a Bazel genrule to write the file. It's used for code review/ schema documentation, code generation for other tooling, automated schema drift detection in long-lived environments, and automated squashing of migrations every once in a while. We're basically using pgschema as a pg_dump replacement since it's easier to get as a binary that bazel can manage.

The pgschema comments don't necessarily hinder any of this, but they are generally just noise that we prefer to strip out.

And for what it's worth, https://github.com/pgschema/pgschema/issues/252 would also be useful as qualified names in schema dump would allow us to use a schema dump as a squashed migration file with no additional transformation.

isaacparker0 avatar Jan 28 '26 14:01 isaacparker0

@isaacparker0 I see. The original request is to strip out comments (pgschema header and object comments). IIUC, stripping object comments while keeping the header should serve your use case. I am asking because pg_dump --no-comments still keeps the header and I want pgschema follow that convention if possible.

tianzhou avatar Jan 28 '26 14:01 tianzhou

Ah I'm actually realizing that --no-comments strips out COMMENT ON statements, not pg_dump's actual object comments 🤦‍♂️ . It would still be nice if there was an option to remove object comments but understandable if you'd prefer to stick to pg_dump conventions.

isaacparker0 avatar Jan 28 '26 14:01 isaacparker0

@isaacparker0 pg_dump --no-comments always keeps COMMENT ON statements. It only strips inline comments for the object. While it retains the header comment like below

--
-- PostgreSQL database dump
--

-- Dumped from database version 17.5 (Debian 17.5-1.pgdg120+1)
-- Dumped by pg_dump version 17.5 (Homebrew)

tianzhou avatar Jan 28 '26 14:01 tianzhou

I just tested this with postgres 18.1:

CREATE SCHEMA IF NOT EXISTS comment_test;
CREATE TABLE comment_test.my_table (
    id serial PRIMARY KEY,
    name text
);
COMMENT ON TABLE comment_test.my_table IS 'This is a table comment';
COMMENT ON COLUMN comment_test.my_table.name IS 'This is a column comment';
pg_dump $DSN [--no-comments] --schema=comment_test

Both with and without the flag the dump has the object comments; --no-comments strips only COMMENT ON statements.

Admittedly this is confusing and I am seemingly not the only one to be tripped up by this: https://www.postgresql.org/message-id/ZyjdAjEsXbFPkD3t%40momjian.us.

isaacparker0 avatar Jan 28 '26 15:01 isaacparker0

TIL. I will stick with my plan to strip object comments while keeping the header.

tianzhou avatar Jan 28 '26 15:01 tianzhou