-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcmd_down.go
66 lines (50 loc) · 1.14 KB
/
cmd_down.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
)
var downCmd = &Command{
Name: "down",
Usage: "",
Summary: "Roll back the version by 1",
Help: `down extended help here...`,
}
func downRun(cmd *Command, args ...string) {
conf, err := MakeDBConf()
if err != nil {
log.Fatal(err)
}
current := getDBVersion(conf)
previous, earliest := getPreviousVersion(conf.MigrationsDir, current)
if current == 0 {
fmt.Println("db is empty, can't go down.")
return
}
// if we're at the earliest version, indicate that the
// only available step is to roll back to an empty database
if current == earliest {
previous = 0
}
runMigrations(conf, conf.MigrationsDir, previous, false)
}
func getPreviousVersion(dirpath string, version int64) (previous, earliest int64) {
previous = -1
earliest = (1 << 31) - 1
filepath.Walk(dirpath, func(name string, info os.FileInfo, err error) error {
if v, e := numericComponent(name); e == nil {
if v > previous && v < version {
previous = v
}
if v < earliest {
earliest = v
}
}
return nil
})
return previous, earliest
}
func init() {
downCmd.Run = downRun
}