forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_content.go
41 lines (35 loc) · 898 Bytes
/
migration_content.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package pop
import (
"bytes"
"io"
"io/ioutil"
"text/template"
"github.com/gobuffalo/fizz"
"github.com/pkg/errors"
)
// MigrationContent returns the content of a migration.
func MigrationContent(mf Migration, c *Connection, r io.Reader, usingTemplate bool) (string, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return "", nil
}
content := ""
if usingTemplate {
t := template.Must(template.New("migration").Parse(string(b)))
var bb bytes.Buffer
err = t.Execute(&bb, c.Dialect.Details())
if err != nil {
return "", errors.Wrapf(err, "could not execute migration template %s", mf.Path)
}
content = bb.String()
} else {
content = string(b)
}
if mf.Type == "fizz" {
content, err = fizz.AString(content, c.Dialect.FizzTranslator())
if err != nil {
return "", errors.Wrapf(err, "could not fizz the migration %s", mf.Path)
}
}
return content, nil
}