-
Notifications
You must be signed in to change notification settings - Fork 1
/
direction.go
81 lines (73 loc) · 1.56 KB
/
direction.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
package internal
import (
"fmt"
"strings"
)
// Direction describes which way the change goes.
type Direction uint8
const (
// DirUnknown is a fallback value for an undetermined direction.
DirUnknown Direction = iota
// DirForward is like migrate up, typically the change you want to apply to
// the DB.
DirForward
// DirReverse is like migrate down; used for rollbacks. Not all changes can
// be rolled back.
DirReverse
)
func (d Direction) String() string {
return [...]string{"unknown", "forward", "reverse"}[d]
}
// Allowed names for directions in migration filenames.
var (
ForwardDirections = []string{
"forward",
"migrate",
"up",
}
ReverseDirections = []string{
"reverse",
"rollback",
"down",
}
)
func validateDirectionLabel(okVals []string, val string) (err error) {
var ok bool
for _, okVal := range okVals {
if val == okVal {
ok = true
break
}
}
if !ok {
err = fmt.Errorf(
"invalid value (%q); should be one of: %s",
val, strings.Join(okVals, ", "),
)
}
return
}
// Indirection is some glue to help determine the direction of a migration,
// usually from a filename with an alias for a direction.
type Indirection struct {
Value Direction
Label string
}
func parseIndirection(basename string) (ind Indirection) {
lo := strings.ToLower(basename)
for _, pre := range ForwardDirections {
if strings.HasPrefix(lo, pre) {
ind.Value = DirForward
ind.Label = pre
return
}
}
for _, pre := range ReverseDirections {
if strings.HasPrefix(lo, pre) {
ind.Value = DirReverse
ind.Label = pre
return
}
}
return
}