-
Notifications
You must be signed in to change notification settings - Fork 30
/
migrate.go
91 lines (79 loc) · 2.01 KB
/
migrate.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
package v8
import (
"encoding/json"
liberr "github.com/jortel/go-utils/error"
"github.com/jortel/go-utils/logr"
v7 "github.com/konveyor/tackle2-hub/migration/v7/model"
"github.com/konveyor/tackle2-hub/migration/v8/model"
"gorm.io/gorm"
)
var log = logr.WithName("migration|v8")
type Migration struct{}
func (r Migration) Apply(db *gorm.DB) (err error) {
result := db.Model(model.Setting{}).Where("key = ?", "ui.ruleset.order").Update("key", "ui.target.order")
if result.Error != nil {
err = liberr.Wrap(err)
return
}
oldCustomRuleSets := []v7.RuleSet{}
result = db.Find(&oldCustomRuleSets, "uuid IS NULL")
if result.Error != nil {
err = liberr.Wrap(err)
return
}
err = db.AutoMigrate(r.Models()...)
if err != nil {
err = liberr.Wrap(err)
return
}
for _, rs := range oldCustomRuleSets {
target := model.Target{
Name: rs.Name,
Description: rs.Description,
RuleSetID: &rs.ID,
ImageID: rs.ImageID,
}
target.CreateUser = rs.CreateUser
type TargetLabel struct {
Name string `json:"name"`
Label string `json:"label"`
}
uniqueLabels := make(map[string]bool)
for _, rule := range rs.Rules {
ruleLabels := []string{}
_ = json.Unmarshal(rule.Labels, &ruleLabels)
for _, label := range ruleLabels {
uniqueLabels[label] = true
}
}
targetLabels := []TargetLabel{}
for k, _ := range uniqueLabels {
targetLabels = append(targetLabels, TargetLabel{Name: k, Label: k})
}
target.Labels, _ = json.Marshal(targetLabels)
result = db.Save(target)
if result.Error != nil {
err = liberr.Wrap(result.Error)
return
}
}
err = db.Migrator().DropConstraint(&model.RuleSet{}, "fk_RuleSet_Image")
if err != nil {
err = liberr.Wrap(err)
return
}
err = db.Migrator().DropColumn(&model.RuleSet{}, "ImageID")
if err != nil {
err = liberr.Wrap(err)
return
}
err = db.Migrator().DropColumn(&model.RuleSet{}, "Custom")
if err != nil {
err = liberr.Wrap(err)
return
}
return
}
func (r Migration) Models() []interface{} {
return model.All()
}