Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(agent): support PostgreSQL connection testing (#21229)

Sbaaoui Idrissdd4715260475
diff --git a/internal/service/agent_dbcheck.go b/internal/service/agent_dbcheck.go
index 5433c87216..4d8ff8c321 100644
--- a/internal/service/agent_dbcheck.go
+++ b/internal/service/agent_dbcheck.go
@@ -24,11 +24,13 @@ import (
"fmt"
"net"
"net/netip"
+ "net/url"
"strconv"
"strings"
"time"
"github.com/go-sql-driver/mysql"
+ _ "github.com/lib/pq"
"go.uber.org/zap"
"ragflow/internal/common"
@@ -241,10 +243,11 @@ func (s *AgentService) TestDBConnection(ctx context.Context, userID string, req
return common.CodeDataError, err
}
+ var db *sql.DB
+ dbProbeTimeout := 5 * time.Second
switch req.DBType {
case "mysql", "mariadb", "oceanbase":
port := dbConnectionPort(req.Port)
- dbProbeTimeout := 5 * time.Second
config := mysql.Config{
User: req.Username,
Passwd: req.Password,
@@ -254,25 +257,36 @@ func (s *AgentService) TestDBConnection(ctx context.Context, userID string, req
Timeout: dbProbeTimeout,
AllowNativePasswords: true,
}
- var db *sql.DB
db, err = sql.Open("mysql", config.FormatDSN())
- if err != nil {
- return common.CodeExceptionError, err
- }
- defer db.Close()
-
- newCtx, cancel := context.WithTimeout(ctx, dbProbeTimeout)
- defer cancel()
-
- if err = db.PingContext(newCtx); err != nil {
- return common.CodeExceptionError, err
- }
- if _, err = db.ExecContext(newCtx, "SELECT 1"); err != nil {
- return common.CodeExceptionError, err
+ case "postgres", "postgresql":
+ dsn := url.URL{
+ Scheme: "postgres",
+ User: url.UserPassword(req.Username, req.Password),
+ Host: net.JoinHostPort(safeHost, dbConnectionPort(req.Port)),
+ Path: "/" + req.Database,
+ RawQuery: url.Values{
+ "sslmode": {"disable"},
+ "connect_timeout": {"5"},
+ }.Encode(),
}
+ db, err = sql.Open("postgres", dsn.String())
default:
return common.CodeExceptionError, errors.New("unsupported database type")
}
+ if err != nil {
+ return common.CodeExceptionError, err
+ }
+ defer db.Close()
+
+ newCtx, cancel := context.WithTimeout(ctx, dbProbeTimeout)
+ defer cancel()
+
+ if err = db.PingContext(newCtx); err != nil {
+ return common.CodeExceptionError, err
+ }
+ if _, err = db.ExecContext(newCtx, "SELECT 1"); err != nil {
+ return common.CodeExceptionError, err
+ }
return common.CodeSuccess, nil
}
diff --git a/internal/service/agent_test.go b/internal/service/agent_test.go
index fd9f833759..0951230e23 100644
--- a/internal/service/agent_test.go
+++ b/internal/service/agent_test.go
@@ -1539,7 +1539,7 @@ func TestTestDBConnectionMissingFields(t *testing.T) {
func TestTestDBConnectionUnsupportedDatabaseType(t *testing.T) {
ctx := t.Context()
code, err := NewAgentService().TestDBConnection(ctx, "user-1", &TestDBConnectionRequest{
- DBType: "postgres",
+ DBType: "unsupported",
Database: "rag_flow",
Username: "root",
Host: "8.8.8.8",