Skip to content

Commit

Permalink
sql/migrate: allow formatting template output to any writer
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Jul 22, 2024
1 parent 2f21932 commit 6b9a8c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions sql/migrate/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,20 @@ func (t TemplateFormatter) Format(plan *Plan) ([]File, error) {
return files, nil
}

// FormatTo calls Format and writes the files' content to the given writer.
func (t TemplateFormatter) FormatTo(plan *Plan, w io.Writer) error {
files, err := t.Format(plan)
if err != nil {
return err
}
for _, f := range files {
if _, err := w.Write(f.Bytes()); err != nil {
return err
}
}
return nil
}

// HashFileName of the migration directory integrity sum file.
const HashFileName = "atlas.sum"

Expand Down
15 changes: 15 additions & 0 deletions sql/migrate/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,21 @@ func TestDirTar(t *testing.T) {
require.Equal(t, "create table t(c int);", string(files[0].Bytes()))
}

func TestDefaultFormatter_FormatTo(t *testing.T) {
var b bytes.Buffer
err := migrate.DefaultFormatter.FormatTo(&migrate.Plan{
Changes: []*migrate.Change{
{Cmd: "create table t1(c int)"},
{Cmd: "create table t2(c int)", Comment: "create table"},
},
}, &b)
require.NoError(t, err)
require.Equal(t, `create table t1(c int);
-- Create table
create table t2(c int);
`, b.String())
}

func fileNames(r io.Reader) ([]string, error) {
var out []string
tr := tar.NewReader(r)
Expand Down

0 comments on commit 6b9a8c1

Please sign in to comment.