diff --git a/backend/app/repo/database.go b/backend/app/repo/database.go index 96ee6a439..873806da9 100644 --- a/backend/app/repo/database.go +++ b/backend/app/repo/database.go @@ -1,6 +1,7 @@ package repo import ( + "context" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/global" "gorm.io/gorm" @@ -11,20 +12,21 @@ type DatabaseRepo struct{} type IDatabaseRepo interface { GetList(opts ...DBOption) ([]model.Database, error) Page(limit, offset int, opts ...DBOption) (int64, []model.Database, error) - Create(database *model.Database) error + Create(ctx context.Context, database *model.Database) error Update(id uint, vars map[string]interface{}) error - Delete(opts ...DBOption) error + Delete(ctx context.Context, opts ...DBOption) error Get(opts ...DBOption) (model.Database, error) WithByFrom(from string) DBOption WithoutByFrom(from string) DBOption WithByMysqlList() DBOption + WithAppInstallID(appInstallID uint) DBOption } func NewIDatabaseRepo() IDatabaseRepo { return &DatabaseRepo{} } -func (u *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) { +func (d *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) { var database model.Database db := global.DB for _, opt := range opts { @@ -34,7 +36,7 @@ func (u *DatabaseRepo) Get(opts ...DBOption) (model.Database, error) { return database, err } -func (u *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Database, error) { +func (d *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Database, error) { var users []model.Database db := global.DB.Model(&model.Database{}) for _, opt := range opts { @@ -46,7 +48,7 @@ func (u *DatabaseRepo) Page(page, size int, opts ...DBOption) (int64, []model.Da return count, users, err } -func (u *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) { +func (d *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) { var databases []model.Database db := global.DB.Model(&model.Database{}) for _, opt := range opts { @@ -56,36 +58,37 @@ func (u *DatabaseRepo) GetList(opts ...DBOption) ([]model.Database, error) { return databases, err } -func (c *DatabaseRepo) WithByMysqlList() DBOption { +func (d *DatabaseRepo) WithByMysqlList() DBOption { return func(g *gorm.DB) *gorm.DB { - return g.Where("type == ? OR type == ?", "mysql", "mariadb") + return g.Where("type = ? OR type = ?", "mysql", "mariadb") } } -func (c *DatabaseRepo) WithByFrom(from string) DBOption { +func (d *DatabaseRepo) WithByFrom(from string) DBOption { return func(g *gorm.DB) *gorm.DB { - return g.Where("`from` == ?", from) + return g.Where("`from` = ?", from) } } -func (c *DatabaseRepo) WithoutByFrom(from string) DBOption { +func (d *DatabaseRepo) WithoutByFrom(from string) DBOption { return func(g *gorm.DB) *gorm.DB { return g.Where("`from` != ?", from) } } - -func (u *DatabaseRepo) Create(database *model.Database) error { - return global.DB.Create(database).Error +func (d *DatabaseRepo) WithAppInstallID(appInstallID uint) DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("app_install_id = ?", appInstallID) + } } -func (u *DatabaseRepo) Update(id uint, vars map[string]interface{}) error { +func (d *DatabaseRepo) Create(ctx context.Context, database *model.Database) error { + return getTx(ctx).Create(database).Error +} + +func (d *DatabaseRepo) Update(id uint, vars map[string]interface{}) error { return global.DB.Model(&model.Database{}).Where("id = ?", id).Updates(vars).Error } -func (u *DatabaseRepo) Delete(opts ...DBOption) error { - db := global.DB - for _, opt := range opts { - db = opt(db) - } - return db.Delete(&model.Database{}).Error +func (d *DatabaseRepo) Delete(ctx context.Context, opts ...DBOption) error { + return getTx(ctx, opts...).Delete(&model.Database{}).Error } diff --git a/backend/app/service/app.go b/backend/app/service/app.go index c2d52a7e0..afbfc0884 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -352,13 +352,20 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( appInstall.ContainerName = containerName index := 0 + serviceName := "" for k := range servicesMap { - appInstall.ServiceName = k + serviceName = k if index > 0 { continue } index++ } + if app.Limit == 0 { + servicesMap[appInstall.Name] = servicesMap[serviceName] + delete(servicesMap, serviceName) + serviceName = appInstall.Name + } + appInstall.ServiceName = serviceName if err = addDockerComposeCommonParam(composeMap, appInstall.ServiceName, req.AppContainerConfig, req.Params); err != nil { return diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 2bbec1972..5ef3a0c94 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -96,7 +96,24 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall return err } appInstall.Param = string(authByte) + if app.Key == "mysql" || app.Key == "mariadb" { + database := &model.Database{ + AppInstallID: appInstall.ID, + Name: appInstall.Name, + Type: app.Key, + Version: appInstall.Version, + From: "local", + Address: appInstall.ServiceName, + Username: "root", + Password: password.(string), + Port: 3306, + } + if err := databaseRepo.Create(ctx, database); err != nil { + return err + } + } } + } case "redis": if password, ok := params["PANEL_REDIS_ROOT_PASSWORD"]; ok { @@ -141,6 +158,7 @@ func createLink(ctx context.Context, app model.App, appInstall *model.AppInstall 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 @@ -229,13 +247,16 @@ func deleteAppInstall(install model.AppInstall, deleteBackup bool, forceDelete b } func deleteLink(ctx context.Context, install *model.AppInstall, deleteDB bool, forceDelete bool) error { + if install.App.Key == "mysql" || install.App.Key == "mariadb" { + _ = databaseRepo.Delete(ctx, databaseRepo.WithAppInstallID(install.ID)) + } resources, _ := appInstallResourceRepo.GetBy(appInstallResourceRepo.WithAppInstallId(install.ID)) if len(resources) == 0 { return nil } for _, re := range resources { mysqlService := NewIMysqlService() - if re.Key == "mysql" && deleteDB { + if re.Key == "mysql" || re.Key == "mariadb" && deleteDB { database, _ := mysqlRepo.Get(commonRepo.WithByID(re.ResourceId)) if reflect.DeepEqual(database, model.DatabaseMysql{}) { continue diff --git a/backend/app/service/database.go b/backend/app/service/database.go index 71b0fc689..39a6c541f 100644 --- a/backend/app/service/database.go +++ b/backend/app/service/database.go @@ -109,7 +109,7 @@ func (u *DatabaseService) Create(req dto.DatabaseCreate) error { if err := copier.Copy(&db, &req); err != nil { return errors.WithMessage(constant.ErrStructTransform, err.Error()) } - if err := databaseRepo.Create(&db); err != nil { + if err := databaseRepo.Create(context.Background(), &db); err != nil { return err } return nil @@ -120,7 +120,7 @@ func (u *DatabaseService) Delete(id uint) error { if db.ID == 0 { return constant.ErrRecordNotFound } - if err := databaseRepo.Delete(commonRepo.WithByID(id)); err != nil { + if err := databaseRepo.Delete(context.Background(), commonRepo.WithByID(id)); err != nil { return err } if db.From != "local" {