fix: 修改数据库删除逻辑 (#2314)

pull/2315/head
ssongliu 2023-09-15 18:38:15 +08:00 committed by GitHub
parent a3bf4513e1
commit 40c751cde2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 78 deletions

View File

@ -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 {

View File

@ -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
}
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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

View File

@ -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"
]
}
}
},

View File

@ -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"
]
}
}
},

View File

@ -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:

View File

@ -47,8 +47,6 @@ export namespace Database {
}
export interface MysqlDBDelete {
id: number;
type: string;
databaseID: number;
forceDelete: boolean;
deleteBackup: boolean;
}

View File

@ -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<FormInstance>();
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,
};