Skip to content

Commit

Permalink
feat: 优化创建网站创建数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 26, 2024
1 parent 3677d03 commit 5d5633b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
1 change: 1 addition & 0 deletions internal/biz/database_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/data/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions internal/data/database_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 14 additions & 27 deletions internal/data/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/http/request/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions internal/route/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
{
Expand Down
14 changes: 14 additions & 0 deletions internal/service/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/database/CreateServerModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
}
</script>
Expand Down

0 comments on commit 5d5633b

Please sign in to comment.