-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
file_migrator.go
96 lines (88 loc) · 2.27 KB
/
file_migrator.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
package pop
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/pop/v6/logging"
)
// FileMigrator is a migrator for SQL and Fizz
// files on disk at a specified path.
type FileMigrator struct {
Migrator
Path string
}
// NewFileMigrator for a path and a Connection
func NewFileMigrator(path string, c *Connection) (FileMigrator, error) {
fm := FileMigrator{
Migrator: NewMigrator(c),
Path: path,
}
fm.SchemaPath = path
runner := func(mf Migration, tx *Connection) error {
f, err := os.Open(mf.Path)
if err != nil {
return err
}
defer f.Close()
content, err := MigrationContent(mf, tx, f, true)
if err != nil {
return fmt.Errorf("error processing %s: %w", mf.Path, err)
}
if content == "" {
return nil
}
_, err = tx.Store.Exec(content)
if err != nil {
return fmt.Errorf("error executing %s, sql: %s: %w", mf.Path, content, err)
}
return nil
}
err := fm.findMigrations(runner)
if err != nil {
return fm, err
}
return fm, nil
}
func (fm *FileMigrator) findMigrations(runner func(mf Migration, tx *Connection) error) error {
dir := fm.Path
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
// directory doesn't exist
return nil
}
return filepath.Walk(dir, func(p string, info os.FileInfo, err error) error {
if !info.IsDir() {
match, err := ParseMigrationFilename(info.Name())
if err != nil {
if strings.HasPrefix(err.Error(), "unsupported dialect") {
log(logging.Warn, "ignoring migration file with %s", err.Error())
return nil
}
return err
}
if match == nil {
log(logging.Warn, "ignoring file %s because it does not match the migration file pattern", info.Name())
return nil
}
mf := Migration{
Path: p,
Version: match.Version,
Name: match.Name,
DBType: match.DBType,
Direction: match.Direction,
Type: match.Type,
Runner: runner,
}
switch mf.Direction {
case "up":
fm.UpMigrations.Migrations = append(fm.UpMigrations.Migrations, mf)
case "down":
fm.DownMigrations.Migrations = append(fm.DownMigrations.Migrations, mf)
default:
// the regex only matches `(up|down)` for direction, so a panic here is appropriate
panic("got unknown migration direction " + mf.Direction)
}
}
return nil
})
}