Skip to content
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

sql/migrate: fixed delimiter on the same line with the line comment #3334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion sql/migrate/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,28 @@ func delim(s string) string {
return fmt.Sprintf("-- atlas:%s %s", directiveDelimiter, s)
}

// stmt returns a statement with the given delimiter.
func stmt(cmd, delim string) string {
if cmd == "" {
return ""
}
if delim == "" {
delim = delimiter
}
if nl := strings.LastIndexByte(cmd, '\n'); nl != -1 && strings.HasPrefix(cmd[nl+1:], directivePrefixSQL) {
// Put delimiter on the newline, to separate it from the line comment
return fmt.Sprintf("%s\n%s\n", cmd, delim)
}
return fmt.Sprintf("%s%s\n", cmd, delim)
}

var (
// templateFunc contains the template.FuncMap for the DefaultFormatter.
templateFuncs = template.FuncMap{
"upper": strings.ToUpper,
"now": NewVersion,
"delim": delim,
"stmt": stmt,
}
// DefaultFormatter is a default implementation for Formatter.
DefaultFormatter = TemplateFormatter{
Expand All @@ -552,7 +568,7 @@ var (
"{{ with .Version }}{{ . }}{{ else }}{{ now }}{{ end }}{{ with .Name }}_{{ . }}{{ end }}.sql",
)),
C: template.Must(template.New("").Funcs(templateFuncs).Parse(
`{{ 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 }}`,
`{{ with .Delimiter }}{{ delim . | printf "%s\n\n" }}{{ end }}{{ range .Changes }}{{ with .Comment }}{{ printf "-- %s%s\n" (slice . 0 1 | upper ) (slice . 1) }}{{ end }}{{ stmt .Cmd $.Delimiter }}{{ end }}`,
)),
},
}
Expand Down
31 changes: 31 additions & 0 deletions sql/migrate/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,37 @@ func TestHashSum(t *testing.T) {
require.NotContains(t, string(c), "exclude_2.sql")
}

func TestPlanDelimiter(t *testing.T) {
var cases = []struct {
Cmd string
Delimiter string
Expected string
}{
{"", "", ""},
{"\n", "", "\n;\n"},
{"cmd\n-- comment", "", "cmd\n-- comment\n;\n"},
{"cmd\n-- comment", "\nGO", "-- atlas:delimiter \\nGO\n\ncmd\n-- comment\n\nGO\n"},
}
d := &migrate.MemDir{}
pl := migrate.NewPlanner(nil, d, migrate.PlanWithChecksum(false))
require.NotNil(t, pl)
for _, c := range cases {
plan := &migrate.Plan{
Version: "1",
Name: "plan",
Delimiter: c.Delimiter,
Changes: []*migrate.Change{{
Cmd: c.Cmd,
}},
}
require.NoError(t, pl.WritePlan(plan))
files, err := d.Files()
require.NoError(t, err)
require.Equal(t, 1, len(files))
require.Equal(t, c.Expected, string(files[0].Bytes()))
}
}

var (
//go:embed testdata/migrate/atlas.sum
hash []byte
Expand Down
Loading