forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_box.go
73 lines (66 loc) · 1.67 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
package pop
import (
"github.com/gobuffalo/packd"
"github.com/pkg/errors"
)
// MigrationBox is a wrapper around packr.Box and Migrator.
// This will allow you to run migrations from a packed box
// inside of a compiled binary.
type MigrationBox struct {
Migrator
Box packd.Walkable
}
// NewMigrationBox from a packr.Box and a Connection.
func NewMigrationBox(box packd.Walkable, c *Connection) (MigrationBox, error) {
fm := MigrationBox{
Migrator: NewMigrator(c),
Box: box,
}
runner := func(f packd.File) func(mf Migration, tx *Connection) error {
return func(mf Migration, tx *Connection) error {
content, err := 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 := fm.findMigrations(runner)
if err != nil {
return fm, err
}
return fm, nil
}
func (fm *MigrationBox) findMigrations(runner func(f packd.File) func(mf Migration, tx *Connection) error) error {
return fm.Box.Walk(func(p string, f packd.File) error {
info, err := f.FileInfo()
if err != nil {
return err
}
match, err := ParseMigrationFilename(info.Name())
if err != nil {
return err
}
if match == nil {
return nil
}
mf := Migration{
Path: p,
Version: match.Version,
Name: match.Name,
DBType: match.DBType,
Direction: match.Direction,
Type: match.Type,
Runner: runner(f),
}
fm.Migrations[mf.Direction] = append(fm.Migrations[mf.Direction], mf)
return nil
})
}