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

internal/tool: add templates for liquibase compatible migration files #696

Merged
merged 1 commit into from
Apr 11, 2022
Merged
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
17 changes: 17 additions & 0 deletions internal/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var (
// funcs contains the template.FuncMap for the different formatters.
funcs = template.FuncMap{
"inc": func(x int) int { return x + 1 },
// now format the current time in a lexicographically ascending order while maintaining human readability.
"now": func() string { return time.Now().Format("20060102150405") },
"rev": reverse,
Expand Down Expand Up @@ -51,6 +52,22 @@ func NewFlywayFormatter() (migrate.Formatter, error) {
)
}

// NewLiquibaseFormatter returns a migrate.Formatter computable with Liquibase.
func NewLiquibaseFormatter() (migrate.Formatter, error) { // TODO(masseelch): add dbms property (meta data needed)
return templateFormatter(
"{{ now }}{{ with .Name }}_{{ . }}{{ end }}.sql",
`{{- $now := now -}}
--liquibase formatted sql

{{- range $index, $change := .Changes }}
--changeset atlas:{{ $now }}-{{ inc $index }}
{{ with $change.Comment }}--comment: {{ . }}{{ end }}
{{ $change.Cmd }};
{{ with $change.Reverse }}--rollback: {{ . }};{{ end }}
{{ end }}`,
)
}

// templateFormatter parses the given templates and passes them on to the migrate.NewTemplateFormatter.
func templateFormatter(templates ...string) (fmt migrate.Formatter, err error) {
tpls := make([]*template.Template, len(templates))
Expand Down
18 changes: 18 additions & 0 deletions internal/tool/tool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tool_test

import (
"fmt"
"io/fs"
"testing"
"time"
Expand Down Expand Up @@ -80,6 +81,23 @@ DROP TABLE t1 IF EXISTS;
`,
},
},
{
"liquibase",
tool.NewLiquibaseFormatter,
map[string]string{
v + "_tooling-plan.sql": fmt.Sprintf(`--liquibase formatted sql
--changeset atlas:%s-1
--comment: create table t1
CREATE TABLE t1(c int);
--rollback: DROP TABLE t1 IF EXISTS;

--changeset atlas:%s-2
--comment: create table t2
CREATE TABLE t2(c int);
--rollback: DROP TABLE t2;
`, v, v),
},
},
} {
t.Run(tt.name, func(t *testing.T) {
d := dir(t)
Expand Down