Repository navigation
Expand file tree
/
Copy pathstatus.go
More file actions
82 lines (64 loc) · 1.61 KB
/
Copy pathstatus.go
File metadata and controls
82 lines (64 loc) · 1.61 KB
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
package migrate
import (
"sort"
)
type MigrationStatus struct {
// Version is the version of this migration.
Version uint64 `json:"-"`
// Name helps finding this migration in the source folder
Name string `json:"-"`
// Check if the migration is applied on the cluster
IsApplied bool `json:"database_status"`
// Check if the migration is present on the local.
IsPresent bool `json:"source_status"`
IsDirty bool `json:"-"`
}
type Status struct {
Index uint64Slice `json:"migrations"`
Migrations map[uint64]*MigrationStatus `json:"status"`
}
func NewStatus() *Status {
return &Status{
Index: make(uint64Slice, 0),
Migrations: make(map[uint64]*MigrationStatus),
}
}
func (i *Status) Append(m *MigrationStatus) (ok bool) {
if m == nil {
return false
}
if i.Migrations[m.Version] == nil {
i.Migrations[m.Version] = m
} else {
// If the Version already exists
i.Migrations[m.Version].IsApplied = m.IsApplied
}
i.buildIndex()
return true
}
func (i *Status) buildIndex() {
i.Index = make(uint64Slice, 0)
for version := range i.Migrations {
i.Index = append(i.Index, version)
}
sort.Sort(i.Index)
}
func (i *Status) Read(version uint64) (m *MigrationStatus, ok bool) {
if mx, ok := i.Migrations[version]; ok {
return mx, true
}
return nil, false
}
type uint64Slice []uint64
func (s uint64Slice) Len() int {
return len(s)
}
func (s uint64Slice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s uint64Slice) Less(i, j int) bool {
return s[i] < s[j]
}
func (s uint64Slice) Search(x uint64) int {
return sort.Search(len(s), func(i int) bool { return s[i] >= x })
}