Skip to content

Commit a7b3d5e

Browse files
SQL format default (#239)
1 parent bed0fd3 commit a7b3d5e

File tree

4 files changed

+13
-170
lines changed

4 files changed

+13
-170
lines changed

cmd/pg-schema-diff/plan_cmd.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func buildPlanCmd() *cobra.Command {
4242
toSchemaFlags := createSchemaSourceFlags(cmd, "to-")
4343
tempDbConnFlags := createConnectionFlags(cmd, "temp-db-", "The temporary database to use for schema extraction. This is optional if diffing to/from a Postgres instance")
4444
planOptsFlags := createPlanOptionsFlags(cmd)
45-
outputFmt := outputFormatPretty
45+
outputFmt := outputFormatSql
4646
cmd.Flags().Var(
4747
&outputFmt,
4848
"output-format",
49-
fmt.Sprintf("Change the output format for what is printed. Defaults to pretty-printed human-readable output. (options: %s)", strings.Join(outputFormatStrings(), ", ")),
49+
fmt.Sprintf("Change the output format for what is printed. Defaults to %v. (options: %s)", outputFmt.identifier, strings.Join(outputFormatStrings(), ", ")),
5050
)
5151
cmd.RunE = func(cmd *cobra.Command, args []string) error {
5252
logger := log.SimpleLogger()
@@ -162,11 +162,6 @@ type (
162162
)
163163

164164
var (
165-
outputFormatPretty = outputFormat{
166-
identifier: "pretty",
167-
convertToOutputString: planToPrettyS,
168-
}
169-
170165
outputFormatJson = outputFormat{
171166
identifier: "json",
172167
convertToOutputString: planToJsonS,
@@ -177,10 +172,15 @@ var (
177172
convertToOutputString: planToSql,
178173
}
179174

175+
outputFormatPretty = outputFormat{
176+
identifier: "pretty",
177+
convertToOutputString: planToPrettyS,
178+
}
179+
180180
outputFormats = []outputFormat{
181+
outputFormatSql,
181182
outputFormatPretty,
182183
outputFormatJson,
183-
outputFormatSql,
184184
}
185185

186186
outputFormatStrings = func() []string {
@@ -603,11 +603,14 @@ func planToJsonS(plan diff.Plan) string {
603603
func planToSql(plan diff.Plan) string {
604604
sb := strings.Builder{}
605605
for i, stmt := range plan.Statements {
606+
sb.WriteString("/*\n")
607+
sb.WriteString(fmt.Sprintf("Statement %d\n", i))
606608
if len(stmt.Hazards) > 0 {
607609
for _, hazard := range stmt.Hazards {
608-
sb.WriteString(fmt.Sprintf("-- Hazard %s\n", hazardToPrettyS(hazard)))
610+
sb.WriteString(fmt.Sprintf(" - %s\n", hazardToPrettyS(hazard)))
609611
}
610612
}
613+
sb.WriteString("*/\n")
611614
sb.WriteString(fmt.Sprintf("SET SESSION statement_timeout = %d;\n", stmt.Timeout.Milliseconds()))
612615
sb.WriteString(fmt.Sprintf("SET SESSION lock_timeout = %d;\n", stmt.LockTimeout.Milliseconds()))
613616
sb.WriteString(fmt.Sprintf("%s;", stmt.DDL))

cmd/pg-schema-diff/plan_cmd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (suite *cmdTestSuite) TestPlanCmd() {
113113
CREATE INDEX fizzbuzz_idx ON foobar(fizzbuzz);
114114
`}),
115115
},
116-
outputEquals: "-- Hazard INDEX_BUILD: This might affect database performance. Concurrent index builds require a non-trivial amount of CPU, potentially affecting database performance. They also can take a while but do not lock out writes.\nSET SESSION statement_timeout = 1200000;\nSET SESSION lock_timeout = 3000;\nCREATE INDEX CONCURRENTLY bar_idx ON public.foobar USING btree (bar);\n\n-- Hazard INDEX_BUILD: This might affect database performance. Concurrent index builds require a non-trivial amount of CPU, potentially affecting database performance. They also can take a while but do not lock out writes.\nSET SESSION statement_timeout = 1200000;\nSET SESSION lock_timeout = 3000;\nCREATE INDEX CONCURRENTLY fizzbuzz_idx ON public.foobar USING btree (fizzbuzz);\n",
116+
outputEquals: "/*\nStatement 0\n - INDEX_BUILD: This might affect database performance. Concurrent index builds require a non-trivial amount of CPU, potentially affecting database performance. They also can take a while but do not lock out writes.\n*/\nSET SESSION statement_timeout = 1200000;\nSET SESSION lock_timeout = 3000;\nCREATE INDEX CONCURRENTLY bar_idx ON public.foobar USING btree (bar);\n\n/*\nStatement 1\n - INDEX_BUILD: This might affect database performance. Concurrent index builds require a non-trivial amount of CPU, potentially affecting database performance. They also can take a while but do not lock out writes.\n*/\nSET SESSION statement_timeout = 1200000;\nSET SESSION lock_timeout = 3000;\nCREATE INDEX CONCURRENTLY fizzbuzz_idx ON public.foobar USING btree (fizzbuzz);\n",
117117
},
118118
{
119119
name: "invalid output format",

internal/migration_acceptance_tests/backwards_compat_cases_test.go

Lines changed: 0 additions & 141 deletions
This file was deleted.

pkg/diff/plan_generator.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
externalschema "github.com/stripe/pg-schema-diff/pkg/schema"
1616

1717
"github.com/stripe/pg-schema-diff/pkg/log"
18-
"github.com/stripe/pg-schema-diff/pkg/sqldb"
1918
"github.com/stripe/pg-schema-diff/pkg/tempdb"
2019
)
2120

@@ -103,24 +102,6 @@ func WithRandReader(randReader io.Reader) PlanOpt {
103102
}
104103
}
105104

106-
// deprecated: GeneratePlan generates a migration plan to migrate the database to the target schema. This function only
107-
// diffs the public schemas.
108-
//
109-
// Use Generate instead with the DDLSchemaSource(newDDL) and WithIncludeSchemas("public") and WithTempDbFactory options.
110-
//
111-
// Parameters:
112-
// queryable: The target database to generate the diff for. It is recommended to pass in *sql.DB of the db you
113-
// wish to migrate. If using a connection pool, it is RECOMMENDED to set a maximum number of connections.
114-
// tempDbFactory: used to create a temporary database instance to extract the schema from the new DDL and validate the
115-
// migration plan. It is recommended to use tempdb.NewOnInstanceFactory, or you can provide your own.
116-
// newDDL: DDL encoding the new schema
117-
// opts: Additional options to configure the plan generation
118-
func GeneratePlan(ctx context.Context, queryable sqldb.Queryable, tempdbFactory tempdb.Factory, newDDL []string, opts ...PlanOpt) (Plan, error) {
119-
schemaSource := DBSchemaSource(queryable)
120-
121-
return Generate(ctx, schemaSource, DDLSchemaSource(newDDL), append(opts, WithTempDbFactory(tempdbFactory), WithIncludeSchemas("public"))...)
122-
}
123-
124105
// Generate generates a migration plan to migrate the database to the target schema
125106
//
126107
// Parameters:

0 commit comments

Comments
 (0)