From ef2cb1b3159075312a880b198174b8a30d140023 Mon Sep 17 00:00:00 2001 From: CityFun <31820853+zhengkunwang223@users.noreply.github.com> Date: Wed, 16 Jul 2025 17:44:20 +0800 Subject: [PATCH] feat: Adapt to PostgreSQL cluster (#9529) --- agent/app/service/app_install.go | 8 ++++++-- agent/app/service/database.go | 2 +- agent/app/service/database_common.go | 2 ++ agent/utils/postgresql/client.go | 2 +- agent/utils/postgresql/client/info.go | 1 + agent/utils/postgresql/client/local.go | 2 +- frontend/src/views/database/postgresql/index.vue | 2 +- 7 files changed, 13 insertions(+), 6 deletions(-) diff --git a/agent/app/service/app_install.go b/agent/app/service/app_install.go index ced89b67e..098572566 100644 --- a/agent/app/service/app_install.go +++ b/agent/app/service/app_install.go @@ -473,13 +473,17 @@ func (a *AppInstallService) SyncAll(systemInit bool) error { func (a *AppInstallService) GetServices(key string) ([]response.AppService, error) { var res []response.AppService if DatabaseKeys[key] > 0 { + types := []string{key} if key == constant.AppPostgres { key = constant.AppPostgresql } - types := []string{key} - if key == constant.AppMysql { + switch key { + case constant.AppMysql: types = []string{constant.AppMysql, constant.AppMysqlCluster} + case constant.AppPostgresql: + types = []string{constant.AppPostgresql, constant.AppPostgresqlCluster} } + dbs, _ := databaseRepo.GetList(repo.WithTypes(types)) if len(dbs) == 0 { return res, nil diff --git a/agent/app/service/database.go b/agent/app/service/database.go index 66393af7a..b812d7a99 100644 --- a/agent/app/service/database.go +++ b/agent/app/service/database.go @@ -92,7 +92,7 @@ func (u *DatabaseService) LoadItems(dbType string) ([]dto.DatabaseItem, error) { dbs, err := databaseRepo.GetList(databaseRepo.WithTypeList(dbType)) var datas []dto.DatabaseItem for _, db := range dbs { - if dbType == "postgresql" { + if dbType == constant.AppPostgresql || dbType == constant.AppPostgresqlCluster { items, _ := postgresqlRepo.List(postgresqlRepo.WithByPostgresqlName(db.Name)) for _, item := range items { var dItem dto.DatabaseItem diff --git a/agent/app/service/database_common.go b/agent/app/service/database_common.go index 1b23c1c77..be794d620 100644 --- a/agent/app/service/database_common.go +++ b/agent/app/service/database_common.go @@ -44,6 +44,8 @@ func (u *DBCommonService) LoadDatabaseFile(req dto.OperationWithNameAndType) (st switch req.Type { case "mysql-cluster-conf": filePath = path.Join(global.Dir.DataDir, fmt.Sprintf("apps/mysql-cluster/%s/conf/my.cnf", req.Name)) + case "postgresql-cluster-conf": + filePath = path.Join(global.Dir.DataDir, fmt.Sprintf("apps/postgresql-cluster/%s/data/postgresql.conf", req.Name)) case "mysql-conf": filePath = path.Join(global.Dir.DataDir, fmt.Sprintf("apps/mysql/%s/conf/my.cnf", req.Name)) case "mariadb-conf": diff --git a/agent/utils/postgresql/client.go b/agent/utils/postgresql/client.go index aea8e3c7f..e9888ee6b 100644 --- a/agent/utils/postgresql/client.go +++ b/agent/utils/postgresql/client.go @@ -28,7 +28,7 @@ type PostgresqlClient interface { func NewPostgresqlClient(conn client.DBInfo) (PostgresqlClient, error) { if conn.From == "local" { - connArgs := []string{"exec", conn.Address, "psql", "-t", "-U", conn.Username, "-c"} + connArgs := []string{"exec", "-e", fmt.Sprintf("PGPASSWORD=%s", conn.Password), conn.Address, "psql", "-t", "-U", conn.Username, "-c"} return client.NewLocal(connArgs, conn.Address, conn.Username, conn.Password, conn.Database), nil } escapedUsername := url.QueryEscape(conn.Username) diff --git a/agent/utils/postgresql/client/info.go b/agent/utils/postgresql/client/info.go index b1b789dcf..ee04e56b7 100644 --- a/agent/utils/postgresql/client/info.go +++ b/agent/utils/postgresql/client/info.go @@ -11,6 +11,7 @@ type DBInfo struct { Port uint `json:"port"` Username string `json:"userName"` Password string `json:"password"` + AppKey string `json:"appKey"` Timeout uint `json:"timeout"` // second } diff --git a/agent/utils/postgresql/client/local.go b/agent/utils/postgresql/client/local.go index 14fc4e64d..8c8313685 100644 --- a/agent/utils/postgresql/client/local.go +++ b/agent/utils/postgresql/client/local.go @@ -136,7 +136,7 @@ func (r *Local) Backup(info BackupInfo) error { } defer outfile.Close() global.LOG.Infof("start to pg_dump | gzip > %s.gzip", info.TargetDir+"/"+info.FileName) - cmd := exec.Command("docker", "exec", r.ContainerName, "pg_dump", "-F", "c", "-U", r.Username, "-d", info.Name) + cmd := exec.Command("docker", "exec", "-e", fmt.Sprintf("PGPASSWORD=%s", r.Password), r.ContainerName, "pg_dump", "-F", "c", "-U", r.Username, "-d", info.Name) var stderr bytes.Buffer cmd.Stderr = &stderr diff --git a/frontend/src/views/database/postgresql/index.vue b/frontend/src/views/database/postgresql/index.vue index 98966d219..8db62d205 100644 --- a/frontend/src/views/database/postgresql/index.vue +++ b/frontend/src/views/database/postgresql/index.vue @@ -445,7 +445,7 @@ const checkExist = (data: App.CheckInstalled) => { const loadDBOptions = async () => { try { - const res = await listDatabases('postgresql'); + const res = await listDatabases('postgresql,postgresql-cluster'); let datas = res.data || []; dbOptionsLocal.value = []; dbOptionsRemote.value = [];