Skip to content

Commit 82e1a87

Browse files
Simplify tests. Add hazards
1 parent 95dc472 commit 82e1a87

File tree

3 files changed

+46
-286
lines changed

3 files changed

+46
-286
lines changed

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: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ var (
174174

175175
outputFormatSql = outputFormat{
176176
identifier: "sql",
177-
convertToOutputString: planToSqlS,
177+
convertToOutputString: planToSql,
178178
}
179179

180180
outputFormats = []outputFormat{
@@ -590,6 +590,7 @@ func hazardToPrettyS(hazard diff.MigrationHazard) string {
590590
}
591591
}
592592

593+
// planToJsonS converts the plan to JSON.
593594
func planToJsonS(plan diff.Plan) string {
594595
jsonData, err := json.MarshalIndent(plan, "", " ")
595596
if err != nil {
@@ -598,18 +599,21 @@ func planToJsonS(plan diff.Plan) string {
598599
return string(jsonData)
599600
}
600601

601-
func planToSqlS(plan diff.Plan) string {
602+
// planToSql converts the plan to one large runnable SQL script.
603+
func planToSql(plan diff.Plan) string {
602604
sb := strings.Builder{}
603-
604-
if len(plan.Statements) == 0 {
605-
return ""
606-
}
607-
608-
var stmtStrs []string
609-
for _, stmt := range plan.Statements {
610-
stmtStrs = append(stmtStrs, statementToPrettyS(stmt))
605+
for i, stmt := range plan.Statements {
606+
if len(stmt.Hazards) > 0 {
607+
for _, hazard := range stmt.Hazards {
608+
sb.WriteString(fmt.Sprintf("-- Hazard %s\n", hazardToPrettyS(hazard)))
609+
}
610+
}
611+
sb.WriteString(fmt.Sprintf("SET SESSION statement_timeout = %d;\n", stmt.Timeout.Milliseconds()))
612+
sb.WriteString(fmt.Sprintf("SET SESSION lock_timeout = %d;\n", stmt.LockTimeout.Milliseconds()))
613+
sb.WriteString(fmt.Sprintf("%s;", stmt.DDL))
614+
if i < len(plan.Statements)-1 {
615+
sb.WriteString("\n\n")
616+
}
611617
}
612-
sb.WriteString(strings.Join(stmtStrs, "\n\n"))
613-
614618
return sb.String()
615619
}

0 commit comments

Comments
 (0)