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 { type MysqlDBDelete struct {
ID uint `json:"id" validate:"required"` ID uint `json:"id" validate:"required"`
Type string `json:"type" validate:"required,oneof=mysql mariadb"` ForceDelete bool `json:"forceDelete"`
DatabaseID uint `json:"databaseID" validate:"required"` DeleteBackup bool `json:"deleteBackup"`
ForceDelete bool `json:"forceDelete"`
DeleteBackup bool `json:"deleteBackup"`
} }
type MysqlStatus struct { type MysqlStatus struct {

View File

@ -16,7 +16,6 @@ type IMysqlRepo interface {
Get(opts ...DBOption) (model.DatabaseMysql, error) Get(opts ...DBOption) (model.DatabaseMysql, error)
WithByDatabase(databaseID uint) DBOption WithByDatabase(databaseID uint) DBOption
WithByFrom(from string) DBOption WithByFrom(from string) DBOption
WithDatabaseIDIn(databaseIDs []uint) DBOption
List(opts ...DBOption) ([]model.DatabaseMysql, error) List(opts ...DBOption) ([]model.DatabaseMysql, error)
Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error) Page(limit, offset int, opts ...DBOption) (int64, []model.DatabaseMysql, error)
Create(ctx context.Context, mysql *model.DatabaseMysql) error Create(ctx context.Context, mysql *model.DatabaseMysql) error
@ -122,12 +121,3 @@ func (u *MysqlRepo) WithByFrom(from string) DBOption {
return g 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 { if err := deleteLink(ctx, &install, deleteDB, forceDelete, deleteBackup); err != nil && !forceDelete {
return err return err
} }
var databaseIDs []uint
if DatabaseKeys[install.App.Key] > 0 { 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)) _ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID))
} }
@ -272,8 +267,9 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b
_ = websiteRepo.DeleteAll(ctx) _ = websiteRepo.DeleteAll(ctx)
_ = websiteDomainRepo.DeleteAll(ctx) _ = websiteDomainRepo.DeleteAll(ctx)
case constant.AppMysql, constant.AppMariaDB: case constant.AppMysql, constant.AppMariaDB:
if len(databaseIDs) > 0 { database, _ := databaseRepo.Get(databaseRepo.WithAppInstallID(install.ID))
_ = mysqlRepo.Delete(ctx, mysqlRepo.WithDatabaseIDIn(databaseIDs)) 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, ID: mysqlDatabase.ID,
ForceDelete: forceDelete, ForceDelete: forceDelete,
DeleteBackup: deleteBackup, DeleteBackup: deleteBackup,
Type: re.Key,
}); err != nil && !forceDelete { }); err != nil && !forceDelete {
return err 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) { func reCreateDB(db model.DatabaseMysql, app model.AppInstall, oldEnv string) (*model.DatabaseMysql, map[string]interface{}, error) {
mysqlService := NewIMysqlService() mysqlService := NewIMysqlService()
ctx := context.Background() 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{}) envMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldEnv), &envMap); err != nil { 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 { if err != nil && !req.ForceDelete {
return err 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 { if err != nil {
return err return err
} }
@ -239,26 +243,24 @@ func (u *MysqlService) Delete(ctx context.Context, req dto.MysqlDBDelete) error
return err return err
} }
uploadDir := path.Join(global.CONF.System.BaseDir, fmt.Sprintf("1panel/uploads/database/%s/%s/%s", req.Type, database, db.Name)) 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(uploadDir); err == nil { if _, err := os.Stat(uploadDir2); err == nil {
_ = os.RemoveAll(uploadDir) _ = os.RemoveAll(uploadDir2)
} }
if req.DeleteBackup { if req.DeleteBackup {
localDir, err := loadLocalDir() localDir, err := loadLocalDir()
if err != nil && !req.ForceDelete { if err != nil && !req.ForceDelete {
return err 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 { if _, err := os.Stat(backupDir); err == nil {
_ = os.RemoveAll(backupDir) _ = os.RemoveAll(backupDir)
} }
backupDir2 := path.Join(localDir, fmt.Sprintf("database/%s/%s/%s", req.Type, database, db.Name)) global.LOG.Infof("delete database %s-%s backups successful", database.Name, db.Name)
if _, err := os.Stat(backupDir2); err == nil {
_ = os.RemoveAll(backupDir2)
}
global.LOG.Infof("delete database %s-%s backups successful", database, 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)) _ = mysqlRepo.Delete(ctx, commonRepo.WithByID(db.ID))
return nil return nil

View File

@ -13605,14 +13605,9 @@ const docTemplate = `{
"dto.MysqlDBDelete": { "dto.MysqlDBDelete": {
"type": "object", "type": "object",
"required": [ "required": [
"databaseID", "id"
"id",
"type"
], ],
"properties": { "properties": {
"databaseID": {
"type": "integer"
},
"deleteBackup": { "deleteBackup": {
"type": "boolean" "type": "boolean"
}, },
@ -13621,13 +13616,6 @@ const docTemplate = `{
}, },
"id": { "id": {
"type": "integer" "type": "integer"
},
"type": {
"type": "string",
"enum": [
"mysql",
"mariadb"
]
} }
} }
}, },

View File

@ -13598,14 +13598,9 @@
"dto.MysqlDBDelete": { "dto.MysqlDBDelete": {
"type": "object", "type": "object",
"required": [ "required": [
"databaseID", "id"
"id",
"type"
], ],
"properties": { "properties": {
"databaseID": {
"type": "integer"
},
"deleteBackup": { "deleteBackup": {
"type": "boolean" "type": "boolean"
}, },
@ -13614,13 +13609,6 @@
}, },
"id": { "id": {
"type": "integer" "type": "integer"
},
"type": {
"type": "string",
"enum": [
"mysql",
"mariadb"
]
} }
} }
}, },

View File

@ -1285,23 +1285,14 @@ definitions:
type: object type: object
dto.MysqlDBDelete: dto.MysqlDBDelete:
properties: properties:
databaseID:
type: integer
deleteBackup: deleteBackup:
type: boolean type: boolean
forceDelete: forceDelete:
type: boolean type: boolean
id: id:
type: integer type: integer
type:
enum:
- mysql
- mariadb
type: string
required: required:
- databaseID
- id - id
- type
type: object type: object
dto.MysqlDBDeleteCheck: dto.MysqlDBDeleteCheck:
properties: properties:

View File

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

View File

@ -48,8 +48,6 @@ import { MsgSuccess } from '@/utils/message';
let deleteReq = ref({ let deleteReq = ref({
id: 0, id: 0,
type: '',
databaseID: 0,
deleteBackup: false, deleteBackup: false,
forceDelete: false, forceDelete: false,
}); });
@ -62,17 +60,13 @@ const deleteForm = ref<FormInstance>();
interface DialogProps { interface DialogProps {
id: number; id: number;
type: string;
name: string; name: string;
databaseID: number;
} }
const emit = defineEmits<{ (e: 'search'): void }>(); const emit = defineEmits<{ (e: 'search'): void }>();
const acceptParams = async (prop: DialogProps) => { const acceptParams = async (prop: DialogProps) => {
deleteReq.value = { deleteReq.value = {
id: prop.id, id: prop.id,
type: prop.type,
databaseID: prop.databaseID,
deleteBackup: false, deleteBackup: false,
forceDelete: false, forceDelete: false,
}; };