-
Notifications
You must be signed in to change notification settings - Fork 51
feat: add SQL output format #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Navbryce
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks solid! Thank you for this change. A few small comments.
-- Doing this from my cell phone, so excuse any weird formatting in my comments.
cmd/pg-schema-diff/plan_cmd.go
Outdated
| var stmtStrs []string | ||
| for _, stmt := range plan.Statements { | ||
| ddl := strings.TrimSpace(stmt.DDL) | ||
| if !strings.HasSuffix(ddl, ";") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this not consistent across the outputted SQL? I would prefer to ensure are output is consistent inside the diff package than handling normalization in the CLI.
| return string(jsonData) | ||
| } | ||
|
|
||
| func planToSqlS(plan diff.Plan) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we materialize the warnings and timeouts in the plan as well?
The warnings could be added as comments. The timeouts could be set via set statement timeout command?
38a3371 to
95dc472
Compare
|
Can we update this to actually set the statement timeouts? |
Should be possible |
Can we do it in this PR? I think it would be nice just to have it fully fleshed out! Appreciate the work here! |
|
Getting this into a mergable state here!. Your code authorship should still appear among contributions! |
Description
This PR introduces a new --output-format sql option to the pg-schema-diff plan command, allowing users to generate clean, executable SQL output without
formatting headers and numbering. The SQL format produces migration statements that can be directly saved to .sql files and executed against databases.
Motivation
Users frequently need to save migration plans as executable SQL files for manual review, version control, or execution in different environments. The
existing pretty format includes decorative elements like #### headers and 1. numbering that make the output unsuitable for direct database execution.
The new SQL format addresses this by providing clean, semicolon-terminated SQL statements without any formatting artifacts.
Changes
Output Comparison
Pretty Format (Default)
################################ Generated plan ################################
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"user_id" bigint NOT NULL,
"conversation_id" bigint NOT NULL
);
SQL Format (New)
CREATE TABLE "public"."conversation_pin" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"user_id" bigint NOT NULL,
"conversation_id" bigint NOT NULL
);
Testing
DSN-to-Dir, etc.)
Usage