From 40c751cde29db4fa7373a230ca4cd6f0e0c1249c Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:38:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91=20(#2314)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/database.go | 8 +++---- backend/app/repo/databse_mysql.go | 10 -------- backend/app/service/app_utils.go | 11 +++------ backend/app/service/backup_app.go | 2 +- backend/app/service/database_mysql.go | 24 ++++++++++--------- cmd/server/docs/docs.go | 14 +---------- cmd/server/docs/swagger.json | 14 +---------- cmd/server/docs/swagger.yaml | 9 ------- frontend/src/api/interface/database.ts | 2 -- .../src/views/database/mysql/delete/index.vue | 6 ----- 10 files changed, 22 insertions(+), 78 deletions(-) diff --git a/backend/app/dto/database.go b/backend/app/dto/database.go index a3e3f8fb6..f21edafd0 100644 --- a/backend/app/dto/database.go +++ b/backend/app/dto/database.go @@ -50,11 +50,9 @@ type MysqlDBDeleteCheck struct { } type MysqlDBDelete struct { - ID uint `json:"id" validate:"required"` - Type string `json:"type" validate:"required,oneof=mysql mariadb"` - DatabaseID uint `json:"databaseID" validate:"required"` - ForceDelete bool `json:"forceDelete"` - DeleteBackup bool `json:"deleteBackup"` + ID uint `json:"id" validate:"required"` + ForceDelete bool `json:"forceDelete"` + DeleteBackup bool `json:"deleteBackup"` } type MysqlStatus struct { diff --git a/backend/app/repo/databse_mysql.go b/backend/app/repo/databse_mysql.go index a8179a8f8..6e8dbc2d0 100644 --- a/backend/app/repo/databse_mysql.go +++ b/backend/app/repo/databse_mysql.go @@ -16,7 +16,6 @@ type IMysqlRepo interface { Get(opts ...DBOption) (model.DatabaseMysql, error) WithByDatabase(databaseID uint) DBOption WithByFrom(from string) DBOption - WithDatabaseIDIn(databaseIDs []uint) DBOption List(opts ...DBOption) ([]model.DatabaseMysql, error) Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error) Create(ctx context.Context, mysql *model.DatabaseMysql) error @@ -122,12 +121,3 @@ func (u *MysqlRepo) WithByFrom(from string) DBOption { return g } } - -func (u *MysqlRepo) WithDatabaseIDIn(databaseIDs []uint) DBOption { - return func(g *gorm.DB) *gorm.DB { - if len(databaseIDs) != 0 { - return g.Where("database_id in (?)", databaseIDs) - } - return g - } -} diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index c6f982f66..c0373d6f7 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -258,12 +258,7 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b if err := deleteLink(ctx, &install, deleteDB, forceDelete, deleteBackup); err != nil && !forceDelete { return err } - var databaseIDs []uint if DatabaseKeys[install.App.Key] > 0 { - databases, _ := databaseRepo.GetList(databaseRepo.WithAppInstallID(install.ID)) - for _, database := range databases { - databaseIDs = append(databaseIDs, database.ID) - } _ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID)) } @@ -272,8 +267,9 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b _ = websiteRepo.DeleteAll(ctx) _ = websiteDomainRepo.DeleteAll(ctx) case constant.AppMysql, constant.AppMariaDB: - if len(databaseIDs) > 0 { - _ = mysqlRepo.Delete(ctx, mysqlRepo.WithDatabaseIDIn(databaseIDs)) + database, _ := databaseRepo.Get(databaseRepo.WithAppInstallID(install.ID)) + if database.ID > 0 { + _ = mysqlRepo.Delete(ctx, mysqlRepo.WithByDatabase(database.ID)) } } @@ -311,7 +307,6 @@ func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, f ID: mysqlDatabase.ID, ForceDelete: forceDelete, DeleteBackup: deleteBackup, - Type: re.Key, }); err != nil && !forceDelete { return err } diff --git a/backend/app/service/backup_app.go b/backend/app/service/backup_app.go index ea4315dea..81618e6ba 100644 --- a/backend/app/service/backup_app.go +++ b/backend/app/service/backup_app.go @@ -255,7 +255,7 @@ func handleAppRecover(install *model.AppInstall, recoverFile string, isRollback func reCreateDB(db model.DatabaseMysql, app model.AppInstall, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) { mysqlService := NewIMysqlService() ctx := context.Background() - _ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: db.ID, DatabaseID: db.DatabaseID, Type: app.App.Key, DeleteBackup: true, ForceDelete: true}) + _ = mysqlService.Delete(ctx, dto.MysqlDBDelete{ID: db.ID, DeleteBackup: true, ForceDelete: true}) envMap := make(map[string]interface{}) if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil { diff --git a/backend/app/service/database_mysql.go b/backend/app/service/database_mysql.go index 68f9b2a30..40bcc5940 100644 --- a/backend/app/service/database_mysql.go +++ b/backend/app/service/database_mysql.go @@ -224,7 +224,11 @@ func (u *MysqlService) Delete(ctx context.Context, req dto.MysqlDBDelete) error if err != nil && !req.ForceDelete { return err } - cli, version, database, err := LoadMysqlClientByFrom(req.DatabaseID) + database, err := databaseRepo.Get(commonRepo.WithByID(db.DatabaseID)) + if err != nil && !req.ForceDelete { + return err + } + cli, version, _, err := LoadMysqlClientByFrom(db.DatabaseID) if err != nil { return err } @@ -239,26 +243,24 @@ func (u *MysqlService) Delete(ctx context.Context, req dto.MysqlDBDelete) error return err } - uploadDir := path.Join(global.CONF.System.BaseDir, fmt.Sprintf("1panel/uploads/database/%s/%s/%s", req.Type, database, db.Name)) - if _, err := os.Stat(uploadDir); err == nil { - _ = os.RemoveAll(uploadDir) + uploadDir2 := path.Join(global.CONF.System.BaseDir, fmt.Sprintf("1panel/uploads/database/%s/%s-%s/%s", database.Type, database.From, database.Name, db.Name)) + if _, err := os.Stat(uploadDir2); err == nil { + _ = os.RemoveAll(uploadDir2) } if req.DeleteBackup { localDir, err := loadLocalDir() if err != nil && !req.ForceDelete { return err } - backupDir := path.Join(localDir, fmt.Sprintf("database/%s/%s-%s/%s", req.Type, db.From, database, db.Name)) + backupDir := path.Join(localDir, fmt.Sprintf("database/%s/%s-%s/%s", database.Type, db.From, database.Name, db.Name)) if _, err := os.Stat(backupDir); err == nil { _ = os.RemoveAll(backupDir) } - backupDir2 := path.Join(localDir, fmt.Sprintf("database/%s/%s/%s", req.Type, database, db.Name)) - if _, err := os.Stat(backupDir2); err == nil { - _ = os.RemoveAll(backupDir2) - } - global.LOG.Infof("delete database %s-%s backups successful", database, db.Name) + global.LOG.Infof("delete database %s-%s backups successful", database.Name, db.Name) } - _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(req.Type), commonRepo.WithByName(database), backupRepo.WithByDetailName(db.Name)) + _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType(database.Type), + commonRepo.WithByName(fmt.Sprintf("%v", database.ID)), + backupRepo.WithByDetailName(db.Name)) _ = mysqlRepo.Delete(ctx, commonRepo.WithByID(db.ID)) return nil diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 7f2381179..c5ff0f006 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -13605,14 +13605,9 @@ const docTemplate = `{ "dto.MysqlDBDelete": { "type": "object", "required": [ - "databaseID", - "id", - "type" + "id" ], "properties": { - "databaseID": { - "type": "integer" - }, "deleteBackup": { "type": "boolean" }, @@ -13621,13 +13616,6 @@ const docTemplate = `{ }, "id": { "type": "integer" - }, - "type": { - "type": "string", - "enum": [ - "mysql", - "mariadb" - ] } } }, diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 1d5499b65..192bd2a52 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -13598,14 +13598,9 @@ "dto.MysqlDBDelete": { "type": "object", "required": [ - "databaseID", - "id", - "type" + "id" ], "properties": { - "databaseID": { - "type": "integer" - }, "deleteBackup": { "type": "boolean" }, @@ -13614,13 +13609,6 @@ }, "id": { "type": "integer" - }, - "type": { - "type": "string", - "enum": [ - "mysql", - "mariadb" - ] } } }, diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 93fc9ec1e..74180bfe1 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -1285,23 +1285,14 @@ definitions: type: object dto.MysqlDBDelete: properties: - databaseID: - type: integer deleteBackup: type: boolean forceDelete: type: boolean id: type: integer - type: - enum: - - mysql - - mariadb - type: string required: - - databaseID - id - - type type: object dto.MysqlDBDeleteCheck: properties: diff --git a/frontend/src/api/interface/database.ts b/frontend/src/api/interface/database.ts index b3655834c..6a1117aa6 100644 --- a/frontend/src/api/interface/database.ts +++ b/frontend/src/api/interface/database.ts @@ -47,8 +47,6 @@ export namespace Database { } export interface MysqlDBDelete { id: number; - type: string; - databaseID: number; forceDelete: boolean; deleteBackup: boolean; } diff --git a/frontend/src/views/database/mysql/delete/index.vue b/frontend/src/views/database/mysql/delete/index.vue index eb3b7ddf7..aba38730a 100644 --- a/frontend/src/views/database/mysql/delete/index.vue +++ b/frontend/src/views/database/mysql/delete/index.vue @@ -48,8 +48,6 @@ import { MsgSuccess } from '@/utils/message'; let deleteReq = ref({ id: 0, - type: '', - databaseID: 0, deleteBackup: false, forceDelete: false, }); @@ -62,17 +60,13 @@ const deleteForm = ref(); interface DialogProps { id: number; - type: string; name: string; - databaseID: number; } const emit = defineEmits<{ (e: 'search'): void }>(); const acceptParams = async (prop: DialogProps) => { deleteReq.value = { id: prop.id, - type: prop.type, - databaseID: prop.databaseID, deleteBackup: false, forceDelete: false, };