forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigration_content.go
More file actions
40 lines (34 loc) · 867 Bytes
/
migration_content.go
File metadata and controls
40 lines (34 loc) · 867 Bytes
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
package pop
import (
"bytes"
"fmt"
"io"
"text/template"
"github.com/gobuffalo/fizz"
)
// MigrationContent returns the content of a migration.
func MigrationContent(mf Migration, c *Connection, r io.Reader, usingTemplate bool) (string, error) {
b, err := io.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 "", fmt.Errorf("could not execute migration template %s: %w", mf.Path, err)
}
content = bb.String()
} else {
content = string(b)
}
if mf.Type == "fizz" {
content, err = fizz.AString(content, c.Dialect.FizzTranslator())
if err != nil {
return "", fmt.Errorf("could not fizz the migration %s: %w", mf.Path, err)
}
}
return content, nil
}