Support for Inline Schema Documentation
This proposal introduces a system for managing rich, multi-line schema documentation directly in .sql files using a special --- comment syntax, inspired by languages like Dart. The plan command will directly translate these comments, while the dump command will intelligently format them according to a configurable line width in pgschema.yaml, creating a seamless "documentation-as-code" round-trip.
The Problem
The standard COMMENT ON syntax is clumsy for writing detailed, multi-paragraph documentation, leading to poor or inconsistent schema documentation.
Proposed Solution
1. Inline Doc Comment Syntax (---)
A --- comment is recognized as documentation for the object that immediately follows it. Developers can write multi-line comments and create paragraphs with blank --- lines.
Developer's SQL File (users.sql):
--- This table is central to the authentication and authorization systems and
--- stores all core user account and profile information.
---
--- It contains sensitive data and should be handled with care.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
--- The primary login email for the user. This is used for authentication,
--- notifications, and password resets.
email VARCHAR(255) UNIQUE NOT NULL
);
2. plan and apply Behavior (Direct Translation)
The plan command's job is simple: it directly translates the developer's --- comments into the corresponding COMMENT ON statements, preserving their manual formatting exactly as written.
Generated Plan Output:
CREATE TABLE users (...);
COMMENT ON TABLE users IS E'This table is central to the authentication and authorization systems and stores all core user account and profile information.\n\nIt contains sensitive data and should be handled with care.';
COMMENT ON COLUMN users.email IS E'The primary login email for the user. This is used for authentication, notifications, and password resets.';
3. dump and pgschema.yaml Behavior (Intelligent Formatting)
The dump command is responsible for formatting. It reads comments from the database and uses a lineWidth setting in pgschema.yaml to generate clean, readable, and consistently formatted --- comments in the output file.
Configuration (pgschema.yaml):
formatting:
# 'dump' will wrap comments to this line width. Set to 0 to disable.
lineWidth: 80
Result of pgschema dump:
--- This table is central to the authentication and authorization systems and stores
--- all core user account and profile information.
---
--- It contains sensitive data and should be handled with care.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
--- The primary login email for the user. This is used for authentication,
--- notifications, and password resets.
email VARCHAR(255) UNIQUE NOT NULL
);
Benefits
- Ergonomic Writing: Developers can write documentation naturally, with manual or long lines, focusing on content, not formatting.
- Consistent Formatting: The
dumpcommand acts as a formatter, ensuring the entire schema codebase maintains a consistent, readable documentation style. - Lossless Round-Trip: The workflow is perfectly symmetrical. You write documentation freely, and
dumpcleans it up, making the.sqlfiles in Git the single source of truth for both schema and its rich documentation.
The pgschema.yaml file serves as the single source of truth for project-level configuration. This provides a stable and extensible foundation for future enhancements, allowing new features to be configured in one logical place. For instance, this could be extended to consolidate other files, such as moving the ignore patterns from .pgschemaignore into a unified settings file.
This is a bit magic and deviates from the standard SQL. It also adds extra learning curve. No plan to support this.
Thanks for the quick review — I appreciate the concern and the time taken to respond. I respectfully disagree that --- is “magic” or unsafe; maybe I did not explain it well. A few clarifications:
-
Backward-compatible:
---(or even--if preferred) remains a SQL comment (just--plus one extra-) and does not affect SQL semantics. Other tools will continue to work unchanged. -
Opt-in: The feature could be controlled in
pgschema.yaml(defaultfalse), so no change for projects that don’t want it.formatting: inlineDocs: false # enable to support --- inline docs lineWidth: 80 # wrapping for dump output -
Preprocessor-only: When enabled,
plan/applywould translate these comments intoCOMMENT ONstatements for planning only — source files stay untouched. -
Fail-safe behavior: Ambiguous or unmatched doc blocks would produce clear warnings, or can simply be ignored instead of attaching silently.
-
Deterministic dump formatting:
dumpwould readCOMMENT ONvalues and re-wrap them according to thelineWidthsetting for consistent round-tripping.
This is intended as a quality of life improvement that aligns with the idea of having schema and documentation in one place — a “single source of truth.”
If --- feels too unconventional, the same idea could be done with --, though that might blur the line between code comments and documentation comments.
I fully respect your final decision.
--- is not so distinguishable from --
In general, comment block --- and COMMENT ON serves different design purpose. The former is transient (stateless) while the latter is part of the schema (stateful). Providing syntax sugar to convert the former to latter complicates the design.
We are very careful about introducing pgschema specific dialect unless it's necessary.