-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd_up.go
49 lines (38 loc) · 1.03 KB
/
cmd_up.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
package main
import (
"log"
"os"
"path/filepath"
)
var upCmd = &Command{
Name: "up",
Usage: "",
Summary: "Migrate the DB to the most recent version available",
Help: `Run with the "--outOfOrder" flag to also run older migrations that were previously missed`,
}
var outOfOrder = upCmd.Flag.Bool("outOfOrder", false, "Allow previously missed migrations to be run out of order")
func upRun(cmd *Command, args ...string) {
conf, err := MakeDBConf()
if err != nil {
log.Fatal(err)
}
target := mostRecentVersionAvailable(conf.MigrationsDir)
runMigrations(conf, conf.MigrationsDir, target, *outOfOrder)
}
// helper to identify the most recent possible version
// within a folder of migration scripts
func mostRecentVersionAvailable(dirpath string) int64 {
mostRecent := int64(-1)
filepath.Walk(dirpath, func(name string, info os.FileInfo, err error) error {
if v, e := numericComponent(name); e == nil {
if v > mostRecent {
mostRecent = v
}
}
return nil
})
return mostRecent
}
func init() {
upCmd.Run = upRun
}