Add dump --format=json for IR output#442
Draft
guillaume86 wants to merge 1 commit into
Draft
Conversation
`pgschema dump` previously only emitted SQL. The IR it builds internally is already fully JSON-tagged on the public fields, so this exposes it directly via a new `--format` flag (default `sql`, new `json`). Motivation: downstream tooling (LSPs, diff visualizers, lint engines, CI gates) wants to consume the model without re-parsing the SQL dump. The internal IR was already the natural source of truth; this is just publishing it. The JSON output is pretty-printed (2-space indent) and stable under round-trip marshal/unmarshal because the IR uses sorted map keys and unexported sync.RWMutex fields are ignored by encoding/json. `--multi-file` is rejected with `--format=json` since the IR is a single document; unknown formats produce a clear error. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
--formatflag topgschema dump.--format=sql(default) keeps the current behavior;--format=jsonwrites the IR pretty-printed (2-space indent) to stdout.sync.RWMutexfields are ignored byencoding/jsonautomatically.--multi-fileis rejected with--format=json(the IR is a single document); unknown formats produce a clear error.Motivation
Downstream tooling — LSPs, diff visualizers, lint engines, CI gates, IR-aware test harnesses — currently has to re-parse pgschema's SQL output to recover the model. The internal IR is the natural source of truth, and exposing it directly:
ExecuteDump).Test plan
TestIRJSONRoundTrip(unit, no DB) — marshal a constructed IR, unmarshal, marshal again; assert byte-for-byte equality. Proves round-trip stability.TestExecuteDump_FormatValidation(unit) — unknown format andjson + --multi-fileproduce clear errors.TestDumpCommand_FormatJSON(integration, PG 17) — apply real DDL to embedded PG, rundump --format=json, parse the output, assert schema/table/column/index shape, and check the round-trip is stable.Run just the new tests:
Full suite green locally (
go test ./...~7 min on PG 17).Possible follow-ups (not in this PR, happy to discuss)
model_versionfield on the JSON envelope so future schema-changes to the IR can be detected by consumers (mirrors whatPlanalready does withPgschemaVersion).LoadIRFromJSONhelper onirfor symmetric consumption — currently consumers canjson.Unmarshal(bytes, &ir.IR{})directly, which works but a typed loader is friendlier.dump --format=jsonJSON Schema published alongside.Kept this PR minimal — each of the above can be its own conversation.
🤖 Generated with Claude Code
Note (2026-05-23): pgproj (a downstream DACFX-shaped fork at https://github.com/guillaume86/pgproj) has adopted a hard-fork posture and will continue independently. This contribution remains open without expectation of merge; happy to address review feedback if a maintainer engages.