-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
migration_box.go
186 lines (161 loc) · 4.72 KB
/
migration_box.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package pkgerx
import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"text/template"
"github.com/gobuffalo/fizz"
"github.com/gobuffalo/pop/v6"
"github.com/markbates/pkger"
"github.com/pkg/errors"
"github.com/ory/x/logrusx"
)
type (
// MigrationBox is a wrapper around pkger.Dir and Migrator.
// This will allow you to run migrations from migrations packed
// inside of a compiled binary.
MigrationBox struct {
pop.Migrator
Dir pkger.Dir
l *logrusx.Logger
migrationContent MigrationContent
}
MigrationContent func(mf pop.Migration, c *pop.Connection, r io.Reader, usingTemplate bool) (string, error)
)
func templatingMigrationContent(params map[string]interface{}) func(pop.Migration, *pop.Connection, io.Reader, bool) (string, error) {
return func(mf pop.Migration, c *pop.Connection, r io.Reader, usingTemplate bool) (string, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return "", nil
}
content := ""
if usingTemplate {
t := template.New("migration")
t.Funcs(SQLTemplateFuncs)
t, err := t.Parse(string(b))
if err != nil {
return "", err
}
var bb bytes.Buffer
err = t.Execute(&bb, struct {
DialectDetails *pop.ConnectionDetails
Parameters map[string]interface{}
}{
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" {
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
}
}
func WithTemplateValues(v map[string]interface{}) func(*MigrationBox) *MigrationBox {
return func(m *MigrationBox) *MigrationBox {
m.migrationContent = templatingMigrationContent(v)
return m
}
}
func WithMigrationContentMiddleware(middleware func(content string, err error) (string, error)) func(*MigrationBox) *MigrationBox {
return func(m *MigrationBox) *MigrationBox {
prev := m.migrationContent
m.migrationContent = func(mf pop.Migration, c *pop.Connection, r io.Reader, usingTemplate bool) (string, error) {
return middleware(prev(mf, c, r, usingTemplate))
}
return m
}
}
// NewMigrationBox from a packr.Dir and a Connection.
//
// migrations, err := NewMigrationBox(pkger.Dir("/migrations"))
//
func NewMigrationBox(dir pkger.Dir, c *pop.Connection, l *logrusx.Logger, opts ...func(*MigrationBox) *MigrationBox) (*MigrationBox, error) {
mb := &MigrationBox{
Migrator: pop.NewMigrator(c),
Dir: dir,
l: l,
migrationContent: pop.MigrationContent,
}
for _, o := range opts {
mb = o(mb)
}
runner := func(f io.Reader) func(mf pop.Migration, tx *pop.Connection) error {
return func(mf pop.Migration, tx *pop.Connection) error {
content, err := mb.migrationContent(mf, tx, f, true)
if err != nil {
return errors.Wrapf(err, "error processing %s", mf.Path)
}
if content == "" {
return nil
}
err = tx.RawQuery(content).Exec()
if err != nil {
return errors.Wrapf(err, "error executing %s, sql: %s", mf.Path, content)
}
return nil
}
}
err := mb.findMigrations(runner)
if err != nil {
return mb, err
}
return mb, nil
}
func (fm *MigrationBox) findMigrations(runner func(f io.Reader) func(mf pop.Migration, tx *pop.Connection) error) error {
return pkger.Walk(string(fm.Dir), func(p string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
match, err := pop.ParseMigrationFilename(info.Name())
if err != nil {
if strings.HasPrefix(err.Error(), "unsupported dialect") {
fm.l.Debugf("Ignoring migration file %s because dialect is not supported: %s", info.Name(), err.Error())
return nil
}
return errors.WithStack(err)
}
if match == nil {
fm.l.Debugf("Ignoring migration file %s because it does not match the file pattern.", info.Name())
return nil
}
file, err := pkger.Open(p)
if err != nil {
return errors.WithStack(err)
}
defer file.Close()
content, err := ioutil.ReadAll(file)
if err != nil {
return errors.WithStack(err)
}
mf := pop.Migration{
Path: p,
Version: match.Version,
Name: match.Name,
DBType: match.DBType,
Direction: match.Direction,
Type: match.Type,
Runner: runner(bytes.NewReader(content)),
}
switch mf.Direction {
case "down":
fm.DownMigrations.Migrations = append(fm.DownMigrations.Migrations, mf)
case "up":
fm.UpMigrations.Migrations = append(fm.UpMigrations.Migrations, mf)
default:
return errors.Errorf("unknown direction %s", mf.Direction)
}
return nil
})
}