MCPcopy Index your code
hub / github.com/53AI/53AIHub / addUUIDToLibrary

Function addUUIDToLibrary

api/main.go:310–349  ·  view source on GitHub ↗

addUUIDToLibrary 为Library表添加UUID字段并填充已有数据

(db *gorm.DB)

Source from the content-addressed store, hash-verified

308
309// addUUIDToLibrary 为Library表添加UUID字段并填充已有数据
310func addUUIDToLibrary(db *gorm.DB) error {
311 if db == nil {
312 return fmt.Errorf("database connection is nil")
313 }
314
315 migrator := db.Migrator()
316 if migrator == nil {
317 return fmt.Errorf("database migrator is nil")
318 }
319
320 if !migrator.HasTable(&model.Library{}) {
321 return nil
322 }
323
324 if err := db.AutoMigrate(&model.Library{}); err != nil {
325 return fmt.Errorf("auto migrate library table: %w", err)
326 }
327
328 var count int64
329 db.Model(&model.Library{}).Where("uuid IS NULL OR uuid = ''").Count(&count)
330 if count > 0 {
331 db.Model(&model.Library{}).Where("uuid = ''").Update("uuid", gorm.Expr("NULL"))
332
333 var libraries []model.Library
334 result := db.Model(&model.Library{}).Where("uuid IS NULL").Find(&libraries)
335 if result.Error != nil {
336 return result.Error
337 }
338
339 for _, library := range libraries {
340 uuidValue := uuid.New().String()
341 result := db.Model(&model.Library{}).Where("id = ?", library.ID).Update("uuid", uuidValue)
342 if result.Error != nil {
343 return result.Error
344 }
345 }
346 }
347
348 return nil
349}

Callers 1

mainFunction · 0.85

Calls 4

ErrorfMethod · 0.80
UpdateMethod · 0.65
StringMethod · 0.45
NewMethod · 0.45

Tested by

no test coverage detected