feat: 远程数据库增加默认数据

pull/1748/head
ssongliu 2023-07-24 11:24:38 +08:00 committed by ssongliu
parent 7f79f5f031
commit cd742b0933
4 changed files with 67 additions and 51 deletions

View File

@ -15,6 +15,7 @@ type IRemoteDBRepo interface {
Update(id uint, vars map[string]interface{}) error
Delete(opts ...DBOption) error
Get(opts ...DBOption) (model.RemoteDB, error)
WithByFrom(from string) DBOption
WithoutByFrom(from string) DBOption
}
@ -54,6 +55,12 @@ func (u *RemoteDBRepo) GetList(opts ...DBOption) ([]model.RemoteDB, error) {
return databases, err
}
func (c *RemoteDBRepo) WithByFrom(from string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`from` != ?", from)
}
}
func (c *RemoteDBRepo) WithoutByFrom(from string) DBOption {
return func(g *gorm.DB) *gorm.DB {
return g.Where("`from` != ?", from)

View File

@ -82,46 +82,31 @@ func (u *MysqlService) Create(ctx context.Context, req dto.MysqlDBCreate) (*mode
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
if req.Username == "root" {
if req.From == "local" && req.Username == "root" {
return nil, errors.New("Cannot set root as user name")
}
dbInfo := client.DBInfo{
From: req.From,
Timeout: 300,
if req.From == "127.0.0.1" {
return nil, errors.New("Cannot set 127.0.0.1 as address")
}
version := ""
cli, version, err := loadClientByFrom(req.From)
if err != nil {
return nil, err
}
if req.From == "local" {
app, err := appInstallRepo.LoadBaseInfo("mysql", "")
if err != nil {
return nil, err
}
mysqlData, _ := mysqlRepo.Get(commonRepo.WithByName(req.Name))
if mysqlData.ID != 0 {
return nil, constant.ErrRecordExist
}
dbInfo.Address = app.ContainerName
dbInfo.Username = "root"
dbInfo.Password = app.Password
version = app.Version
createItem.MysqlName = app.Name
} else {
mysqlData, err := remoteDBRepo.Get(commonRepo.WithByName(req.From))
mysqlData, err := remoteDBRepo.Get(remoteDBRepo.WithByFrom(req.From))
if err != nil {
return nil, err
}
dbInfo.Address = mysqlData.Address
dbInfo.Port = mysqlData.Port
dbInfo.Username = mysqlData.Username
dbInfo.Password = mysqlData.Password
version = mysqlData.Version
createItem.MysqlName = mysqlData.Name
}
cli, err := mysql.NewMysqlClient(dbInfo)
if err != nil {
return nil, err
}
defer cli.Close()
if err := cli.Create(client.CreateInfo{
Name: req.Name,
@ -169,15 +154,15 @@ func (u *MysqlService) DeleteCheck(id uint) ([]string, error) {
}
func (u *MysqlService) Delete(ctx context.Context, req dto.MysqlDBDelete) error {
cli, version, err := loadClientByID(req.ID)
if err != nil {
return err
}
defer cli.Close()
db, err := mysqlRepo.Get(commonRepo.WithByID(req.ID))
if err != nil && !req.ForceDelete {
return err
}
cli, version, err := loadClientByFrom(db.From)
if err != nil {
return err
}
defer cli.Close()
if err := cli.Delete(client.DeleteInfo{
Name: db.Name,
Version: version,
@ -218,7 +203,7 @@ func (u *MysqlService) ChangePassword(info dto.ChangeDBInfo) error {
if cmd.CheckIllegal(info.Value) {
return buserr.New(constant.ErrCmdIllegal)
}
cli, version, err := loadClientByID(info.ID)
cli, version, err := loadClientByFrom(info.From)
if err != nil {
return err
}
@ -281,7 +266,7 @@ func (u *MysqlService) ChangeAccess(info dto.ChangeDBInfo) error {
if cmd.CheckIllegal(info.Value) {
return buserr.New(constant.ErrCmdIllegal)
}
cli, version, err := loadClientByID(info.ID)
cli, version, err := loadClientByFrom(info.From)
if err != nil {
return err
}
@ -546,25 +531,15 @@ func updateMyCnf(oldFiles []string, group string, param string, value interface{
return newFiles
}
func loadClientByID(id uint) (mysql.MysqlClient, string, error) {
func loadClientByFrom(from string) (mysql.MysqlClient, string, error) {
var (
mysqlData model.DatabaseMysql
dbInfo client.DBInfo
version string
err error
dbInfo client.DBInfo
version string
err error
)
dbInfo.From = "local"
if id != 0 {
mysqlData, err = mysqlRepo.Get(commonRepo.WithByID(id))
if err != nil {
return nil, "", err
}
dbInfo.From = mysqlData.From
}
if dbInfo.From != "local" {
databaseItem, err := remoteDBRepo.Get(commonRepo.WithByName(mysqlData.From))
if from != "local" {
databaseItem, err := remoteDBRepo.Get(remoteDBRepo.WithByFrom(from))
if err != nil {
return nil, "", err
}

View File

@ -1,6 +1,8 @@
package migrations
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@ -481,9 +483,41 @@ var EncryptHostPassword = &gormigrate.Migration{
}
var AddRemoteDB = &gormigrate.Migration{
ID: "20230720-add-remote-db",
ID: "20230724-add-remote-db",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&model.RemoteDB{}); err != nil {
if err := tx.AutoMigrate(&model.RemoteDB{}, &model.DatabaseMysql{}); err != nil {
return err
}
var (
app model.App
appInstall model.AppInstall
)
if err := global.DB.Where("key = ?", "mysql").First(&app).Error; err != nil {
return err
}
if err := global.DB.Where("app_id = ?", app.ID).First(&appInstall).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
envMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(appInstall.Env), &envMap); err != nil {
return err
}
password, ok := envMap["PANEL_DB_ROOT_PASSWORD"].(string)
if !ok {
return errors.New("error password in app env")
}
if err := tx.Create(&model.RemoteDB{
Name: "local",
Type: "mysql",
Version: appInstall.Version,
From: "local",
Address: "127.0.0.1",
Username: "root",
Password: password,
}).Error; err != nil {
return err
}
return nil

View File

@ -109,7 +109,7 @@ const acceptParams = (params: DialogProps): void => {
? i18n.global.t('database.changePassword')
: i18n.global.t('database.permission');
changeForm.id = params.id;
changeForm.from = params.from === 'local' ? 'local' : 'remote';
changeForm.from = params.from;
changeForm.mysqlName = params.mysqlName;
changeForm.userName = params.username;
changeForm.password = params.password;