@@ -41,6 +41,41 @@ func createAssociationTable(ctx context.Context, db dal.RDB, conf *upgrader.Conf
41
41
return nil
42
42
}
43
43
44
+ func createInstanceAssociationIndex (ctx context.Context , db dal.RDB , conf * upgrader.Config ) error {
45
+
46
+ idxArr , err := db .Table (common .BKTableNameInstAsst ).Indexes (ctx )
47
+ if err != nil {
48
+ blog .Errorf ("get table %s index error. err:%s" , common .BKTableNameInstAsst , err .Error ())
49
+ return err
50
+ }
51
+
52
+ createIdxArr := []dal.Index {
53
+ dal.Index {Name : "idx_id" , Keys : map [string ]interface {}{"id" : - 1 }, Background : true , Unique : true },
54
+ dal.Index {Name : "idx_objID_asstObjID_asstID" , Keys : map [string ]interface {}{"bk_obj_id" : - 1 , "bk_asst_obj_id" : - 1 , "bk_asst_id" : - 1 }},
55
+ }
56
+ for _ , idx := range createIdxArr {
57
+ exist := false
58
+ for _ , existIdx := range idxArr {
59
+ if existIdx .Name == idx .Name {
60
+ exist = true
61
+ break
62
+ }
63
+ }
64
+ // index already exist, skip create
65
+ if exist {
66
+ continue
67
+ }
68
+ if err := db .Table (common .BKTableNameInstAsst ).CreateIndex (ctx , idx ); err != nil {
69
+ blog .ErrorJSON ("create index to cc_InstAsst error, err:%s, current index:%s, all create index:%s" , err .Error (), idx , createIdxArr )
70
+ return err
71
+ }
72
+
73
+ }
74
+
75
+ return nil
76
+
77
+ }
78
+
44
79
func addPresetAssociationType (ctx context.Context , db dal.RDB , conf * upgrader.Config ) error {
45
80
tablename := common .BKTableNameAsstDes
46
81
@@ -196,40 +231,53 @@ func reconcilAsstData(ctx context.Context, db dal.RDB, conf *upgrader.Config) er
196
231
return err
197
232
}
198
233
199
- // update ObjAsst
200
- instAssts := []metadata.InstAsst {}
201
-
202
234
instCond := condition .CreateCondition ()
203
235
instCond .Field ("bk_obj_id" ).Eq (asst .AsstObjID )
204
236
instCond .Field ("bk_asst_obj_id" ).Eq (asst .ObjectID )
205
237
instCond .Field (flag ).NotEq (true )
206
238
207
- if err = db .Table (common .BKTableNameInstAsst ).Find (instCond .ToMapStr ()).All (ctx , & instAssts ); err != nil {
208
- return err
209
- }
210
- for _ , instAsst := range instAssts {
211
- updateInst := mapstr .New ()
212
- updateInst .Set ("bk_obj_asst_id" , asst .AssociationName )
213
- updateInst .Set ("bk_asst_id" , asst .AsstKindID )
214
-
215
- // 交换 源<->目标
216
- updateInst .Set ("bk_obj_id" , instAsst .AsstObjectID )
217
- updateInst .Set ("bk_asst_obj_id" , instAsst .ObjectID )
218
- updateInst .Set ("bk_inst_id" , instAsst .AsstInstID )
219
- updateInst .Set ("bk_asst_inst_id" , instAsst .InstID )
220
-
221
- updateInst .Set (flag , true )
222
-
223
- updateInst .Set ("last_time" , time .Now ())
224
- if err = db .Table (common .BKTableNameInstAsst ).Update (ctx ,
225
- mapstr.MapStr {
226
- "id" : instAsst .ID ,
227
- }, updateInst ); err != nil {
239
+ pageSize := uint64 (2000 )
240
+ page := 0
241
+ for {
242
+ page += 1
243
+ // update ObjAsst
244
+ instAssts := []metadata.InstAsst {}
245
+ blog .InfoJSON ("find data from table:%s, page:%s, cond:%s" , common .BKTableNameInstAsst , page , instCond .ToMapStr ())
246
+ if err = db .Table (common .BKTableNameInstAsst ).Find (instCond .ToMapStr ()).Limit (pageSize ).All (ctx , & instAssts ); err != nil {
228
247
return err
229
248
}
249
+
250
+ blog .InfoJSON ("find data from table:%s, cond:%s, result count:%s" , common .BKTableNameInstAsst , instCond .ToMapStr (), len (instAssts ))
251
+ if len (instAssts ) == 0 {
252
+ break
253
+ }
254
+ for _ , instAsst := range instAssts {
255
+ updateInst := mapstr .New ()
256
+ updateInst .Set ("bk_obj_asst_id" , asst .AssociationName )
257
+ updateInst .Set ("bk_asst_id" , asst .AsstKindID )
258
+
259
+ // 交换 源<->目标
260
+ updateInst .Set ("bk_obj_id" , instAsst .AsstObjectID )
261
+ updateInst .Set ("bk_asst_obj_id" , instAsst .ObjectID )
262
+ updateInst .Set ("bk_inst_id" , instAsst .AsstInstID )
263
+ updateInst .Set ("bk_asst_inst_id" , instAsst .InstID )
264
+
265
+ updateInst .Set (flag , true )
266
+
267
+ updateInst .Set ("last_time" , time .Now ())
268
+ blog .InfoJSON ("update instasst, id:%s, updateInst:%s" , instAsst .ID , updateInst )
269
+ if err = db .Table (common .BKTableNameInstAsst ).Update (ctx ,
270
+ mapstr.MapStr {
271
+ "id" : instAsst .ID ,
272
+ }, updateInst ); err != nil {
273
+ return err
274
+ }
275
+ }
230
276
}
277
+
231
278
}
232
279
}
280
+ blog .InfoJSON ("start drop column cond:%s" , flag )
233
281
if err = db .Table (common .BKTableNameInstAsst ).DropColumn (ctx , flag ); err != nil {
234
282
return err
235
283
}
@@ -241,18 +289,21 @@ func reconcilAsstData(ctx context.Context, db dal.RDB, conf *upgrader.Config) er
241
289
cloudIDUpdateData := mapstr .New ()
242
290
cloudIDUpdateData .Set (common .BKPropertyTypeField , common .FieldTypeForeignKey )
243
291
cloudIDUpdateData .Set (common .BKOptionField , nil )
292
+ blog .InfoJSON ("update host cloud association cond:%s, data:%s" , cloudIDUpdateCond .ToMapStr (), cloudIDUpdateData )
244
293
err = db .Table (common .BKTableNameObjAttDes ).Update (ctx , cloudIDUpdateCond .ToMapStr (), cloudIDUpdateData )
245
294
if err != nil {
246
295
return err
247
296
}
248
297
deleteHostCloudAssociation := condition .CreateCondition ()
249
298
deleteHostCloudAssociation .Field ("bk_obj_id" ).Eq (common .BKInnerObjIDHost )
250
299
deleteHostCloudAssociation .Field ("bk_asst_obj_id" ).Eq (common .BKInnerObjIDPlat )
300
+ blog .InfoJSON ("delete host cloud association table:%s, cond:%s" , common .BKTableNameObjAsst , deleteHostCloudAssociation .ToMapStr ())
251
301
err = db .Table (common .BKTableNameObjAsst ).Delete (ctx , deleteHostCloudAssociation .ToMapStr ())
252
302
if err != nil {
253
303
return err
254
304
}
255
305
306
+ blog .InfoJSON ("delete host cloud association table:%s, cond:%s" , common .BKTableNameObjAttDes , propertyCond .ToMapStr ())
256
307
// drop outdate propertys
257
308
err = db .Table (common .BKTableNameObjAttDes ).Delete (ctx , propertyCond .ToMapStr ())
258
309
if err != nil {
@@ -262,13 +313,15 @@ func reconcilAsstData(ctx context.Context, db dal.RDB, conf *upgrader.Config) er
262
313
// drop outdate column
263
314
outdateColumns := []string {"bk_object_att_id" , "bk_asst_forward" , "bk_asst_name" }
264
315
for _ , column := range outdateColumns {
316
+ blog .InfoJSON ("delete field from table:%s, cond:%s" , common .BKTableNameObjAsst , column )
265
317
if err = db .Table (common .BKTableNameObjAsst ).DropColumn (ctx , column ); err != nil {
266
318
return err
267
319
}
268
320
}
269
321
270
322
delCond := condition .CreateCondition ()
271
323
delCond .Field (common .AssociationKindIDField ).Eq (nil )
324
+ blog .InfoJSON ("delete host cloud association table:%s, cond:%s" , common .BKTableNameObjAsst , delCond .ToMapStr ())
272
325
if err = db .Table (common .BKTableNameObjAsst ).Delete (ctx , delCond .ToMapStr ()); err != nil {
273
326
return err
274
327
}
0 commit comments