From 25ccadea9e59a8c78785119d26f8de5273ec6539 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 9 Aug 2023 14:14:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E8=AE=A1=E5=88=92?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=A4=87=E4=BB=BD=E8=BF=9C=E7=A8=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=AF=A6=E6=83=85=E6=98=BE=E7=A4=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=20(#1886)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/cornjob.go | 9 +++++++++ backend/init/migration/migrate.go | 1 + backend/init/migration/migrations/init.go | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+) 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 + }, +}