forked from cookieY/Yearning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.go
211 lines (200 loc) · 7.06 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
// Copyright 2019 HenryYee.
//
// Licensed under the AGPL, Version 3.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.gnu.org/licenses/agpl-3.0.en.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package service
import (
"Yearning-go/src/engine"
"Yearning-go/src/lib"
"Yearning-go/src/model"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gookit/gcli/v2/interact"
"os"
"time"
)
func DataInit(o *engine.AuditRole, other *model.Other, ldap *model.Ldap, message *model.Message, a *model.PermissionList) {
c, _ := json.Marshal(o)
oh, _ := json.Marshal(other)
l, _ := json.Marshal(ldap)
m, _ := json.Marshal(message)
ak, _ := json.Marshal(a)
sId := uuid.New().String()
group, _ := json.Marshal([]string{sId})
model.DB().Debug().Create(&model.CoreAccount{
Username: "admin",
RealName: "超级管理员",
Password: lib.DjangoEncrypt("Yearning_admin", string(lib.GetRandom())),
Department: "DBA",
Email: "",
})
model.DB().Debug().Create(&model.CoreGlobalConfiguration{
Authorization: "global",
Other: oh,
AuditRole: c,
Message: m,
Ldap: l,
})
model.DB().Debug().Create(&model.CoreGrained{
Username: "admin",
Group: group,
})
model.DB().Debug().Create(&model.CoreRoleGroup{
Name: "admin",
Permissions: ak,
GroupId: sId,
})
}
func Migrate() {
if !model.DB().Migrator().HasTable("core_accounts") {
if os.Getenv("IS_DOCKER") == "" {
if !interact.Confirm("是否已将数据库字符集设置为UTF8/UTF8MB4?") {
return
}
}
_ = model.DB().AutoMigrate(&model.CoreAccount{})
_ = model.DB().AutoMigrate(&model.CoreDataSource{})
_ = model.DB().AutoMigrate(&model.CoreGlobalConfiguration{})
_ = model.DB().AutoMigrate(&model.CoreGrained{})
_ = model.DB().AutoMigrate(&model.CoreOrderComment{})
_ = model.DB().AutoMigrate(&model.CoreSqlOrder{})
_ = model.DB().AutoMigrate(&model.CoreSqlRecord{})
_ = model.DB().AutoMigrate(&model.CoreRollback{})
_ = model.DB().AutoMigrate(&model.CoreQueryRecord{})
_ = model.DB().AutoMigrate(&model.CoreQueryOrder{})
_ = model.DB().AutoMigrate(&model.CoreAutoTask{})
_ = model.DB().AutoMigrate(&model.CoreRoleGroup{})
_ = model.DB().AutoMigrate(&model.CoreWorkflowTpl{})
_ = model.DB().AutoMigrate(&model.CoreWorkflowDetail{})
o := engine.AuditRole{
DMLInsertColumns: false,
DMLMaxInsertRows: 10,
DMLWhere: false,
DMLOrder: false,
DMLSelect: false,
DDLCheckTableComment: false,
DDLCheckColumnNullable: false,
DDLCheckColumnDefault: false,
DDLEnableAcrossDBRename: false,
DDLEnableAutoincrementInit: false,
DDLEnableAutoIncrement: false,
DDLEnableAutoincrementUnsigned: false,
DDLEnableDropTable: false,
DDLEnableDropDatabase: false,
DDLEnableNullIndexName: false,
DDLIndexNameSpec: false,
DDLMaxKeyParts: 5,
DDLMaxKey: 5,
DDLMaxCharLength: 10,
DDLAllowColumnType: false,
DDLPrimaryKeyMust: false,
MaxTableNameLen: 10,
MaxAffectRows: 1000,
SupportCharset: "",
SupportCollation: "",
CheckIdentifier: false,
MustHaveColumns: "",
DDLMultiToCommit: false,
AllowCreatePartition: false,
AllowCreateView: false,
AllowSpecialType: false,
DDLEnablePrimaryKey: false,
}
other := model.Other{
Limit: 1000,
IDC: []string{"Aliyun", "AWS"},
Query: false,
Register: false,
Export: false,
ExQueryTime: 60,
}
ldap := model.Ldap{
Url: "",
User: "",
Password: "",
Type: "(&(objectClass=organizationalPerson)(sAMAccountName=%s))",
Sc: "",
}
message := model.Message{
WebHook: "",
Host: "",
Port: 25,
User: "",
Password: "",
ToUser: "",
Mail: false,
Ding: false,
Ssl: false,
}
a := model.PermissionList{
DDLSource: []string{},
DMLSource: []string{},
QuerySource: []string{},
}
time.Sleep(2)
DataInit(&o, &other, &ldap, &message, &a)
fmt.Println("初始化成功!\n用户名: admin\n密码:Yearning_admin\n请通过./Yearning run 运行,默认地址:http://<host>:8000")
} else {
fmt.Println("已初始化过,请不要再次执行")
}
}
func UpdateData() {
fmt.Println("检查更新.......")
_ = model.DB().AutoMigrate(&model.CoreAccount{})
_ = model.DB().AutoMigrate(&model.CoreDataSource{})
_ = model.DB().AutoMigrate(&model.CoreGlobalConfiguration{})
_ = model.DB().AutoMigrate(&model.CoreGrained{})
_ = model.DB().AutoMigrate(&model.CoreSqlOrder{})
_ = model.DB().AutoMigrate(&model.CoreSqlRecord{})
_ = model.DB().AutoMigrate(&model.CoreRollback{})
_ = model.DB().AutoMigrate(&model.CoreQueryRecord{})
_ = model.DB().AutoMigrate(&model.CoreQueryOrder{})
_ = model.DB().AutoMigrate(&model.CoreAutoTask{})
_ = model.DB().AutoMigrate(&model.CoreRoleGroup{})
_ = model.DB().AutoMigrate(&model.CoreWorkflowTpl{})
_ = model.DB().AutoMigrate(&model.CoreWorkflowDetail{})
_ = model.DB().AutoMigrate(&model.CoreOrderComment{})
if model.DB().Migrator().HasColumn(&model.CoreAutoTask{}, "base") {
_ = model.DB().Migrator().RenameColumn(&model.CoreAutoTask{}, "base", "data_base")
}
if model.DB().Migrator().HasColumn(&model.CoreSqlOrder{}, "uuid") {
_ = model.DB().Migrator().DropColumn(&model.CoreSqlOrder{}, "uuid")
}
if model.DB().Migrator().HasColumn(&model.CoreWorkflowDetail{}, "rejected") {
_ = model.DB().Migrator().DropColumn(&model.CoreWorkflowDetail{}, "rejected")
}
if model.DB().Migrator().HasColumn(&model.CoreAutoTask{}, "base") {
_ = model.DB().Migrator().DropColumn(&model.CoreAutoTask{}, "base")
}
fmt.Println("数据已更新!")
}
func DelCol() {
_ = model.DB().Migrator().DropColumn(&model.CoreQueryOrder{}, "source")
}
func MargeRuleGroup() {
fmt.Println("破坏性变更修复…………")
_ = model.DB().Migrator().DropColumn(&model.CoreSqlOrder{}, "rejected")
_ = model.DB().Migrator().DropColumn(&model.CoreGrained{}, "permissions")
_ = model.DB().Migrator().DropColumn(&model.CoreGrained{}, "rule")
ldap := model.Ldap{
Url: "",
User: "",
Password: "",
Type: "(&(objectClass=organizationalPerson)(sAMAccountName=%s))",
Sc: "",
}
b, _ := json.Marshal(ldap)
model.DB().Model(model.CoreGlobalConfiguration{}).Where("1=1").Updates(&model.CoreGlobalConfiguration{Ldap: b})
_ = model.DB().Exec("alter table core_sql_orders modify assigned varchar(550) not null")
_ = model.DB().Exec("alter table core_workflow_details modify action varchar(550) not null")
fmt.Println("修复成功!")
}