diff --git a/backend/init/migration/migrations/v_1_10.go b/backend/init/migration/migrations/v_1_10.go index 62546c0ba..4ce0ca200 100644 --- a/backend/init/migration/migrations/v_1_10.go +++ b/backend/init/migration/migrations/v_1_10.go @@ -55,54 +55,54 @@ var NewMonitorDB = &gormigrate.Migration{ ios []model.MonitorIO networks []model.MonitorNetwork ) - if err := tx.Find(&bases).Error; err != nil { - return err - } - if err := tx.Find(&ios).Error; err != nil { - return err - } - if err := tx.Find(&networks).Error; err != nil { - return err - } + _ = tx.Find(&bases).Error + _ = tx.Find(&ios).Error + _ = tx.Find(&networks).Error + if err := global.MonitorDB.AutoMigrate(&model.MonitorBase{}, &model.MonitorIO{}, &model.MonitorNetwork{}); err != nil { return err } - _ = global.MonitorDB.Exec("DELETE FROM monitor_bases").Error _ = global.MonitorDB.Exec("DELETE FROM monitor_ios").Error _ = global.MonitorDB.Exec("DELETE FROM monitor_networks").Error - for i := 0; i <= len(bases)/200; i++ { - var itemData []model.MonitorBase - if 200*(i+1) <= len(bases) { - itemData = bases[200*i : 200*(i+1)] - } else { - itemData = bases[200*i:] - } - if err := global.MonitorDB.Create(&itemData).Error; err != nil { - return err + if len(bases) != 0 { + for i := 0; i <= len(bases)/200; i++ { + var itemData []model.MonitorBase + if 200*(i+1) <= len(bases) { + itemData = bases[200*i : 200*(i+1)] + } else { + itemData = bases[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } } } - for i := 0; i <= len(ios)/200; i++ { - var itemData []model.MonitorIO - if 200*(i+1) <= len(ios) { - itemData = ios[200*i : 200*(i+1)] - } else { - itemData = ios[200*i:] - } - if err := global.MonitorDB.Create(&itemData).Error; err != nil { - return err + if len(ios) != 0 { + for i := 0; i <= len(ios)/200; i++ { + var itemData []model.MonitorIO + if 200*(i+1) <= len(ios) { + itemData = ios[200*i : 200*(i+1)] + } else { + itemData = ios[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } } } - for i := 0; i <= len(networks)/200; i++ { - var itemData []model.MonitorNetwork - if 200*(i+1) <= len(networks) { - itemData = networks[200*i : 200*(i+1)] - } else { - itemData = networks[200*i:] - } - if err := global.MonitorDB.Create(&itemData).Error; err != nil { - return err + if len(networks) != 0 { + for i := 0; i <= len(networks)/200; i++ { + var itemData []model.MonitorNetwork + if 200*(i+1) <= len(networks) { + itemData = networks[200*i : 200*(i+1)] + } else { + itemData = networks[200*i:] + } + if err := global.MonitorDB.Create(&itemData).Error; err != nil { + return err + } } } return nil