forked from facesea/banshee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
213 lines (206 loc) · 6.09 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
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
// Copyright 2015 Eleme Inc. All rights reserved.
// Tool to migrate admin rules, projects and users from bell.
//
// Requirements
//
// bell.js v2.0+ https://github.com/eleme/bell.js
// banshee v0.0.7+ https://github.com/eleme/banshee
//
// Command Line Usage
//
// ./migrate -from bell.db -to banshee.db -with-users
// mv banshee.db path/to/storage/admin
//
// Warning: Must backup your db files before the migration.
//
package main
import (
"flag"
"github.com/eleme/banshee/models"
"github.com/eleme/banshee/util/log"
"github.com/jinzhu/gorm"
"github.com/mattn/go-sqlite3"
)
var (
// Arguments
bellDBFileName = flag.String("from", "bell.db", "bell db file name")
bansheeDBFileName = flag.String("to", "banshee.db", "banshee db file name")
withUsers = flag.Bool("with-users", false, "if migrate users")
withProjs = flag.Bool("with-projs", false, "if migrate projects")
// DB Handles
bellDB *gorm.DB
bansheeDB *gorm.DB
)
// Init.
//
// 1. Parse arguments.
// 2. Init db handles.
// 3. Auto migrate schema.
//
func init() {
flag.Parse()
// Bell DB
if db, err := gorm.Open("sqlite3", *bellDBFileName); err != nil {
log.Fatal("%s: %v", *bellDBFileName, err)
} else {
bellDB = &db
bellDB.LogMode(false)
}
// Banshee DB
if db, err := gorm.Open("sqlite3", *bansheeDBFileName); err != nil {
log.Fatal("%s:%v", *bansheeDBFileName, err)
} else {
bansheeDB = &db
bansheeDB.LogMode(false)
}
if err := bansheeDB.AutoMigrate(&models.Project{}, &models.Rule{}, &models.User{}).Error; err != nil {
log.Fatal("failed to migrate schema for %s: %v", *bansheeDBFileName, err)
}
}
// Main
//
// 1. Migrate projects and all their rules.
// 2. Migrate users and their user-project relationships.
//
func main() {
if *withProjs {
migrateProjects()
}
if *withUsers {
migrateUsers()
}
}
// Migrate projects.
//
// 1. Fetch all projects from belldb.
// 2. Create the project into bansheedb.
// 3. Create the rules for each project.
//
func migrateProjects() {
var projs []Project
// Fetch all projects from belldb.
if err := bellDB.Find(&projs).Error; err != nil {
log.Fatal("fetch all projects from %s: %v", *bellDBFileName, err)
}
for _, proj := range projs {
// Create banshee project.
if err := models.ValidateProjectName(proj.Name); err != nil {
log.Warn("project %s: %v, skipping..", proj.Name, err)
continue
}
p := &models.Project{Name: proj.Name}
if err := bansheeDB.Create(p).Error; err != nil {
sqliteErr, ok := err.(sqlite3.Error)
if ok && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
log.Warn("project %s already in %s, skipping..", p.Name, *bansheeDBFileName)
} else {
log.Fatal("cannot create project %s: %v", p.Name, err)
}
}
// Fetch its rules from belldb.
var rules []Rule
if err := bellDB.Model(proj).Related(&rules).Error; err != nil {
log.Fatal("cannot fetch rules for %s: %v", p.Name, err)
}
for _, rule := range rules {
// Create banshee rule.
if err := models.ValidateRulePattern(rule.Pattern); err != nil {
log.Warn("rule %s: %v, belongs to %s, skippig..", rule.Pattern, err, proj.Name)
continue
}
r := &models.Rule{
Pattern: rule.Pattern,
ProjectID: p.ID,
TrendUp: rule.Up,
TrendDown: rule.Down,
// Important: max and min for bell is reversed with banshee's.
ThresholdMax: rule.Min,
ThresholdMin: rule.Max,
}
if err := bansheeDB.Create(r).Error; err != nil {
sqliteErr, ok := err.(sqlite3.Error)
if ok && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
log.Warn("rule %s already in %s, skipping..", r.Pattern, *bansheeDBFileName)
} else {
log.Fatal("cannot create rule %s: %v", r.Pattern, err)
}
}
}
}
}
// Migrate users.
//
// 1. Fetch all users from belldb.
// 2. Create the users into bansheedb.
// 3. Establish the relationships between project and user.
//
func migrateUsers() {
var users []Receiver
// Fetch all users from belldb.
if err := bellDB.Find(&users).Error; err != nil {
log.Fatal("fetch all users from %s: %v", *bellDBFileName, err)
}
for _, user := range users {
// Create banshee user.
err := models.ValidateUserName(user.Name)
if err == nil {
err = models.ValidateUserEmail(user.Email)
}
if err == nil {
err = models.ValidateUserPhone(user.Phone)
}
if err != nil {
log.Warn("user %s: %v, skipping..", user.Name, err)
}
u := &models.User{
Name: user.Name,
Email: user.Email,
Phone: user.Phone,
EnableEmail: user.EnableEmail,
EnablePhone: user.EnablePhone,
Universal: user.Universal,
}
if err := bansheeDB.Create(u).Error; err != nil {
sqliteErr, ok := err.(sqlite3.Error)
if ok && sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
log.Warn("user %s already in %s, skipping..", u.Name, *bansheeDBFileName)
} else {
log.Fatal("cannot create user %s: %v", u.Name, err)
}
}
// Establish relationship to project.
if user.Universal {
continue
}
// Get all relationships for this user.
var relations []ReceiverProject
if err := bellDB.Where("ReceiverId = ?", user.ID).Find(&relations).Error; err != nil {
log.Fatal("cannot fetch user-project relations for user %s: %v", user.Name, err)
}
for _, relation := range relations {
var proj Project
if err := bellDB.First(&proj, relation.ProjectID).Error; err != nil {
if err == gorm.RecordNotFound {
log.Warn("project %d not found for user %s, skipping..", relation.ProjectID, user.Name)
continue
}
log.Fatal("cannot get project %d for user %s", relation.ProjectID, user.Name)
}
p := &models.Project{}
if err := bansheeDB.Where("name = ?", proj.Name).First(p).Error; err != nil {
if err == gorm.RecordNotFound {
log.Warn("project %s not found in %s, skipping..", proj.Name, *bansheeDBFileName)
continue
}
log.Fatal("cannot get project %s in %s", proj.Name, *bansheeDBFileName)
}
if err := bansheeDB.Model(p).Association("Users").Append(u).Error; err != nil {
if err == gorm.RecordNotFound {
log.Warn("record not found: %v", err)
continue
}
log.Fatal("cannot append user %s to project %s:%v", u.Name, p.Name, err)
}
}
}
}