-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan.go
57 lines (49 loc) · 1 KB
/
plan.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
package migration
import (
"fmt"
)
// a migrationPlan contains the information required to
// migrate to a version from the previous version, and back
// down again.
type migrationPlan struct {
id VersionID
up action
down action
errs Errors
}
func newPlan(def *Definition, plans map[VersionID]*migrationPlan) *migrationPlan {
p := &migrationPlan{
id: def.id,
errs: def.errs(),
}
if def.upAction != nil {
def.upAction(&p.up)
}
if def.downAction != nil {
def.downAction(&p.down)
}
addError := func(s string) {
p.errs = append(p.errs, &Error{
Version: p.id,
Description: s,
})
}
replayUp := func(a *action) {
if a.replayUp != nil {
replayID := *a.replayUp
if replayID >= p.id {
addError("replay must specify an earlier version")
return
}
prevPlan := plans[replayID]
if prevPlan == nil {
addError(fmt.Sprintf("replay refers to unknown version %d", replayID))
} else {
*a = prevPlan.up
}
}
}
replayUp(&p.up)
replayUp(&p.down)
return p
}