-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration.go
127 lines (106 loc) · 2.79 KB
/
migration.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package lightmigrate
import "sort"
// Direction is either up or down.
type Direction string
const (
// Down direction is used when migrations should be reverted.
Down Direction = "down"
// Up direction is the default direction for migrations.
Up Direction = "up"
)
// migrations wraps migration and has an internal index
// to keep track of migration order.
type migrations struct {
// Store the order of the versions
index []uint64
// Store migrations for each version
migrations map[uint64]map[Direction]*migration
}
// migration meta object
type migration struct {
// Version is the version of this migration.
Version uint64
// Identifier can be any string that helps identifying
// this migration in the source.
Identifier string
// Direction is either Up or Down.
Direction Direction
// Raw holds the raw location path to this migration in source.
// ReadUp and ReadDown will use this.
Raw string
}
func newMigrations() *migrations {
return &migrations{
index: make([]uint64, 0),
migrations: make(map[uint64]map[Direction]*migration),
}
}
func (i *migrations) Append(m *migration) (ok bool) {
if m == nil {
return false
}
if i.migrations[m.Version] == nil {
i.migrations[m.Version] = make(map[Direction]*migration)
}
// reject duplicate versions
if _, dup := i.migrations[m.Version][m.Direction]; dup {
return false
}
i.migrations[m.Version][m.Direction] = m
i.buildIndex()
return true
}
func (i *migrations) buildIndex() {
i.index = make([]uint64, 0, len(i.migrations))
for version := range i.migrations {
i.index = append(i.index, version)
}
sort.Slice(i.index, func(x, y int) bool {
return i.index[x] < i.index[y]
})
}
func (i *migrations) First() (version uint64, ok bool) {
if len(i.index) == 0 {
return 0, false
}
return i.index[0], true
}
func (i *migrations) Prev(version uint64) (prevVersion uint64, ok bool) {
pos := i.findPos(version)
if pos >= 1 && len(i.index) > pos-1 {
return i.index[pos-1], true
}
return 0, false
}
func (i *migrations) Next(version uint64) (nextVersion uint64, ok bool) {
pos := i.findPos(version)
if len(i.index) > pos+1 {
return i.index[pos+1], true
}
return 0, false
}
func (i *migrations) Up(version uint64) (m *migration, ok bool) {
if _, ok := i.migrations[version]; ok {
if mx, ok := i.migrations[version][Up]; ok {
return mx, true
}
}
return nil, false
}
func (i *migrations) Down(version uint64) (m *migration, ok bool) {
if _, ok := i.migrations[version]; ok {
if mx, ok := i.migrations[version][Down]; ok {
return mx, true
}
}
return nil, false
}
func (i *migrations) findPos(version uint64) int {
if len(i.index) > 0 {
ix := sort.Search(len(i.index), func(j int) bool { return i.index[j] >= version })
if ix < len(i.index) && i.index[ix] == version {
return ix
}
}
return -1
}