This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
forked from uadmin/uadmin
/
migration_interfaces.go
283 lines (243 loc) · 6.59 KB
/
migration_interfaces.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package core
import (
"container/list"
"fmt"
"sort"
"strings"
)
type IMigrationRegistry interface {
GetByName(migrationName string) (IMigration, error)
AddMigration(migration IMigration)
GetSortedMigrations() MigrationList
}
type IMigration interface {
Up(uadminDatabase *UadminDatabase) error
Down(uadminDatabase *UadminDatabase) error
GetName() string
GetID() int64
Deps() []string
}
type IMigrationNode interface {
IsApplied() bool
GetMigration() IMigration
SetItAsRoot()
IsRoot() bool
AddChild(node IMigrationNode)
AddDep(node IMigrationNode)
GetChildrenCount() int
GetChildren() *list.List
GetDeps() *list.List
TraverseDeps(migrationList []int64, depList MigrationDepList) MigrationDepList
TraverseChildren(migrationList []int64) []int64
IsDummy() bool
Downgrade(uadminDatabase *UadminDatabase) error
Apply(uadminDatabase *UadminDatabase) error
}
type IMigrationTree interface {
GetRoot() IMigrationNode
SetRoot(root IMigrationNode)
GetNodeByMigrationID(ID int64) (IMigrationNode, error)
AddNode(node IMigrationNode) error
TreeBuilt()
IsTreeBuilt() bool
}
func GetBluePrintNameFromMigrationName(migrationName string) string {
return strings.Split(migrationName, ".")[0]
}
type MigrationNode struct {
Deps *list.List
Node IMigration
Children *list.List
applied bool
isRoot bool
dummy bool
}
func (n MigrationNode) IsDummy() bool {
return n.dummy
}
func (n MigrationNode) Apply(uadminDatabase *UadminDatabase) error {
res := n.Node.Up(uadminDatabase)
if res == nil {
n.applied = true
}
return res
}
func (n MigrationNode) Downgrade(uadminDatabase *UadminDatabase) error {
res := n.Node.Down(uadminDatabase)
if res == nil {
n.applied = false
}
return res
}
func (n MigrationNode) GetMigration() IMigration {
return n.Node
}
func (n MigrationNode) GetChildren() *list.List {
return n.Children
}
func (n MigrationNode) GetDeps() *list.List {
return n.Deps
}
func (n MigrationNode) GetChildrenCount() int {
return n.Children.Len()
}
func (n MigrationNode) IsApplied() bool {
return n.applied
}
func (n MigrationNode) SetItAsRoot() {
n.isRoot = true
}
func (n MigrationNode) IsRoot() bool {
return n.isRoot
}
func (n MigrationNode) AddChild(node IMigrationNode) {
n.Children.PushBack(node)
}
func (n MigrationNode) AddDep(node IMigrationNode) {
n.Deps.PushBack(node)
}
func (n MigrationNode) TraverseDeps(migrationList []int64, depList MigrationDepList) MigrationDepList {
for l := n.GetDeps().Front(); l != nil; l = l.Next() {
migration := l.Value.(IMigrationNode)
if migration.IsDummy() {
continue
}
migrationName := l.Value.(IMigrationNode).GetMigration().GetID()
if !ContainsInt64(migrationList, migrationName) && !ContainsInt64(depList, migrationName) {
depList = append(depList, l.Value.(IMigrationNode).GetMigration().GetID())
depList = l.Value.(IMigrationNode).TraverseDeps(migrationList, depList)
}
}
return depList
}
func (n MigrationNode) TraverseChildren(migrationList []int64) []int64 {
for l := n.GetChildren().Front(); l != nil; l = l.Next() {
migration := l.Value.(IMigrationNode)
if migration.IsDummy() {
continue
}
migrationName := l.Value.(IMigrationNode).GetMigration().GetID()
if !ContainsInt64(migrationList, migrationName) {
migrationList = append(migrationList, l.Value.(IMigrationNode).GetMigration().GetID())
migrationDepList := n.TraverseDeps(migrationList, make(MigrationDepList, 0))
sort.Slice(migrationDepList, func(i int, j int) bool {
return i > j
})
for _, m := range migrationDepList {
migrationList = append(migrationList, m)
}
migrationList = n.TraverseChildren(migrationList)
}
}
return migrationList
}
func NewMigrationNode(dep IMigrationNode, node IMigration, child IMigrationNode) IMigrationNode {
depsList := list.New()
if dep != nil {
depsList.PushBack(dep)
}
childrenList := list.New()
if child != nil {
childrenList.PushBack(child)
}
return &MigrationNode{
Deps: depsList,
Node: node,
Children: childrenList,
applied: false,
dummy: false,
isRoot: false,
}
}
func NewMigrationRootNode() IMigrationNode {
return &MigrationNode{
Deps: list.New(),
Node: nil,
Children: list.New(),
applied: false,
dummy: true,
isRoot: true,
}
}
type MigrationTree struct {
Root IMigrationNode
nodes map[int64]IMigrationNode
treeBuilt bool
}
func (t MigrationTree) TreeBuilt() {
t.treeBuilt = true
}
func (t MigrationTree) IsTreeBuilt() bool {
return t.treeBuilt
}
func (t MigrationTree) GetNodeByMigrationID(migrationID int64) (IMigrationNode, error) {
node, ok := t.nodes[migrationID]
if ok {
return node, nil
}
return nil, fmt.Errorf("no node with name %d has been found", migrationID)
}
func (t MigrationTree) AddNode(node IMigrationNode) error {
_, ok := t.nodes[node.GetMigration().GetID()]
if ok {
// return fmt.Errorf("Migration with name %s has been added to tree before", node.GetMigration().GetName())
return nil
}
t.nodes[node.GetMigration().GetID()] = node
return nil
}
func (t MigrationTree) GetRoot() IMigrationNode {
return t.Root
}
func (t MigrationTree) SetRoot(root IMigrationNode) {
root.SetItAsRoot()
t.Root = root
}
type MigrationList []IMigration
func (m MigrationList) Len() int { return len(m) }
func (m MigrationList) Less(i, j int) bool {
return m[i].GetID() < m[j].GetID()
}
func (m MigrationList) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
type MigrationDepList []int64
func (m MigrationDepList) Len() int { return len(m) }
func (m MigrationDepList) Less(i, j int) bool {
return i < j
}
func (m MigrationDepList) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
type MigrationRegistry struct {
migrations map[string]IMigration
}
func (r MigrationRegistry) AddMigration(migration IMigration) {
r.migrations[migration.GetName()] = migration
}
func (r MigrationRegistry) GetByName(migrationName string) (IMigration, error) {
migration, ok := r.migrations[migrationName]
if ok {
return migration, nil
}
return nil, fmt.Errorf("No migration with name %s exists", migrationName)
}
func (r MigrationRegistry) GetSortedMigrations() MigrationList {
sortedMigrations := make(MigrationList, len(r.migrations))
i := 0
for _, migration := range r.migrations {
sortedMigrations[i] = migration
i++
}
sort.Slice(sortedMigrations, func(i int, j int) bool {
return sortedMigrations[i].GetID() < sortedMigrations[j].GetID()
})
return sortedMigrations
}
func NewMigrationRegistry() *MigrationRegistry {
return &MigrationRegistry{
migrations: make(map[string]IMigration),
}
}
func NewMigrationTree() IMigrationTree {
return &MigrationTree{
Root: NewMigrationRootNode(),
nodes: make(map[int64]IMigrationNode),
}
}