diff --git a/backend/app/service/cornjob.go b/backend/app/service/cornjob.go index 8e6eb89de..688c27b3a 100644 --- a/backend/app/service/cornjob.go +++ b/backend/app/service/cornjob.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "strconv" "time" "github.com/1Panel-dev/1Panel/backend/app/dto" @@ -54,6 +55,14 @@ func (u *CronjobService) SearchWithPage(search dto.SearchWithPage) (int64, inter } else { item.TargetDir = "-" } + if item.Type == "database" && item.DBName != "all" { + itemID, _ := strconv.Atoi(item.DBName) + dbItem, err := mysqlRepo.Get(commonRepo.WithByID(uint(itemID))) + if err != nil { + return 0, nil, err + } + item.DBName = dbItem.Name + } record, _ := cronjobRepo.RecordFirst(cronjob.ID) if record.ID != 0 { item.LastRecordTime = record.StartTime.Format("2006-01-02 15:04:05") diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index be5636ef7..deb04f23f 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -37,6 +37,7 @@ func Init() { migrations.EncryptHostPassword, migrations.AddRemoteDB, migrations.UpdateRedisParam, + migrations.UpdateCronjobWithDb, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 34d8b4f54..59ac9e41e 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -547,3 +547,26 @@ var UpdateRedisParam = &gormigrate.Migration{ }, } + +var UpdateCronjobWithDb = &gormigrate.Migration{ + ID: "20230809-update-cronjob-with-db", + Migrate: func(tx *gorm.DB) error { + var cronjobs []model.Cronjob + if err := global.DB.Where("type = ? AND db_name != ?", "database", "all").Find(&cronjobs).Error; err != nil { + return nil + } + + for _, job := range cronjobs { + var db model.DatabaseMysql + if err := global.DB.Where("name = ?", job.DBName).First(&db).Error; err != nil { + continue + } + if err := tx.Model(&model.Cronjob{}). + Where("id = ?", job.ID). + Updates(map[string]interface{}{"db_name": db.ID}).Error; err != nil { + continue + } + } + return nil + }, +}