Skip to content

Commit fac0ff2

Browse files
authored
Merge branch 'stripe:main' into lmcrean/212
2 parents 8a3f630 + a7b3d5e commit fac0ff2

File tree

16 files changed

+217
-307
lines changed

16 files changed

+217
-307
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ assignees: ''
88
---
99

1010
### Describe the bug**
11-
[//]: # A clear and concise description of what the bug is.
11+
<!-- A clear and concise description of what the bug is. -->
1212

1313
### Expected behavior**
14-
[//]: # A clear and concise description of what you expected to happen.
14+
<!-- A clear and concise description of what you expected to happen. -->
1515

1616
### To Reproduce
17-
[//]: # Steps to reproduce the behavior:
18-
[//]: # 1. Go to '...'
19-
[//]: # 2. Click on '....'
20-
[//]: # 3. Scroll down to '....'
21-
[//]: # 4. See error
17+
<!-- Steps to reproduce the behavior:
18+
1. Go to '...'
19+
2. Click on '....'
20+
3. Scroll down to '....'
21+
4. See error -->
2222

2323
### Context
24-
[//]: # Fetch from "pg-schema-diff version" if using the CLI or go.mod if using the library
24+
<!-- Fetch from "pg-schema-diff version" if using the CLI or go.mod if using the library -->
2525
pg-schema-diff version: Some sha or version
26-
[//]: # If you are using the CLI or using the library programatically
26+
<!-- If you are using the CLI or using the library programatically -->
2727
pg-schema-diff usage: [CLI | LIBRARY]
2828
Postgres version: [14, 15, 16, 17]
29-
[//]: # Only if relevant
29+
<!-- Only if relevant -->
3030
pg_dump of database: Gist link
3131

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ assignees: ''
88
---
99

1010
### Describe the feature
11-
[//]: # Provide a thorough description of the feature. If possible, include examples of the feature and its behavior. For example, if it's a new command flag, provide examples of using the command flag and expected outputs.
11+
<!-- Provide a thorough description of the feature. If possible, include examples of the feature and its behavior. For example, if it's a new command flag, provide examples of using the command flag and expected outputs. -->
1212

1313
### Motivation
14-
[//]: # Describe why you want this feature. This is important for assessing the priority of the feature and if alternative solutions exist.
14+
<!-- Describe why you want this feature. This is important for assessing the priority of the feature and if alternative solutions exist. -->

.github/pull_request_template.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
[//]: # README: Ensure you've read the CONTRIBUTING.MD and that your commits are signed
1+
<!-- README: Ensure you've read the CONTRIBUTING.MD and that your commits are signed -->
22

33
### Description
4-
[//]: # A clear and concise description of the purpose of this Pull Request. What is being changed? Include any relevant background for this change.
4+
<!-- A clear and concise description of the purpose of this Pull Request. What is being changed? Include any relevant background for this change. -->
55

66
### Motivation
7-
[//]: # Why you made these changes. Link to any relevant issues.
7+
<!-- Why you made these changes. Link to any relevant issues. -->
88

99
### Testing
10-
[//]: # Describe how you tested these changes
10+
<!-- Describe how you tested these changes -->

cmd/pg-schema-diff/apply_cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ func (suite *cmdTestSuite) TestApplyCmd() {
7878
})
7979
// The migration should have been successful. Assert it was.
8080
expectedDb := tempDbWithSchema(suite.T(), suite.pgEngine, tc.expectedSchemaDDL)
81-
expectedDbDump, err := pgdump.GetDump(expectedDb, pgdump.WithSchemaOnly())
81+
expectedDbDump, err := pgdump.GetDump(expectedDb, pgdump.WithSchemaOnly(), pgdump.WithRestrictKey(pgdump.FixedRestrictKey))
8282
suite.Require().NoError(err)
83-
fromDbDump, err := pgdump.GetDump(fromDb, pgdump.WithSchemaOnly())
83+
fromDbDump, err := pgdump.GetDump(fromDb, pgdump.WithSchemaOnly(), pgdump.WithRestrictKey(pgdump.FixedRestrictKey))
8484
suite.Require().NoError(err)
8585

8686
suite.Equal(expectedDbDump, fromDbDump)

cmd/pg-schema-diff/main_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type runCmdWithAssertionsParams struct {
3434
// saving schemas to a randomly generated temporary directory.
3535
dynamicArgs []dArgGenerator
3636

37+
// outputEquals is the exact string that stdout should equal.
38+
outputEquals string
3739
// outputContains is a list of substrings that are expected to be contained in the stdout output of the command.
3840
outputContains []string
3941
// expectErrContains is a list of substrings that are expected to be contained in the error returned by
@@ -72,6 +74,9 @@ func (suite *cmdTestSuite) runCmdWithAssertions(tc runCmdWithAssertionsParams) {
7274
suite.Contains(stdOutStr, o)
7375
}
7476
}
77+
if len(tc.outputEquals) > 0 {
78+
suite.Equal(tc.outputEquals, stdOutStr)
79+
}
7580
}
7681

7782
// dArgGenerator generates argument at the run-time of the test case...

cmd/pg-schema-diff/plan_cmd.go

Lines changed: 36 additions & 7 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,17 +162,23 @@ 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,
173168
}
174169

170+
outputFormatSql = outputFormat{
171+
identifier: "sql",
172+
convertToOutputString: planToSql,
173+
}
174+
175+
outputFormatPretty = outputFormat{
176+
identifier: "pretty",
177+
convertToOutputString: planToPrettyS,
178+
}
179+
175180
outputFormats = []outputFormat{
181+
outputFormatSql,
176182
outputFormatPretty,
177183
outputFormatJson,
178184
}
@@ -584,10 +590,33 @@ func hazardToPrettyS(hazard diff.MigrationHazard) string {
584590
}
585591
}
586592

593+
// planToJsonS converts the plan to JSON.
587594
func planToJsonS(plan diff.Plan) string {
588595
jsonData, err := json.MarshalIndent(plan, "", " ")
589596
if err != nil {
590597
panic(err)
591598
}
592599
return string(jsonData)
593600
}
601+
602+
// planToSql converts the plan to one large runnable SQL script.
603+
func planToSql(plan diff.Plan) string {
604+
sb := strings.Builder{}
605+
for i, stmt := range plan.Statements {
606+
sb.WriteString("/*\n")
607+
sb.WriteString(fmt.Sprintf("Statement %d\n", i))
608+
if len(stmt.Hazards) > 0 {
609+
for _, hazard := range stmt.Hazards {
610+
sb.WriteString(fmt.Sprintf(" - %s\n", hazardToPrettyS(hazard)))
611+
}
612+
}
613+
sb.WriteString("*/\n")
614+
sb.WriteString(fmt.Sprintf("SET SESSION statement_timeout = %d;\n", stmt.Timeout.Milliseconds()))
615+
sb.WriteString(fmt.Sprintf("SET SESSION lock_timeout = %d;\n", stmt.LockTimeout.Milliseconds()))
616+
sb.WriteString(fmt.Sprintf("%s;", stmt.DDL))
617+
if i < len(plan.Statements)-1 {
618+
sb.WriteString("\n\n")
619+
}
620+
}
621+
return sb.String()
622+
}

cmd/pg-schema-diff/plan_cmd_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func (suite *cmdTestSuite) TestPlanCmd() {
1212
args []string
1313
dynamicArgs []dArgGenerator
1414

15+
// outputEquals is the exact string that stdout should equal.
16+
outputEquals string
1517
// outputContains is a list of substrings that are expected to be contained in the stdout output of the command.
1618
outputContains []string
1719
// expectErrContains is a list of substrings that are expected to be contained in the error returned by
@@ -91,11 +93,43 @@ func (suite *cmdTestSuite) TestPlanCmd() {
9193
args: []string{"--from-dir", "some-dir", "--to-dir", "some-other-dir"},
9294
expectErrContains: []string{"at least one Postgres server"},
9395
},
96+
{
97+
name: "sql output format",
98+
args: []string{"--output-format", "sql"},
99+
dynamicArgs: []dArgGenerator{
100+
tempDsnDArg(suite.pgEngine, "temp-db-dsn", []string{""}),
101+
tempSchemaDirDArg("from-dir", []string{`
102+
CREATE TABLE foobar(
103+
bar TEXT,
104+
fizzbuzz TEXT
105+
);
106+
`}),
107+
tempSchemaDirDArg("to-dir", []string{`
108+
CREATE TABLE foobar(
109+
bar TEXT,
110+
fizzbuzz TEXT
111+
);
112+
CREATE INDEX bar_idx ON foobar(bar);
113+
CREATE INDEX fizzbuzz_idx ON foobar(fizzbuzz);
114+
`}),
115+
},
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",
117+
},
118+
{
119+
name: "invalid output format",
120+
args: []string{"--output-format", "invalid"},
121+
dynamicArgs: []dArgGenerator{
122+
tempDsnDArg(suite.pgEngine, "from-dsn", nil),
123+
tempDsnDArg(suite.pgEngine, "to-dsn", []string{"CREATE TABLE foobar()"}),
124+
},
125+
expectErrContains: []string{"invalid output format"},
126+
},
94127
} {
95128
suite.Run(tc.name, func() {
96129
suite.runCmdWithAssertions(runCmdWithAssertionsParams{
97130
args: append([]string{"plan"}, tc.args...),
98131
dynamicArgs: tc.dynamicArgs,
132+
outputEquals: tc.outputEquals,
99133
outputContains: tc.outputContains,
100134
expectErrContains: tc.expectErrContains,
101135
})

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/go-logfmt/logfmt v0.6.0
77
github.com/google/go-cmp v0.5.9
88
github.com/google/uuid v1.3.0
9+
github.com/hashicorp/go-version v1.7.0
910
github.com/jackc/pgx/v4 v4.18.2
1011
github.com/kr/pretty v0.3.1
1112
github.com/lib/pq v1.10.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
2929
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
3030
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
3131
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
32+
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
33+
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
3234
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
3335
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
3436
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=

internal/migration_acceptance_tests/acceptance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func runTest(t *testing.T, tc acceptanceTestCase) {
183183

184184
// Make sure the pgdump after running the migration is the same as the
185185
// pgdump from a database where we directly run the newSchemaDDL
186-
oldDbDump, err := pgdump.GetDump(oldDb, pgdump.WithSchemaOnly())
186+
oldDbDump, err := pgdump.GetDump(oldDb, pgdump.WithSchemaOnly(), pgdump.WithRestrictKey(pgdump.FixedRestrictKey))
187187
require.NoError(t, err)
188188

189189
newDbDump := directlyRunDDLAndGetDump(t, engine, tc.expectedDBSchemaDDL)
@@ -221,7 +221,7 @@ func directlyRunDDLAndGetDump(t *testing.T, engine *pgengine.Engine, ddl []strin
221221
defer newDb.DropDB()
222222
require.NoError(t, applyDDL(newDb, ddl))
223223

224-
newDbDump, err := pgdump.GetDump(newDb, pgdump.WithSchemaOnly())
224+
newDbDump, err := pgdump.GetDump(newDb, pgdump.WithSchemaOnly(), pgdump.WithRestrictKey(pgdump.FixedRestrictKey))
225225
require.NoError(t, err)
226226
return newDbDump
227227
}

0 commit comments

Comments
 (0)