From 4e7350aaaee8ab6f5d8e0a81b7cbe8f145648bc8 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:46:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=AE=89=E8=A3=85=E5=88=A0=E9=99=A4=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20(#2311)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/repo/databse_mysql.go | 10 +++++ backend/app/service/app.go | 28 ++++++------ backend/app/service/app_install.go | 2 +- backend/app/service/app_utils.go | 54 ++++++++++++++--------- backend/constant/errs.go | 2 - backend/i18n/lang/en.yaml | 2 - backend/i18n/lang/zh-Hant.yaml | 2 - backend/i18n/lang/zh.yaml | 2 - backend/init/migration/migrate.go | 1 + backend/init/migration/migrations/init.go | 10 +++++ 10 files changed, 69 insertions(+), 44 deletions(-) diff --git a/backend/app/repo/databse_mysql.go b/backend/app/repo/databse_mysql.go index 6e8dbc2d0..a8179a8f8 100644 --- a/backend/app/repo/databse_mysql.go +++ b/backend/app/repo/databse_mysql.go @@ -16,6 +16,7 @@ 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 @@ -121,3 +122,12 @@ 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.go b/backend/app/service/app.go index 1d484411c..f26c22f2f 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -23,7 +23,6 @@ import ( "net/http" "os" "path" - "reflect" "strconv" "strings" ) @@ -283,12 +282,6 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( if err != nil { return } - if DatabaseKeys[app.Key] > 0 { - if existDatabases, _ := databaseRepo.GetList(commonRepo.WithByName(req.Name)); len(existDatabases) > 0 { - err = buserr.New(constant.ErrRemoteExist) - return - } - } for key := range req.Params { if !strings.Contains(key, "PANEL_APP_PORT") { continue @@ -396,13 +389,22 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( } } }() - if hostName, ok := req.Params["PANEL_DB_HOST"]; ok { - database, _ := databaseRepo.Get(commonRepo.WithByName(hostName.(string))) - if !reflect.DeepEqual(database, model.Database{}) { - req.Params["PANEL_DB_HOST"] = database.Address - req.Params["PANEL_DB_PORT"] = database.Port - req.Params["PANEL_DB_HOST_NAME"] = hostName + if dbHost, ok := req.Params["PANEL_DB_HOST"]; ok { + var ( + databaseID int + database model.Database + ) + databaseID, err = strconv.Atoi(dbHost.(string)) + if err != nil { + return } + database, err = databaseRepo.Get(commonRepo.WithByID(uint(databaseID))) + if err != nil { + return + } + req.Params["PANEL_DB_HOST"] = database.Address + req.Params["PANEL_DB_PORT"] = database.Port + req.Params["PANEL_DB_HOST_ID"] = uint(databaseID) } paramByte, err = json.Marshal(req.Params) if err != nil { diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index fe956a970..3eddc97c0 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -435,7 +435,7 @@ func (a *AppInstallService) GetServices(key string) ([]response.AppService, erro for _, db := range dbs { service := response.AppService{ Label: db.Name, - Value: db.Name, + Value: strconv.Itoa(int(db.ID)), } if db.AppInstallID > 0 { install, err := appInstallRepo.GetFirst(commonRepo.WithByID(db.AppInstallID)) diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 79896e94d..c6f982f66 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -187,19 +187,25 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall return buserr.New(constant.ErrDbUserNotValid) } } else { - var createMysql dto.MysqlDBCreate - createMysql.Name = dbConfig.DbName - createMysql.Username = dbConfig.DbUser - // createMysql.Database = dbInstall.Name - createMysql.Format = "utf8mb4" - createMysql.Permission = "%" - createMysql.Password = dbConfig.Password - createMysql.From = "local" - mysqldb, err := NewIMysqlService().Create(ctx, createMysql) - if err != nil { - return err + if databaseID, ok := params["PANEL_DB_HOST_ID"]; ok { + database, err := databaseRepo.Get(commonRepo.WithByID(databaseID.(uint))) + if err != nil { + return err + } + var createMysql dto.MysqlDBCreate + createMysql.Name = dbConfig.DbName + createMysql.Username = dbConfig.DbUser + createMysql.DatabaseID = database.ID + createMysql.Format = "utf8mb4" + createMysql.Permission = "%" + createMysql.Password = dbConfig.Password + createMysql.From = "local" + mysqldb, err := NewIMysqlService().Create(ctx, createMysql) + if err != nil { + return err + } + resourceId = mysqldb.ID } - resourceId = mysqldb.ID } } var installResource model.AppInstallResource @@ -252,17 +258,23 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b if err := deleteLink(ctx, &install, deleteDB, forceDelete, deleteBackup); err != nil && !forceDelete { return err } - - // if DatabaseKeys[install.App.Key] > 0 { - // _ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID)) - // } + 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)) + } switch install.App.Key { case constant.AppOpenresty: _ = websiteRepo.DeleteAll(ctx) _ = websiteDomainRepo.DeleteAll(ctx) case constant.AppMysql, constant.AppMariaDB: - // _ = mysqlRepo.Delete(ctx, mysqlRepo.WithByMysqlName(install.Name)) + if len(databaseIDs) > 0 { + _ = mysqlRepo.Delete(ctx, mysqlRepo.WithDatabaseIDIn(databaseIDs)) + } } _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("app"), commonRepo.WithByName(install.App.Key), backupRepo.WithByDetailName(install.Name)) @@ -284,7 +296,6 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b } func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool, deleteBackup bool) error { - resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID)) if len(resources) == 0 { return nil @@ -292,16 +303,15 @@ func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, f for _, re := range resources { mysqlService := NewIMysqlService() if (re.Key == constant.AppMysql || re.Key == constant.AppMariaDB) && deleteDB { - database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId)) - if reflect.DeepEqual(database, model.DatabaseMysql{}) { + mysqlDatabase, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId)) + if mysqlDatabase.ID == 0 { continue } if err := mysqlService.Delete(ctx, dto.MysqlDBDelete{ - ID: database.ID, + ID: mysqlDatabase.ID, ForceDelete: forceDelete, DeleteBackup: deleteBackup, Type: re.Key, - // Database: database.MysqlName, }); err != nil && !forceDelete { return err } diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 02b3d2c23..973c2ddf8 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -96,8 +96,6 @@ var ( ErrUserIsExist = "ErrUserIsExist" ErrDatabaseIsExist = "ErrDatabaseIsExist" ErrExecTimeOut = "ErrExecTimeOut" - ErrRemoteExist = "ErrRemoteExist" - ErrLocalExist = "ErrLocalExist" ) // redis diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 7f98ee4bd..dbc20f440 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -82,8 +82,6 @@ ErrSSLCertificateFormat: 'Certificate file format error, please use pem format' ErrUserIsExist: "The current user already exists. Please enter a new user" ErrDatabaseIsExist: "The current database already exists. Please enter a new database" ErrExecTimeOut: "SQL execution timed out, please check the database" -ErrRemoteExist: "The remote database already exists with that name, please modify it and try again" -ErrLocalExist: "The local database already exists with that name, please modify it and try again" #redis ErrTypeOfRedis: "The recovery file type does not match the current persistence mode. Modify the file type and try again" diff --git a/backend/i18n/lang/zh-Hant.yaml b/backend/i18n/lang/zh-Hant.yaml index 5372c71b3..7c7288129 100644 --- a/backend/i18n/lang/zh-Hant.yaml +++ b/backend/i18n/lang/zh-Hant.yaml @@ -82,8 +82,6 @@ ErrSSLCertificateFormat: '證書文件格式錯誤,請使用 pem 格式' ErrUserIsExist: "當前用戶已存在,請重新輸入" ErrDatabaseIsExist: "當前資料庫已存在,請重新輸入" ErrExecTimeOut: "SQL 執行超時,請檢查數據庫" -ErrRemoteExist: "遠程數據庫已存在該名稱,請修改後重試" -ErrLocalExist: "本地數據庫已存在該名稱,請修改後重試" #redis ErrTypeOfRedis: "恢復文件類型與當前持久化方式不符,請修改後重試" diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 4b46094b1..afec79309 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -82,8 +82,6 @@ ErrSSLCertificateFormat: '证书文件格式错误,请使用 pem 格式' ErrUserIsExist: "当前用户已存在,请重新输入" ErrDatabaseIsExist: "当前数据库已存在,请重新输入" ErrExecTimeOut: "SQL 执行超时,请检查数据库" -ErrRemoteExist: "远程数据库已存在该名称,请修改后重试" -ErrLocalExist: "本地数据库已存在该名称,请修改后重试" #redis ErrTypeOfRedis: "恢复文件类型与当前持久化方式不符,请修改后重试" diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index fc3e6806c..47a8128a0 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -44,6 +44,7 @@ func Init() { migrations.UpdateAppInstallResource, migrations.DropDatabaseLocal, migrations.AddDatabaseID, + migrations.RemoveDatabaseUnique, }) 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 30f49eae2..18100115f 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -735,6 +735,16 @@ var AddDatabaseID = &gormigrate.Migration{ }, } +var RemoveDatabaseUnique = &gormigrate.Migration{ + ID: "20230914-update-database", + Migrate: func(tx *gorm.DB) error { + if err := tx.AutoMigrate(&model.Database{}); err != nil { + return err + } + return nil + }, +} + func addDatabaseData(appType string, installRepo repo.IAppInstallRepo) *model.Database { dbInfo, err := installRepo.LoadBaseInfo(appType, "") if err == nil {