Skip to content

Commit d9b5b99

Browse files
authored
sql/migrate: support custom Delimiter in Plan (#3238)
* sql/migrate: support custom Delimiter in Plan * chore: fixed review comment
1 parent 5ec55e7 commit d9b5b99

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

sql/migrate/dir.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,22 @@ func CheckVersion(v string) error {
528528
return nil
529529
}
530530

531+
// delim returns a directive with the given delimiter.
532+
func delim(s string) string {
533+
if s == "" {
534+
return ""
535+
}
536+
// Escape delimiters. e.g. "\n" => "\\n".
537+
s = strings.NewReplacer("\n", `\n`, "\r", `\r`, "\t", `\t`).Replace(s)
538+
return fmt.Sprintf("-- atlas:%s %s", directiveDelimiter, s)
539+
}
540+
531541
var (
532542
// templateFunc contains the template.FuncMap for the DefaultFormatter.
533543
templateFuncs = template.FuncMap{
534544
"upper": strings.ToUpper,
535545
"now": NewVersion,
546+
"delim": delim,
536547
}
537548
// DefaultFormatter is a default implementation for Formatter.
538549
DefaultFormatter = TemplateFormatter{
@@ -541,7 +552,7 @@ var (
541552
"{{ with .Version }}{{ . }}{{ else }}{{ now }}{{ end }}{{ with .Name }}_{{ . }}{{ end }}.sql",
542553
)),
543554
C: template.Must(template.New("").Funcs(templateFuncs).Parse(
544-
`{{ range .Changes }}{{ with .Comment }}{{ printf "-- %s%s\n" (slice . 0 1 | upper ) (slice . 1) }}{{ end }}{{ printf "%s;\n" .Cmd }}{{ end }}`,
555+
`{{ with .Delimiter }}{{ delim . | printf "%s\n\n" }}{{ end }}{{ range .Changes }}{{ with .Comment }}{{ printf "-- %s%s\n" (slice . 0 1 | upper ) (slice . 1) }}{{ end }}{{ printf "%s%s\n" .Cmd (or $.Delimiter ";") }}{{ end }}`,
545556
)),
546557
},
547558
}

sql/migrate/migrate.go

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ type (
3434

3535
// Changes defines the list of changeset in the plan.
3636
Changes []*Change
37+
38+
// Delimiter to use for separating statements.
39+
Delimiter string
3740
}
3841

3942
// A Change of migration.

sql/migrate/migrate_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ func TestPlanner_WritePlan(t *testing.T) {
7474
require.Equal(t, countFiles(t, d), 3)
7575
requireFileEqual(t, d, "add_t1_and_t2.up.sql", "CREATE TABLE t1(c int)\nCREATE TABLE t2(c int)\n")
7676
requireFileEqual(t, d, "add_t1_and_t2.down.sql", "DROP TABLE t1 IF EXISTS\nDROP TABLE t2\n")
77+
78+
// With custom delimiter.
79+
plan.Delimiter = "\nGO"
80+
pl = migrate.NewPlanner(nil, d, migrate.PlanWithChecksum(false))
81+
require.NotNil(t, pl)
82+
require.NoError(t, pl.WritePlan(plan))
83+
v = time.Now().UTC().Format("20060102150405")
84+
require.Equal(t, countFiles(t, d), 3)
85+
requireFileEqual(t, d, v+"_add_t1_and_t2.sql", "-- atlas:delimiter \\nGO\n\nCREATE TABLE t1(c int)\nGO\nCREATE TABLE t2(c int)\nGO\n")
7786
}
7887

7988
func TestPlanner_WriteCheckpoint(t *testing.T) {

0 commit comments

Comments
 (0)