-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
migration_content.go
61 lines (55 loc) · 1.66 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package popx
import (
"bytes"
"text/template"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/pop/v6"
"github.com/pkg/errors"
)
func ParameterizedMigrationContent(params map[string]interface{}) func(mf Migration, c *pop.Connection, r []byte, usingTemplate bool) (string, error) {
return func(mf Migration, c *pop.Connection, b []byte, usingTemplate bool) (string, error) {
content := ""
if usingTemplate {
t := template.New("migration")
t.Funcs(SQLTemplateFuncs)
t, err := t.Parse(string(b))
if err != nil {
return "", errors.Wrapf(err, "could not parse template %s", mf.Path)
}
var bb bytes.Buffer
err = t.Execute(&bb, struct {
IsSQLite bool
IsCockroach bool
IsMySQL bool
IsMariaDB bool
IsPostgreSQL bool
DialectDetails *pop.ConnectionDetails
Parameters map[string]interface{}
}{
IsSQLite: c.Dialect.Name() == "sqlite3",
IsCockroach: c.Dialect.Name() == "cockroach",
IsMySQL: c.Dialect.Name() == "mysql",
IsMariaDB: c.Dialect.Name() == "mariadb",
IsPostgreSQL: c.Dialect.Name() == "postgres",
DialectDetails: c.Dialect.Details(),
Parameters: params,
})
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" {
var err error
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
}
}