diff --git a/internal/biz/database_server.go b/internal/biz/database_server.go index a98bfbf57c..67cec7b17f 100644 --- a/internal/biz/database_server.go +++ b/internal/biz/database_server.go @@ -54,6 +54,7 @@ type DatabaseServerRepo interface { Count() (int64, error) List(page, limit uint) ([]*DatabaseServer, int64, error) Get(id uint) (*DatabaseServer, error) + GetByName(name string) (*DatabaseServer, error) Create(req *request.DatabaseServerCreate) error Update(req *request.DatabaseServerUpdate) error UpdateRemark(req *request.DatabaseServerUpdateRemark) error diff --git a/internal/data/database.go b/internal/data/database.go index 0b0503efcf..0733e41781 100644 --- a/internal/data/database.go +++ b/internal/data/database.go @@ -108,6 +108,9 @@ func (r databaseRepo) Create(req *request.DatabaseCreate) error { return err } } + if err = postgres.DatabaseComment(req.Name, req.Comment); err != nil { + return err + } } return nil diff --git a/internal/data/database_server.go b/internal/data/database_server.go index e9d41abcdd..fdfbbcd6f4 100644 --- a/internal/data/database_server.go +++ b/internal/data/database_server.go @@ -51,6 +51,17 @@ func (r databaseServerRepo) Get(id uint) (*biz.DatabaseServer, error) { return databaseServer, nil } +func (r databaseServerRepo) GetByName(name string) (*biz.DatabaseServer, error) { + databaseServer := new(biz.DatabaseServer) + if err := app.Orm.Where("name = ?", name).First(databaseServer).Error; err != nil { + return nil, err + } + + r.checkServer(databaseServer) + + return databaseServer, nil +} + func (r databaseServerRepo) Create(req *request.DatabaseServerCreate) error { databaseServer := &biz.DatabaseServer{ Name: req.Name, diff --git a/internal/data/website.go b/internal/data/website.go index 30bc488f2f..458c06ee58 100644 --- a/internal/data/website.go +++ b/internal/data/website.go @@ -302,34 +302,21 @@ func (r *websiteRepo) Create(req *request.WebsiteCreate) (*biz.Website, error) { } // 创建数据库 - rootPassword, err := NewSettingRepo().Get(biz.SettingKeyMySQLRootPassword) - if err == nil && req.DB && req.DBType == "mysql" { - mysql, err := db.NewMySQL("root", rootPassword, "/tmp/mysql.sock", "unix") - if err != nil { - return nil, err - } - if err = mysql.UserCreate(req.DBUser, req.DBPassword, "localhost"); err != nil { - return nil, err - } - if err = mysql.DatabaseCreate(req.DBName); err != nil { - return nil, err - } - if err = mysql.PrivilegesGrant(req.DBUser, req.DBName, "localhost"); err != nil { - return nil, err - } - } - if req.DB && req.DBType == "postgresql" { - postgres, err := db.NewPostgres("postgres", "", "127.0.0.1", 5432) + name := "local_" + req.DBType + if req.DB { + server, err := NewDatabaseServerRepo().GetByName(name) if err != nil { - return nil, err - } - if err = postgres.UserCreate(req.DBUser, req.DBPassword); err != nil { - return nil, err - } - if err = postgres.DatabaseCreate(req.DBName); err != nil { - return nil, err - } - if err = postgres.PrivilegesGrant(req.DBUser, req.DBName); err != nil { + return nil, fmt.Errorf(`create database: can't find %s database server, please add it first`, name) + } + if err = NewDatabaseRepo().Create(&request.DatabaseCreate{ + ServerID: server.ID, + Name: req.DBName, + CreateUser: true, + Username: req.DBUser, + Password: req.DBPassword, + Host: "localhost", + Comment: fmt.Sprintf("website %s", req.Name), + }); err != nil { return nil, err } } diff --git a/internal/http/request/database.go b/internal/http/request/database.go index 24be05370b..b99dea3f66 100644 --- a/internal/http/request/database.go +++ b/internal/http/request/database.go @@ -7,7 +7,7 @@ type DatabaseCreate struct { Username string `form:"username" json:"username" validate:"required_if=CreateUser true"` Password string `form:"password" json:"password" validate:"required_if=CreateUser true"` Host string `form:"host" json:"host"` - Remark string `form:"remark" json:"remark"` + Comment string `form:"comment" json:"comment"` } type DatabaseDelete struct { diff --git a/internal/route/cli.go b/internal/route/cli.go index 3d145cd5f2..9debfd581e 100644 --- a/internal/route/cli.go +++ b/internal/route/cli.go @@ -216,6 +216,19 @@ func Cli() []*cli.Command { }, }, }, + { + Name: "delete-server", + Usage: "删除数据库服务器", + Action: cliService.DatabaseDeleteServer, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "name", + Usage: "服务器名称", + Aliases: []string{"n"}, + Required: true, + }, + }, + }, }, }, { diff --git a/internal/service/cli.go b/internal/service/cli.go index 8f04c4305d..f9fadff1c2 100644 --- a/internal/service/cli.go +++ b/internal/service/cli.go @@ -500,6 +500,20 @@ func (s *CliService) DatabaseAddServer(ctx context.Context, cmd *cli.Command) er return nil } +func (s *CliService) DatabaseDeleteServer(ctx context.Context, cmd *cli.Command) error { + server, err := s.databaseServerRepo.GetByName(cmd.String("name")) + if err != nil { + return err + } + + if err = s.databaseServerRepo.Delete(server.ID); err != nil { + return err + } + + fmt.Printf("数据库服务器 %s 删除成功\n", server.Name) + return nil +} + func (s *CliService) BackupWebsite(ctx context.Context, cmd *cli.Command) error { fmt.Println(s.hr) fmt.Printf("★ 开始备份 [%s]\n", time.Now().Format(time.DateTime)) diff --git a/web/src/views/database/CreateServerModal.vue b/web/src/views/database/CreateServerModal.vue index 773d241433..2774d58399 100644 --- a/web/src/views/database/CreateServerModal.vue +++ b/web/src/views/database/CreateServerModal.vue @@ -33,7 +33,7 @@ const handleCreate = () => { useRequest(() => database.serverCreate(createModel.value)).onSuccess(() => { show.value = false window.$message.success('添加成功') - window.$bus.emit('database:refresh') + window.$bus.emit('database-server:refresh') }) }