Skip to content

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

feat(vastbase): support Vastbase G100 as the metadata database in Go (#20913)

kk71089199670f751
diff --git a/conf/service_conf.yaml b/conf/service_conf.yaml
index 8f52d3b26a..e091603b14 100644
--- a/conf/service_conf.yaml
+++ b/conf/service_conf.yaml
@@ -72,6 +72,11 @@ vastbase:
user: 'ragflow'
password: 'Infini_Rag@123'
db_name: 'ragflow'
+ # Metadata database, read only when DB_TYPE=vastbase; host/port/user/
+ # password/ssl_mode above are shared with the doc engine.
+ name: 'rag_flow'
+ max_connections: 50
+ stale_timeout: 300
dbcompatibility: 'B'
ssl_mode: '' # empty: engine picks by host (disable on loopback, verify-full otherwise)
kvrocks:
diff --git a/docker/.env b/docker/.env
index 777f05f04a..fa1b7693d0 100644
--- a/docker/.env
+++ b/docker/.env
@@ -36,7 +36,7 @@
DOC_ENGINE=${DOC_ENGINE:-elasticsearch}
# The business metadata database type used by DB_TYPE.
-# Available options: mysql (default), postgres, gaussdb, oceanbase.
+# Available options: mysql (default), postgres, gaussdb, oceanbase, vastbase.
DB_TYPE=${DB_TYPE:-mysql}
# Device on which deepdoc inference run.
@@ -48,7 +48,9 @@ DEVICE=${DEVICE:-cpu}
# This controls only the bundled metadata service started by Compose; DB_TYPE
# independently selects the metadata backend used by RAGFlow. Keep `mysql` to
# start the in-cluster MySQL service. For external GaussDB, set both
-# DB_TYPE=gaussdb and METADATA_DB_PROFILE=gaussdb so MySQL stays disabled.
+# DB_TYPE=gaussdb and METADATA_DB_PROFILE=gaussdb so MySQL stays disabled.
+# For vastbase serving as both doc engine and metadata database, set DB_TYPE=vastbase
+# and METADATA_DB_PROFILE=vastbase so MySQL stays disabled.
METADATA_DB_PROFILE=${METADATA_DB_PROFILE:-mysql}
COMPOSE_PROFILES=${DOC_ENGINE},${DEVICE},metadata-${METADATA_DB_PROFILE}
@@ -176,6 +178,9 @@ VB_USERNAME=ragflow
VB_PASSWORD=Infini_Rag@123
# The doc database of the Vastbase service to use
VB_VEC_DBNAME=ragflow
+# The metadata database of the Vastbase service to use, when DB_TYPE=vastbase.
+# Created by the init script alongside the doc database.
+VB_META_DBNAME=rag_flow
# The compatibility mode of Vastbase.
# Available options:
# - `B` (default): MySQL-compatible mode
diff --git a/docker/docker-compose-base.yml b/docker/docker-compose-base.yml
index eedc41d234..4521dfebaf 100644
--- a/docker/docker-compose-base.yml
+++ b/docker/docker-compose-base.yml
@@ -185,15 +185,25 @@ services:
vastbase:
+ # `vastbase` serves the doc engine; `metadata-vastbase` additionally lets
+ # the same instance carry the business metadata (DB_TYPE=vastbase).
profiles:
- vastbase
+ - metadata-vastbase
image: ${VB_IMAGE:-vastbase-g100-single-v1.4-3.0.9.31338-linux-x86_64:20260513112809}
env_file: .env
environment:
- VB_PASSWORD=${VB_PASSWORD}
- VB_USERNAME=${VB_USERNAME}
- VB_VEC_DBNAME=${VB_VEC_DBNAME:-ragflow}
+ - VB_META_DBNAME=${VB_META_DBNAME:-rag_flow}
- VB_DBCOMPATIBILITY=${VB_DBCOMPATIBILITY:-B}
+ # MySQL-dialect catalog compatibility: the metadata migrations probe the
+ # catalog with uppercase MySQL-style names (INFORMATION_SCHEMA.COLUMNS,
+ # STATISTICS, KEY_COLUMN_USAGE). lower_case_table_names=1 folds unquoted
+ # identifiers to lowercase so those views resolve; without it Vastbase
+ # keeps the written case and every probe fails.
+ - OTHER_PG_CONF=lower_case_table_names=1
# Bind the published port to loopback by default: the database ships
# default credentials, and RAGFlow reaches it over the shared `ragflow`
# network. Remote access is an explicit opt-in through VB_BIND_ADDR
diff --git a/docker/service_conf.yaml.template b/docker/service_conf.yaml.template
index 9e59d8583b..06881e19af 100644
--- a/docker/service_conf.yaml.template
+++ b/docker/service_conf.yaml.template
@@ -97,6 +97,11 @@ vastbase:
user: '${VB_USERNAME:-ragflow}'
password: '${VB_PASSWORD:-Infini_Rag@123}'
db_name: '${VB_VEC_DBNAME:-ragflow}'
+ # Metadata database, read only when DB_TYPE=vastbase; host/port/user/
+ # password/ssl_mode above are shared with the doc engine.
+ name: '${VB_META_DBNAME:-rag_flow}'
+ max_connections: 50
+ stale_timeout: 300
dbcompatibility: '${VB_DBCOMPATIBILITY:-B}'
# No disable fallback: an empty ssl_mode reaches the engine's host policy
# (disable on loopback, verify-full otherwise). docker/.env sets disable
diff --git a/docker/vastbase/init/01_init.sh b/docker/vastbase/init/01_init.sh
index bd974046fb..67c44c6cc3 100755
--- a/docker/vastbase/init/01_init.sh
+++ b/docker/vastbase/init/01_init.sh
@@ -11,6 +11,7 @@ VB_USER="${VB_USERNAME:-ragflow}"
# (upper + lower + digit + special chars, >= 8 chars)
VB_PASS="${VB_PASSWORD:-Infini_Rag@123}"
VB_VEC_DB="${VB_VEC_DBNAME:-ragflow}"
+VB_META_DB="${VB_META_DBNAME:-rag_flow}"
# SQL encoding — shell quoting and the temp file below perform no SQL
# escaping, so encode here: string literals double embedded single quotes,
@@ -21,6 +22,9 @@ VB_USER_LIT=${VB_USER//\'/\'\'}
VB_USER_IDENT=${VB_USER//\"/\"\"}
VB_PASS_LIT=${VB_PASS//\'/\'\'}
VB_DB_IDENT=${VB_VEC_DB//\"/\"\"}
+VB_META_DB_IDENT=${VB_META_DB//\"/\"\"}
+VB_DB_LIT=${VB_VEC_DB//\'/\'\'}
+VB_META_DB_LIT=${VB_META_DB//\'/\'\'}
# ── Step 1: Create user ──────────────────────────────────────────────────
# Role existence is probed with a plain query first so the CREATE can be a
@@ -48,15 +52,56 @@ fi
# ── Step 2: Create databases (outside DO block — CREATE DATABASE requires
# a transaction commit and cannot run inside a PL/pgSQL block) ──
+# Probe pg_database first (same pattern as the role probe in Step 1): a
+# CREATE that fails for any reason must abort this script with its real
+# error on stderr. The previous form piped stderr to /dev/null and printed
+# "already exists" for EVERY failure, so e.g. a rejected DBCOMPATIBILITY
+# value surfaced only later as GRANT's "database does not exist".
+db_exists() {
+ vsql -tA -c "SELECT 1 FROM pg_database WHERE datname = '${1}'" | grep -qx 1
+}
+
echo "Creating database ${VB_VEC_DB}..."
-vsql -c "CREATE DATABASE \"${VB_DB_IDENT}\" OWNER \"${VB_USER_IDENT}\"" 2>/dev/null \
- || echo " Database '${VB_VEC_DB}' already exists"
+if db_exists "${VB_DB_LIT}"; then
+ echo " Database '${VB_VEC_DB}' already exists"
+else
+ vsql -c "CREATE DATABASE \"${VB_DB_IDENT}\" OWNER \"${VB_USER_IDENT}\""
+fi
+
+# The metadata database must end up in B (MySQL) compatibility mode: the Go
+# metadata path issues MySQL-flavored SQL (unquoted references to the reserved
+# user table, DATE_FORMAT, REGEXP, DELETE ... LIMIT) through the PostgreSQL
+# wire protocol, which only B mode accepts. G100 has no per-database
+# DBCOMPATIBILITY option (CREATE DATABASE rejects it with "option not
+# recognized"); the image entrypoint fixes the mode instance-wide at vb_initdb
+# time via VB_DBCOMPATIBILITY, and every database on the instance inherits
+# it. The verify block below fails init if that inheritance did not produce
+# B mode (e.g. the instance was initialized with VB_DBCOMPATIBILITY=PG).
+echo "Creating database ${VB_META_DB}..."
+if db_exists "${VB_META_DB_LIT}"; then
+ echo " Database '${VB_META_DB}' already exists"
+else
+ vsql -c "CREATE DATABASE \"${VB_META_DB_IDENT}\" OWNER \"${VB_USER_IDENT}\""
+fi
+
+# Fail init here rather than at Go startup if the metadata database is not
+# in B mode: the MySQL-flavored metadata SQL cannot run in any other mode.
+# datcompatibility is the same column the doc engine verifies against.
+meta_mode="$(vsql -tA -c "SELECT datcompatibility FROM pg_database WHERE datname = '${VB_META_DB_LIT}'")"
+meta_mode="${meta_mode//[[:space:]]/}"
+if [ "${meta_mode}" != "B" ]; then
+ echo "ERROR: metadata database '${VB_META_DB}' is in '${meta_mode}' compatibility mode, expected 'B'" >&2
+ exit 1
+fi
# ── Step 3: Grant database-level privileges ──────────────────────────────
echo "Granting privileges on ${VB_VEC_DB}..."
vsql -c "GRANT ALL PRIVILEGES ON DATABASE \"${VB_DB_IDENT}\" TO \"${VB_USER_IDENT}\";"
+echo "Granting privileges on ${VB_META_DB}..."
+vsql -c "GRANT ALL PRIVILEGES ON DATABASE \"${VB_META_DB_IDENT}\" TO \"${VB_USER_IDENT}\";"
+
# ── Step 4: Grant schema permissions (must connect to each database) ─────
echo "Granting schema permissions on ${VB_VEC_DB}..."
@@ -64,4 +109,9 @@ vsql -d "${VB_VEC_DB}" -c "GRANT ALL ON SCHEMA public TO \"${VB_USER_IDENT}\";"
vsql -d "${VB_VEC_DB}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO \"${VB_USER_IDENT}\";"
vsql -d "${VB_VEC_DB}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO \"${VB_USER_IDENT}\";"
+echo "Granting schema permissions on ${VB_META_DB}..."
+vsql -d "${VB_META_DB}" -c "GRANT ALL ON SCHEMA public TO \"${VB_USER_IDENT}\";"
+vsql -d "${VB_META_DB}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO \"${VB_USER_IDENT}\";"
+vsql -d "${VB_META_DB}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO \"${VB_USER_IDENT}\";"
+
echo "ragflow initialization complete."
diff --git a/go.mod b/go.mod
index e3daad59ce..8d4c45cff6 100644
--- a/go.mod
+++ b/go.mod
@@ -92,6 +92,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.2
+ gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.7
)
diff --git a/go.sum b/go.sum
index a5e1ab748f..3dba24fa76 100644
--- a/go.sum
+++ b/go.sum
@@ -888,6 +888,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
+gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM=
+gorm.io/driver/postgres v1.5.7/go.mod h1:3e019WlBaYI5o5LIdNV+LyxCMNtLOQETBXL2h4chKpA=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
diff --git a/internal/admin/service.go b/internal/admin/service.go
index b08f81ce24..6bb154f21e 100644
--- a/internal/admin/service.go
+++ b/internal/admin/service.go
@@ -717,6 +717,9 @@ func (s *Service) ListServices(ctx context.Context) ([]ServiceStatus, error) {
case "mysql":
mysqlStatus := s.getMySQLStatus(ctx)
results = append(results, mysqlStatus)
+ case "vastbase":
+ vastbaseConfig := globalConfig.GetVastbaseMetaConfig()
+ results = append(results, s.pingDatabaseStatus(ctx, "vastbase", vastbaseConfig.Host, vastbaseConfig.Port))
default:
mysqlConfig := globalConfig.GetMySQLConfig()
results = append(results, newServiceStatus("database", databaseType, mysqlConfig.Host, mysqlConfig.Port, "not available", time.Now(), "not supported database type"))
@@ -861,6 +864,29 @@ func (s *Service) getMySQLStatus(ctx context.Context) ServiceStatus {
return newServiceStatus(serviceType, name, mysqlConfig.Host, mysqlConfig.Port, "alive", startTime, "")
}
+// pingDatabaseStatus reports the SQL metadata database status for the
+// non-MySQL backends. The connectivity probe is the same SELECT 1 ping the
+// MySQL path uses; only the reported endpoint differs.
+func (s *Service) pingDatabaseStatus(ctx context.Context, name, host string, port int) ServiceStatus {
+ serviceType := "database"
+ startTime := time.Now()
+
+ sqlDB, err := dao.DB.DB()
+ if err != nil {
+ return newServiceStatus(serviceType, name, host, port, "not connected", startTime, err.Error())
+ }
+
+ // The DSN allows a 30s connect_timeout, so an unreachable host would
+ // otherwise hold ListServices for the full dial timeout.
+ pingCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
+ defer cancel()
+ if err = sqlDB.PingContext(pingCtx); err != nil {
+ return newServiceStatus(serviceType, name, host, port, "timeout", startTime, err.Error())
+ }
+
+ return newServiceStatus(serviceType, name, host, port, "alive", startTime, "")
+}
+
// getKvrocksStatus gets the Kvrocks service status.
func (s *Service) getKvrocksStatus(ctx context.Context) ServiceStatus {
diff --git a/internal/dao/conversation_history.go b/internal/dao/conversation_history.go
index d300d8a73c..370fc3aec0 100644
--- a/internal/dao/conversation_history.go
+++ b/internal/dao/conversation_history.go
@@ -48,6 +48,35 @@ type ConversationHistoryOptions struct {
IncludeElapsedMs bool
}
+// historyModel returns the typed row model for a history child table. History
+// inserts must carry it: GORM's create callback calls result.LastInsertId()
+// on every schema-less (Table+map) insert, which MySQL answers with a silent
+// 0 but lib/pq — the Vastbase driver — rejects with "LastInsertId is not
+// supported by this driver", failing writes that actually succeeded. With the
+// model attached GORM sees the composite string primary key and skips the
+// read-back entirely.
+func historyModel(table string) interface{} {
+ switch table {
+ case conversationMessageTable:
+ return &entity.ConversationMessage{}
+ case conversationReferenceTable:
+ return &entity.ConversationReference{}
+ case apiConversationMessageTable:
+ return &entity.API4ConversationMessage{}
+ case apiConversationReferenceTable:
+ return &entity.API4ConversationReference{}
+ default:
+ panic("unknown conversation history table: " + table)
+ }
+}
+
+type conversationHistoryRow struct {
+ ConversationID string `gorm:"column:conversation_id"`
+ Position int `gorm:"column:position"`
+ Payload string `gorm:"column:payload"`
+ MessagePos *int `gorm:"column:message_position"`
+}
+
// ConversationHistoryUpdate changes only the addressed turn, never synchronizing
// historical payloads. QuestionID places an answer in its question's reserved slot.
type ConversationHistoryUpdate struct {
@@ -145,7 +174,7 @@ func updateConversationHistory(ctx context.Context, db *gorm.DB, messageTable, c
values := messageValues(fields)
values["conversation_id"], values["position"] = conversationID, position
// A reserved answer slot cannot overwrite a later question.
- if err := db.WithContext(ctx).Table(messageTable).Create(values).Error; err != nil {
+ if err := db.WithContext(ctx).Model(historyModel(messageTable)).Create(values).Error; err != nil {
return err
}
}
@@ -301,7 +330,7 @@ func createHistory(ctx context.Context, db *gorm.DB, table, kind, conversationID
if len(rows) == 0 {
return nil
}
- return db.WithContext(ctx).Table(table).CreateInBatches(rows, 100).Error
+ return db.WithContext(ctx).Model(historyModel(table)).CreateInBatches(rows, 100).Error
}
// historyRows flattens a message or reference payload into child rows. Its
diff --git a/internal/dao/conversation_history_migration.go b/internal/dao/conversation_history_migration.go
index 417d040196..4a4a32e45e 100644
--- a/internal/dao/conversation_history_migration.go
+++ b/internal/dao/conversation_history_migration.go
@@ -205,7 +205,7 @@ func splitConversationHistory(ctx context.Context, db *gorm.DB, split conversati
return false, fmt.Errorf("split %s %s message payload: %w", split.parentTable, row.ID, err)
}
if len(rows) > 0 {
- if err := db.WithContext(ctx).Table(split.messageTable).CreateInBatches(rows, 100).Error; err != nil {
+ if err := db.WithContext(ctx).Model(historyModel(split.messageTable)).CreateInBatches(rows, 100).Error; err != nil {
return false, err
}
written = true
@@ -217,7 +217,7 @@ func splitConversationHistory(ctx context.Context, db *gorm.DB, split conversati
return false, fmt.Errorf("split %s %s reference payload: %w", split.parentTable, row.ID, err)
}
if len(rows) > 0 {
- if err := db.WithContext(ctx).Table(split.referenceTable).CreateInBatches(rows, 100).Error; err != nil {
+ if err := db.WithContext(ctx).Model(historyModel(split.referenceTable)).CreateInBatches(rows, 100).Error; err != nil {
return false, err
}
written = true
diff --git a/internal/dao/database.go b/internal/dao/database.go
index 2a07533a44..7494099597 100644
--- a/internal/dao/database.go
+++ b/internal/dao/database.go
@@ -18,6 +18,7 @@ package dao
import (
"context"
+ "errors"
"fmt"
"ragflow/internal/common"
"ragflow/internal/entity"
@@ -26,11 +27,19 @@ import (
"time"
"ragflow/internal/server"
+ "ragflow/internal/server/config"
"go.uber.org/zap"
+
+ // The vastbase metadata connection speaks lib/pq (DriverName "postgres"),
+ // matching the document engine's client; the import registers the
+ // database/sql driver regardless of which engines the binary links in,
+ // and autoMigrateSafely classifies its error type.
+ "github.com/lib/pq"
gormLogger "gorm.io/gorm/logger"
"gorm.io/driver/mysql"
+ "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/migrator"
"gorm.io/gorm/schema"
@@ -109,53 +118,132 @@ func (m namedIndexMigrator) MigrateColumnUnique(dst interface{}, field *schema.F
return m.Migrator.MigrateColumnUnique(dst, field, columnType)
}
-// InitDB initialize database connection
-func InitDB(ctx context.Context, migrateDB bool) error {
- globalConfig := server.GetConfig()
- databaseConfig := globalConfig.GetMySQLConfig()
+// vastbaseDialector hands out a vastbaseMigrator instead of the stock one.
+// B mode reports the named unique indexes the manual migrations create as
+// single-column UNIQUE constraints, so the stock migrator derives the
+// matching uni_<table>_<column> name and drops a constraint no code path
+// ever created; the PostgreSQL wire answers 42704 and aborts --migrate on
+// the first affected model (user.email, then ingestion_task.document_id).
+type vastbaseDialector struct {
+ *postgres.Dialector
+}
- dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
- databaseConfig.User,
- databaseConfig.Password,
- databaseConfig.Host,
- databaseConfig.Port,
- databaseConfig.DatabaseName,
- databaseConfig.Charset,
- )
+// The capabilities gorm discovers by type assertion on db.Dialector.
+var (
+ _ gorm.Dialector = vastbaseDialector{}
+ _ gorm.ErrorTranslator = vastbaseDialector{}
+ _ gorm.SavePointerDialectorInterface = vastbaseDialector{}
+)
- // Set log level
- var gormLogLevel gormLogger.LogLevel
- if globalConfig.GetMode() == "debug" {
- gormLogLevel = gormLogger.Info
- } else {
- gormLogLevel = gormLogger.Silent
+func (d vastbaseDialector) Migrator(db *gorm.DB) gorm.Migrator {
+ migrator, ok := d.Dialector.Migrator(db).(postgres.Migrator)
+ if !ok {
+ // An unsupported driver shape degrades to the stock behaviour rather
+ // than panicking, mirroring newMigrationAwareDialector.
+ return d.Dialector.Migrator(db)
}
+ return vastbaseMigrator{Migrator: migrator}
+}
- // Connect to database
- var err error
- DB, err = gorm.Open(newMigrationAwareDialector(dsn), &gorm.Config{
- Logger: gormLogger.Default.LogMode(gormLogLevel),
- NowFunc: func() time.Time {
- return time.Now().Local()
- },
- TranslateError: true,
+// vastbaseMigrator is the postgres counterpart of namedIndexMigrator: the
+// same phantom unique-constraint drop, guarded by the same predicate.
+type vastbaseMigrator struct {
+ postgres.Migrator
+}
+
+var _ migrator.BuildIndexOptionsInterface = vastbaseMigrator{}
+
+func (m vastbaseMigrator) MigrateColumnUnique(dst interface{}, field *schema.Field, columnType gorm.ColumnType) error {
+ if phantomUniqueDrop(field, columnType) {
+ return nil
+ }
+ return m.Migrator.MigrateColumnUnique(dst, field, columnType)
+}
+
+// isVastbaseMetaDB reports whether db runs on the vastbase dialector. Guarding
+// by the wrapper type rather than Dialector.Name() keeps a future genuine
+// PostgreSQL dialector out of the skip: instead of silently skipping schema
+// alignment, it fails loudly at the MySQL-flavored catalog probes.
+func isVastbaseMetaDB(db *gorm.DB) bool {
+ _, ok := db.Dialector.(vastbaseDialector)
+ return ok
+}
+
+// newVastbaseDialector builds the metadata-database dialector. lib/pq is the
+// driver (DriverName "postgres"), matching the document engine's connection to
+// the same instance; WithoutReturning is the peewee returning_clause=False
+// escape hatch, off by default because plain RETURNING is how GORM reads back
+// auto-increment ids.
+//
+// The metadata database must run in B (MySQL) compatibility mode — created
+// that way by docker/vastbase/init/01_init.sh, with the catalog emulation
+// enabled by 02_bcompat.sh (lower_case_table_names=1): without it unquoted
+// identifiers keep their written case and the uppercase INFORMATION_SCHEMA
+// views the migrations probe do not resolve. B mode parses MySQL syntax over
+// the PostgreSQL wire protocol, so hand-written DML, DDL and catalog SQL all
+// stay verbatim MySQL; no dialect branching is needed.
+func newVastbaseDialector(cfg config.VastbaseMetaConfig) gorm.Dialector {
+ base := postgres.New(postgres.Config{
+ DriverName: "postgres",
+ DSN: buildVastbaseMetaDSN(cfg),
+ WithoutReturning: cfg.DisableReturning,
})
- if err != nil {
- return fmt.Errorf("failed to connect database: %w", err)
+ if dialector, ok := base.(*postgres.Dialector); ok {
+ return vastbaseDialector{Dialector: dialector}
}
+ return base
+}
- // Get general database object sql.DB
- sqlDB, err := DB.DB()
- if err != nil {
- return fmt.Errorf("failed to get database instance: %w", err)
+// isLocalMetaDBHost reports whether the metadata host is loopback or a
+// unix-socket path — the only addresses allowed to default to plaintext.
+func isLocalMetaDBHost(host string) bool {
+ switch strings.ToLower(strings.TrimSpace(host)) {
+ case "localhost", "127.0.0.1", "::1", "[::1]":
+ return true
}
+ return strings.HasPrefix(host, "/")
+}
- // Set connection pool
- sqlDB.SetMaxIdleConns(databaseConfig.MaxConnections)
- sqlDB.SetMaxOpenConns(databaseConfig.MaxConnections)
- sqlDB.SetConnMaxLifetime(time.Duration(databaseConfig.StaleTimeout) * time.Second)
+// quoteMetaDSNValue wraps a lib/pq keyword-DSN value in single quotes so
+// hosts/users/passwords with special characters cannot break the DSN.
+func quoteMetaDSNValue(v string) string {
+ r := strings.ReplaceAll(v, `\`, `\\`)
+ r = strings.ReplaceAll(r, `'`, `\'`)
+ return "'" + r + "'"
+}
- // Auto migrate all dataModels
+// buildVastbaseMetaDSN builds the lib/pq keyword DSN for the Vastbase metadata
+// database, mirroring the document engine's DSN contract: quoted values, an
+// empty ssl_mode resolved by host (disable on loopback, verify-full on every
+// other TCP host) and the same connect/statement timeouts. lib/pq forwards
+// unrecognized keywords as startup parameters, which is how statement_timeout
+// reaches the server.
+func buildVastbaseMetaDSN(cfg config.VastbaseMetaConfig) string {
+ sslMode := cfg.SSLMode
+ if sslMode == "" {
+ if isLocalMetaDBHost(cfg.Host) {
+ sslMode = "disable"
+ } else {
+ sslMode = "verify-full"
+ }
+ }
+ parts := []string{
+ fmt.Sprintf("host=%s", quoteMetaDSNValue(cfg.Host)),
+ fmt.Sprintf("port=%d", cfg.Port),
+ fmt.Sprintf("user=%s", quoteMetaDSNValue(cfg.User)),
+ fmt.Sprintf("dbname=%s", quoteMetaDSNValue(cfg.DatabaseName)),
+ fmt.Sprintf("sslmode=%s", quoteMetaDSNValue(sslMode)),
+ }
+ if cfg.Password != "" {
+ parts = append(parts, fmt.Sprintf("password=%s", quoteMetaDSNValue(cfg.Password)))
+ }
+ parts = append(parts, "connect_timeout=30", "statement_timeout=300000")
+ return strings.Join(parts, " ")
+}
+
+// autoMigrateModels lists every entity AutoMigrate converges, as one source
+// for InitDB and the dialect shape tests.
+func autoMigrateModels() []interface{} {
dataModels := []interface{}{
&entity.User{},
&entity.Tenant{},
@@ -216,6 +304,72 @@ func InitDB(ctx context.Context, migrateDB bool) error {
}
dataModels = append(dataModels, permissionDataModelsEE()...)
dataModels = append(dataModels, billingDataModelsEE()...)
+ return dataModels
+}
+
+// InitDB initialize database connection
+func InitDB(ctx context.Context, migrateDB bool) error {
+ globalConfig := server.GetConfig()
+
+ // The metadata database dialect: mysql/oceanbase share the MySQL wire
+ // path, vastbase speaks PostgreSQL.
+ var dialector gorm.Dialector
+ var maxConnections, staleTimeout int
+ switch globalConfig.DatabaseType() {
+ case "vastbase":
+ vastbaseConfig := globalConfig.GetVastbaseMetaConfig()
+ dialector = newVastbaseDialector(vastbaseConfig)
+ maxConnections = vastbaseConfig.MaxConnections
+ staleTimeout = vastbaseConfig.StaleTimeout
+ default:
+ databaseConfig := globalConfig.GetMySQLConfig()
+ dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
+ databaseConfig.User,
+ databaseConfig.Password,
+ databaseConfig.Host,
+ databaseConfig.Port,
+ databaseConfig.DatabaseName,
+ databaseConfig.Charset,
+ )
+ dialector = newMigrationAwareDialector(dsn)
+ maxConnections = databaseConfig.MaxConnections
+ staleTimeout = databaseConfig.StaleTimeout
+ }
+
+ // Set log level
+ var gormLogLevel gormLogger.LogLevel
+ if globalConfig.GetMode() == "debug" {
+ gormLogLevel = gormLogger.Info
+ } else {
+ gormLogLevel = gormLogger.Silent
+ }
+
+ // Connect to database
+ var err error
+ DB, err = gorm.Open(dialector, &gorm.Config{
+ Logger: gormLogger.Default.LogMode(gormLogLevel),
+ NowFunc: func() time.Time {
+ return time.Now().Local()
+ },
+ TranslateError: true,
+ })
+ if err != nil {
+ return fmt.Errorf("failed to connect database: %w", err)
+ }
+
+ // Get general database object sql.DB
+ sqlDB, err := DB.DB()
+ if err != nil {
+ return fmt.Errorf("failed to get database instance: %w", err)
+ }
+
+ // Set connection pool
+ sqlDB.SetMaxIdleConns(maxConnections)
+ sqlDB.SetMaxOpenConns(maxConnections)
+ sqlDB.SetConnMaxLifetime(time.Duration(staleTimeout) * time.Second)
+
+ // Auto migrate all dataModels
+ dataModels := autoMigrateModels()
if migrateDB {
// Mirror the Python flow, where tools/scripts/run_migrations.sh runs before
@@ -381,9 +535,41 @@ func autoMigrateSafely(ctx context.Context, db *gorm.DB, model interface{}) erro
return nil
}
+ // PostgreSQL/Vastbase: the same converging conditions surface as lib/pq
+ // errors carrying SQLSTATE 42P07 (relation), 42701 (column) and 42710
+ // (constraint/object). Matching the typed SQLSTATE rather than an
+ // "already exists" text fragment keeps unrelated failures that merely
+ // phrase themselves that way from being read as convergence. Verifying
+ // that the named object belongs to the model being migrated is not
+ // feasible: a re-created index reports the index name, not its table. A
+ // blanket "does not exist" match is deliberately absent: it would also
+ // hide a bad type name, which is a schema bug, not convergence.
+ if isPostgresDuplicateObjectErr(err) {
+ common.Warn("Schema object already exists, skipping", zap.String("error", errStr))
+ return nil
+ }
+
return err
}
+// isPostgresDuplicateObjectErr reports whether err is one of the
+// PostgreSQL-wire SQLSTATEs that mean "the schema object AutoMigrate tried
+// to create already exists": 42P07 (relation), 42701 (column) and 42710
+// (constraint/object). The metadata connection speaks lib/pq, and gorm's
+// postgres error translator maps only 23505/23503/42703, so these driver
+// errors reach this check intact.
+func isPostgresDuplicateObjectErr(err error) bool {
+ var pqErr *pq.Error
+ if !errors.As(err, &pqErr) {
+ return false
+ }
+ switch pqErr.Code {
+ case "42P07", "42701", "42710":
+ return true
+ }
+ return false
+}
+
// autoMigrateRuntimeModels ensures the Go-exclusive runtime tables exist. The
// manual migrations run as the standalone --migrate action, so a server-mode
// process never runs them itself.
diff --git a/internal/dao/database_test.go b/internal/dao/database_test.go
index b3cdae4748..c02dadf617 100644
--- a/internal/dao/database_test.go
+++ b/internal/dao/database_test.go
@@ -19,6 +19,7 @@ package dao
import (
"context"
"errors"
+ "fmt"
"reflect"
"strings"
"testing"
@@ -27,7 +28,9 @@ import (
"ragflow/internal/entity"
"github.com/glebarez/sqlite"
+ "github.com/lib/pq"
"gorm.io/driver/mysql"
+ "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
@@ -482,6 +485,33 @@ func TestNamedIndexMigratorSkipsPhantomUniqueDrop(t *testing.T) {
}
}
+// TestIsPostgresDuplicateObjectErr pins the typed classification the
+// Vastbase convergence skip relies on: only the three duplicate-object
+// SQLSTATEs count as convergence, and errors that merely contain "already
+// exists" in their text — a non-pq error, or a pq error with another code —
+// must surface instead of being suppressed.
+func TestIsPostgresDuplicateObjectErr(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ err error
+ want bool
+ }{
+ {"relation exists", &pq.Error{Code: "42P07", Message: `relation "user" already exists`}, true},
+ {"column exists", &pq.Error{Code: "42701", Message: `column "foo" of relation "user" already exists`}, true},
+ {"object exists", &pq.Error{Code: "42710", Message: `constraint "c" for relation "user" already exists`}, true},
+ {"wrapped relation exists", fmt.Errorf("auto migrate: %w", &pq.Error{Code: "42P07"}), true},
+ {"unique violation", &pq.Error{Code: "23505", Message: "duplicate key value violates unique constraint", Detail: "Key (id)=(1) already exists."}, false},
+ {"text only", errors.New(`relation "user" already exists`), false},
+ {"other pq error", &pq.Error{Code: "42602", Message: "invalid name"}, false},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := isPostgresDuplicateObjectErr(tc.err); got != tc.want {
+ t.Fatalf("isPostgresDuplicateObjectErr(%v) = %v, want %v", tc.err, got, tc.want)
+ }
+ })
+ }
+}
+
// TestWrappersExposeEverythingWrappedTypeDoes is the guard that made the
// concrete embedding necessary: embedding gorm's interfaces instead would drop
// the twenty migrator methods and the three dialector methods that gorm reaches
@@ -497,6 +527,8 @@ func TestWrappersExposeEverythingWrappedTypeDoes(t *testing.T) {
}{
{"dialector", reflect.TypeOf(&mysql.Dialector{}), reflect.TypeOf(migrationAwareDialector{}), "Migrator"},
{"migrator", reflect.TypeOf(mysql.Migrator{}), reflect.TypeOf(namedIndexMigrator{}), "MigrateColumnUnique"},
+ {"dialector", reflect.TypeOf(&postgres.Dialector{}), reflect.TypeOf(vastbaseDialector{}), "Migrator"},
+ {"migrator", reflect.TypeOf(postgres.Migrator{}), reflect.TypeOf(vastbaseMigrator{}), "MigrateColumnUnique"},
} {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i < tc.wrapped.NumMethod(); i++ {
diff --git a/internal/dao/migration.go b/internal/dao/migration.go
index 59c274273b..52c87122c1 100644
--- a/internal/dao/migration.go
+++ b/internal/dao/migration.go
@@ -489,12 +489,17 @@ func createIngestionLogIndexIfMissing(migrator ingestionLogSchemaMigrator, model
return nil
}
+// isDuplicateColumnError reports whether err means the column is already
+// there: MySQL's Error 1060 on the MySQL wire, or the duplicate-object
+// SQLSTATEs on the Vastbase connection. A bare "already exists" text match is
+// deliberately absent — unrelated failures phrase themselves that way too.
func isDuplicateColumnError(err error) bool {
if err == nil {
return false
}
text := err.Error()
- return strings.Contains(text, "Error 1060") && strings.Contains(text, "Duplicate column name")
+ return (strings.Contains(text, "Error 1060") && strings.Contains(text, "Duplicate column name")) ||
+ isPostgresDuplicateObjectErr(err)
}
// migrateKnowledgebaseNameUnique adds a case-insensitive unique constraint on
@@ -732,6 +737,18 @@ func dropRemovedParserConfigKeys(config map[string]interface{}) bool {
// entities, so AutoMigrate converges on the same definition and the two paths
// cannot fight over the column.
func modifyColumnTypes(ctx context.Context, db *gorm.DB) error {
+ // The specs carry MySQL type names (int, longtext, datetime) compared
+ // against COLUMN_TYPE to keep the shared schema aligned with the Python
+ // models. On Vastbase the columns are created by AutoMigrate's postgres
+ // mapping, whose reported type names (integer, bigint, timestamp) never
+ // compare equal to the specs, so every startup would re-issue the MODIFY
+ // and AutoMigrate would flip the column back — an ALTER loop measured on
+ // a live instance (spec int NOT NULL vs stored bigint YES). The columns
+ // are already correct there; there is nothing to align.
+ if isVastbaseMetaDB(db) {
+ return nil
+ }
+
specs := []columnSpec{
// dialog.top_k mirrors Python's IntegerField(default=1024).
{
diff --git a/internal/dao/migration_test.go b/internal/dao/migration_test.go
index 2fa2f6a767..7f48002a02 100644
--- a/internal/dao/migration_test.go
+++ b/internal/dao/migration_test.go
@@ -18,11 +18,16 @@ package dao
import (
"context"
+ "errors"
+ "fmt"
"strings"
"sync"
"testing"
+ "github.com/DATA-DOG/go-sqlmock"
"github.com/glebarez/sqlite"
+ "github.com/lib/pq"
+ "gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"ragflow/internal/entity"
@@ -116,6 +121,59 @@ func TestTenantModelIDColumnSpecsMirrorsBackfillFields(t *testing.T) {
}
}
+// TestIsDuplicateColumnErrorClassification pins the convergence criteria for
+// "the column is already there": MySQL's Error 1060 pairing on the MySQL wire
+// and the typed duplicate-object SQLSTATEs on the Vastbase connection. A bare
+// "already exists" text match is intentionally out — unrelated failures
+// phrase themselves that way.
+func TestIsDuplicateColumnErrorClassification(t *testing.T) {
+ cases := []struct {
+ name string
+ err error
+ want bool
+ }{
+ {"nil", nil, false},
+ {"mysql duplicate column", errors.New("Error 1060: Duplicate column name 'pipeline_log_id'"), true},
+ {"mysql other error", errors.New("Error 1054: Unknown column 'x' in 'where clause'"), false},
+ {"pq duplicate column", &pq.Error{Code: "42701", Message: `column "x" of relation "t" already exists`}, true},
+ {"pq duplicate relation", &pq.Error{Code: "42P07", Message: `relation "uk_tenant_llm" already exists`}, true},
+ {"pq other sqlstate", &pq.Error{Code: "42602", Message: "invalid name syntax"}, false},
+ {"wrapped pq duplicate", fmt.Errorf("alter table: %w", &pq.Error{Code: "42701", Message: `column "x" already exists`}), true},
+ {"plain already-exists text is not convergence", errors.New("the snapshot already exists"), false},
+ }
+ for _, tc := range cases {
+ if got := isDuplicateColumnError(tc.err); got != tc.want {
+ t.Errorf("%s: isDuplicateColumnError = %v, want %v", tc.name, got, tc.want)
+ }
+ }
+}
+
+// TestModifyColumnTypesSkipsVastbase pins the vastbase bail-out. The specs
+// carry MySQL type names that the B-mode catalog never reports back after a
+// MODIFY (int comes back as integer), so on the Vastbase connection the step
+// would re-issue the ALTER on every startup while AutoMigrate flips the
+// column back — the loop measured on a live instance. The step must return
+// without touching the database at all. The dialector is the vastbase
+// wrapper: the guard targets that type, so a plain postgres dialector stays
+// on the aligning path.
+func TestModifyColumnTypesSkipsVastbase(t *testing.T) {
+ mockDB, mock, err := sqlmock.New()
+ if err != nil {
+ t.Fatalf("sqlmock: %v", err)
+ }
+ defer mockDB.Close()
+ db, err := gorm.Open(vastbaseDialector{Dialector: &postgres.Dialector{Config: &postgres.Config{Conn: mockDB}}}, &gorm.Config{})
+ if err != nil {
+ t.Fatalf("open: %v", err)
+ }
+ if err := modifyColumnTypes(context.Background(), db); err != nil {
+ t.Fatalf("modifyColumnTypes on vastbase: %v", err)
+ }
+ if err := mock.ExpectationsWereMet(); err != nil {
+ t.Fatalf("modifyColumnTypes must not query the vastbase catalog: %v", err)
+ }
+}
+
// TestDiscardOrphanedColumnValuesResetsOnlyOrphans covers the values a widened
// int column carries into its new life as a hex-id column. "3" is what matters:
// it looks like a model id to every reader, so leaving it behind turns a visible
diff --git a/internal/dao/vastbase_meta_integration_test.go b/internal/dao/vastbase_meta_integration_test.go
new file mode 100644
index 0000000000..234124dd3f
--- /dev/null
+++ b/internal/dao/vastbase_meta_integration_test.go
@@ -0,0 +1,403 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+//go:build integration
+
+package dao
+
+import (
+ "context"
+ "errors"
+ "os"
+ "strconv"
+ "testing"
+
+ "ragflow/internal/entity"
+ "ragflow/internal/server/config"
+
+ "github.com/lib/pq"
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+)
+
+func ptr[T any](v T) *T { return &v }
+
+// vastbaseIntegrationConfig reads the target instance from the environment:
+// RAGFLOW_TEST_VASTBASE_META_HOST/PORT/USER/PASSWORD/NAME/SSL_MODE (defaults
+// match the bundled docker deployment). Like the engine-specific suites
+// (ES/OceanBase), this suite self-skips unless HOST is set: the CI runners
+// do not provision a Vastbase instance. Run against the bundled deployment
+// with:
+//
+// RAGFLOW_TEST_VASTBASE_META_HOST=127.0.0.1 \
+// go test -tags integration ./internal/dao/ -run TestVastbaseMeta -v
+func vastbaseIntegrationConfig(t *testing.T) config.VastbaseMetaConfig {
+ t.Helper()
+ const envPrefix = "RAGFLOW_TEST_VASTBASE_META_"
+ get := func(key, fallback string) string {
+ if v := os.Getenv(envPrefix + key); v != "" {
+ return v
+ }
+ return fallback
+ }
+ host := get("HOST", "")
+ if host == "" {
+ t.Skip("RAGFLOW_TEST_VASTBASE_META_HOST is not set: this suite needs a live Vastbase G100 meta instance")
+ }
+ port, err := strconv.Atoi(get("PORT", "5432"))
+ if err != nil {
+ t.Fatalf("invalid RAGFLOW_TEST_VASTBASE_META_PORT: %v", err)
+ }
+ cfg := config.VastbaseMetaConfig{
+ Host: host,
+ Port: port,
+ User: get("USER", "ragflow"),
+ Password: get("PASSWORD", "Infini_Rag@123"),
+ DatabaseName: get("NAME", "rag_flow"),
+ SSLMode: get("SSL_MODE", "disable"),
+ }
+ return cfg
+}
+
+func openVastbaseIntegrationDB(t *testing.T) *gorm.DB {
+ t.Helper()
+ db, err := gorm.Open(newVastbaseDialector(vastbaseIntegrationConfig(t)), &gorm.Config{
+ TranslateError: true,
+ })
+ if err != nil {
+ t.Fatalf("open vastbase: %v", err)
+ }
+ t.Cleanup(func() {
+ if sqlDB, err := db.DB(); err == nil {
+ sqlDB.Close()
+ }
+ })
+ return db
+}
+
+// TestVastbaseMetaAutoMigrateAllModels runs the full AutoMigrate model set
+// against a real Vastbase: every CREATE TABLE and index statement must be
+// accepted, and a second run must converge without ALTERs failing.
+func TestVastbaseMetaAutoMigrateAllModels(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ ctx := context.Background()
+ for _, model := range autoMigrateModels() {
+ if err := autoMigrateSafely(ctx, db, model); err != nil {
+ t.Fatalf("autoMigrate %T: %v", model, err)
+ }
+ }
+ // Convergence pass: remapped types must match what was created.
+ for _, model := range autoMigrateModels() {
+ if err := autoMigrateSafely(ctx, db, model); err != nil {
+ t.Fatalf("converge %T: %v", model, err)
+ }
+ }
+}
+
+// TestVastbaseMetaTenantLLMAutoIncrementRoundtrip checks the INSERT ...
+// RETURNING path on the one auto-increment business model: the id must be
+// read back without ON CONFLICT being involved.
+func TestVastbaseMetaTenantLLMAutoIncrementRoundtrip(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ modelType := "chat"
+ llmName := "integration-model"
+ row := &entity.TenantLLM{
+ TenantID: "integration-tenant",
+ LLMFactory: "openai-api-compatible",
+ ModelType: &modelType,
+ APIKey: ptr("integration"),
+ APIBase: ptr("https://example.invalid/v1"),
+ LLMName: &llmName,
+ }
+ if err := db.Create(row).Error; err != nil {
+ t.Fatalf("create tenant_llm: %v", err)
+ }
+ if row.ID == 0 {
+ t.Fatal("auto-increment id was not read back via RETURNING")
+ }
+ defer db.Delete(&entity.TenantLLM{}, row.ID)
+
+ var stored entity.TenantLLM
+ if err := db.Where("tenant_id = ? AND llm_factory = ? AND llm_name = ?",
+ row.TenantID, row.LLMFactory, llmName).First(&stored).Error; err != nil {
+ t.Fatalf("read back tenant_llm: %v", err)
+ }
+ if stored.ID != row.ID {
+ t.Fatalf("read-back id = %d, want %d", stored.ID, row.ID)
+ }
+}
+
+// TestVastbaseMetaSystemSettingsUpsert checks the ON CONFLICT DO UPDATE path
+// without RETURNING — the combination Vastbase B mode rejects when a default-
+// valued field drags RETURNING in. Mirrors setDatabaseMigrationVersion's shape.
+func TestVastbaseMetaSystemSettingsUpsert(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ ctx := context.Background()
+ upsert := func(value string) error {
+ setting := entity.SystemSettings{
+ Name: "integration.marker", Source: "integration",
+ DataType: "string", Value: value,
+ }
+ return db.WithContext(ctx).Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "name"}},
+ DoUpdates: clause.AssignmentColumns([]string{"source", "data_type", "value", "update_time", "update_date"}),
+ }).Create(&setting).Error
+ }
+ defer db.Exec("DELETE FROM system_settings WHERE name = ?", "integration.marker")
+
+ if err := upsert("first"); err != nil {
+ t.Fatalf("initial upsert: %v", err)
+ }
+ if err := upsert("second"); err != nil {
+ t.Fatalf("conflicting upsert: %v", err)
+ }
+ var value string
+ if err := db.Raw("SELECT value FROM system_settings WHERE name = ?", "integration.marker").
+ Row().Scan(&value); err != nil {
+ t.Fatalf("read marker: %v", err)
+ }
+ if value != "second" {
+ t.Fatalf("marker value = %q, want second", value)
+ }
+}
+
+// TestVastbaseMetaDuplicateKeyTranslation checks the unique-index conflict
+// surfaces as gorm.ErrDuplicatedKey, which the service layer's "already
+// exists" mapping consumes.
+func TestVastbaseMetaDuplicateKeyTranslation(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ dup := &entity.SystemSettings{
+ Name: "integration.dup", Source: "integration", DataType: "string", Value: "v",
+ }
+ defer db.Exec("DELETE FROM system_settings WHERE name = ?", "integration.dup")
+ if err := db.Create(dup).Error; err != nil {
+ t.Fatalf("seed unique row: %v", err)
+ }
+ conflict := &entity.SystemSettings{
+ Name: "integration.dup", Source: "integration", DataType: "string", Value: "v2",
+ }
+ err := db.Create(conflict).Error
+ if !errors.Is(err, gorm.ErrDuplicatedKey) {
+ t.Fatalf("duplicate insert error = %v, want gorm.ErrDuplicatedKey", err)
+ }
+ if !IsDuplicateKeyErr(err) {
+ t.Fatalf("IsDuplicateKeyErr(%v) = false", err)
+ }
+}
+
+// TestVastbaseMetaDuplicateObjectSQLSTATE pins the wire form the
+// convergence skip in autoMigrateSafely matches: re-issuing DDL for an
+// existing table, column and index must surface as *pq.Error with the
+// duplicate-object SQLSTATEs (42P07/42701), not merely as text containing
+// "already exists". A Vastbase that wraps or recodes these errors would
+// break the typed classification and fail here.
+func TestVastbaseMetaDuplicateObjectSQLSTATE(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ const marker = "integration_duplicate_marker"
+ db.Exec("DROP TABLE IF EXISTS " + marker)
+ defer db.Exec("DROP TABLE IF EXISTS " + marker)
+
+ for _, ddl := range []string{
+ `CREATE TABLE ` + marker + ` (id varchar(32))`,
+ `ALTER TABLE ` + marker + ` ADD COLUMN value varchar(64)`,
+ `CREATE INDEX idx_integration_dup_marker ON ` + marker + ` (id)`,
+ } {
+ if err := db.Exec(ddl).Error; err != nil {
+ t.Fatalf("seed DDL: %s: %v", ddl, err)
+ }
+ err := db.Exec(ddl).Error
+ if err == nil {
+ t.Fatalf("duplicate DDL accepted: %s", ddl)
+ }
+ var pqErr *pq.Error
+ if !errors.As(err, &pqErr) {
+ t.Fatalf("duplicate DDL error is not *pq.Error: %T: %v", err, err)
+ }
+ if pqErr.Code != "42P07" && pqErr.Code != "42701" {
+ t.Fatalf("duplicate DDL SQLSTATE = %s, want 42P07 or 42701: %v", pqErr.Code, err)
+ }
+ if !isPostgresDuplicateObjectErr(err) {
+ t.Fatalf("duplicate DDL error not classified as convergence: %v", err)
+ }
+ }
+}
+
+// TestVastbaseMetaManualMigrationsConverge runs the manual migration set in
+// the order InitDB uses against a schema AutoMigrate already populated, then
+// runs it again: the second pass over existing tables is what every restart
+// executes on a live deployment. Every catalog probe here is the verbatim
+// MySQL SQL the migrations issue, so this also pins that the B-mode catalog
+// emulation (lower_case_table_names=1, docker/vastbase/init/02_bcompat.sh)
+// stays in place — without it these queries fail with
+// `relation "information_schema.COLUMNS" does not exist`.
+func TestVastbaseMetaManualMigrationsConverge(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ ctx := context.Background()
+
+ for _, model := range autoMigrateModels() {
+ if err := autoMigrateSafely(ctx, db, model); err != nil {
+ t.Fatalf("autoMigrate %T: %v", model, err)
+ }
+ }
+ if err := RunMigrations(ctx, db); err != nil {
+ t.Fatalf("migrations over populated schema: %v", err)
+ }
+ if err := RunMigrations(ctx, db); err != nil {
+ t.Fatalf("second migration pass: %v", err)
+ }
+
+ // Every unique contract must hold afterwards, probed the same way the
+ // migrations probe: through the MySQL catalog emulation.
+ uniqueContracts := []struct {
+ table string
+ columns []string
+ index string
+ }{
+ {"tenant_llm", []string{"tenant_id", "llm_factory", "llm_name"}, "uk_tenant_llm"},
+ {"user", []string{"email"}, "idx_user_email"},
+ {"user_canvas", []string{"user_id", "canvas_category", "title"}, "idx_user_canvas_user_category_title"},
+ {"ingestion_task", []string{"document_id"}, "idx_ingestion_task_document_id"},
+ }
+ for _, contract := range uniqueContracts {
+ hasUnique, err := hasUniqueIndex(ctx, db, contract.table, contract.columns)
+ if err != nil {
+ t.Fatalf("probe unique index on %s: %v", contract.table, err)
+ }
+ if !hasUnique {
+ t.Errorf("no unique index on %s(%v) after migrations", contract.table, contract.columns)
+ }
+ var idxExists int64
+ if err := db.WithContext(ctx).Raw(
+ `SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND INDEX_NAME = ?`,
+ contract.table, contract.index).Scan(&idxExists).Error; err != nil {
+ t.Fatalf("probe index %s: %v", contract.index, err)
+ }
+ if idxExists == 0 {
+ t.Errorf("index %s missing after migrations", contract.index)
+ }
+ }
+
+ // The knowledgebase name contract is the generated-column scheme: the
+ // name_ci column and its unique index must both be there.
+ var nameCiColumns int64
+ if err := db.WithContext(ctx).Raw(`SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
+ WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'knowledgebase' AND COLUMN_NAME = 'name_ci'`).
+ Scan(&nameCiColumns).Error; err != nil {
+ t.Fatalf("probe knowledgebase.name_ci: %v", err)
+ }
+ if nameCiColumns == 0 {
+ t.Error("knowledgebase.name_ci generated column missing after migrations")
+ }
+
+ // And the tenant_llm uniqueness must actually reject a duplicate row.
+ modelType := "chat"
+ llmName := "converge-model"
+ seed := &entity.TenantLLM{
+ TenantID: "integration-converge",
+ LLMFactory: "openai-api-compatible",
+ ModelType: &modelType,
+ APIKey: ptr("integration"),
+ APIBase: ptr("https://example.invalid/v1"),
+ LLMName: &llmName,
+ }
+ if err := db.Create(seed).Error; err != nil {
+ t.Fatalf("seed tenant_llm: %v", err)
+ }
+ defer db.Exec("DELETE FROM tenant_llm WHERE tenant_id = ?", seed.TenantID)
+ duplicate := *seed
+ duplicate.ID = 0
+ if err := db.Create(&duplicate).Error; !errors.Is(err, gorm.ErrDuplicatedKey) {
+ t.Fatalf("duplicate tenant_llm error = %v, want gorm.ErrDuplicatedKey", err)
+ }
+}
+
+// TestVastbaseMetaBModeMySQLSyntax proves the metadata database really runs
+// in B (MySQL) compatibility mode. The DAO layer's hand-written SQL stays
+// verbatim MySQL — unquoted references to the user table (reserved in
+// vanilla PostgreSQL), DATE_FORMAT, the REGEXP operator, DELETE ... ORDER
+// BY ... LIMIT — and relies on B mode to parse it over the PostgreSQL wire
+// protocol. A failure here usually means the database was not created with
+// DBCOMPATIBILITY = 'B' (docker/vastbase/init/01_init.sh does that).
+func TestVastbaseMetaBModeMySQLSyntax(t *testing.T) {
+ db := openVastbaseIntegrationDB(t)
+ ctx := context.Background()
+ for _, model := range []any{&entity.User{}, &entity.UserTenant{}, &entity.UserCanvas{}, &entity.TimeRecord{}} {
+ if err := autoMigrateSafely(ctx, db, model); err != nil {
+ t.Fatalf("autoMigrate %T: %v", model, err)
+ }
+ }
+
+ const userID = "vbmodeuser0000000000000000000000" // 32 chars, User.id size
+ const tenantID = "vbmodetenant00000000000000000000" // 32 chars
+ defer func() {
+ db.Exec("DELETE FROM user_tenant WHERE user_id = ?", userID)
+ db.Exec("DELETE FROM user WHERE id = ?", userID)
+ }()
+
+ // Unquoted user-table JOIN plus DATE_FORMAT, via the real DAO query
+ // (UserTenantDAO.GetMembersByTenantID).
+ if err := db.Create(&entity.User{
+ ID: userID, Nickname: "vb-mode", Email: "[email protected]",
+ IsAuthenticated: "1", IsActive: "1", IsAnonymous: "0",
+ Status: ptr("1"), IsSuperuser: ptr(false),
+ }).Error; err != nil {
+ t.Fatalf("seed user: %v", err)
+ }
+ if err := db.Create(&entity.UserTenant{
+ ID: "vbmodeut000000000000000000000000", UserID: userID, TenantID: tenantID,
+ Role: "member", InvitedBy: userID, Status: ptr("1"),
+ }).Error; err != nil {
+ t.Fatalf("seed user_tenant: %v", err)
+ }
+ members, err := NewUserTenantDAO().GetMembersByTenantID(ctx, db, tenantID)
+ if err != nil {
+ t.Fatalf("GetMembersByTenantID (user join + DATE_FORMAT): %v", err)
+ }
+ if len(members) != 1 || members[0].Nickname != "vb-mode" {
+ t.Fatalf("members = %+v, want one vb-mode row", members)
+ }
+
+ // REGEXP operator (applyUserCanvasTagFilter's predicate shape).
+ var matches int64
+ if err := db.Raw(
+ "SELECT COUNT(*) FROM user_canvas WHERE user_canvas.tags REGEXP ?",
+ "(^|,)[[:space:]]*go[[:space:]]*(,|$)",
+ ).Scan(&matches).Error; err != nil {
+ t.Fatalf("REGEXP predicate: %v", err)
+ }
+
+ // DELETE ... ORDER BY ... LIMIT (TimeRecordDAO.DeleteOldest) — and the
+ // LIMIT must actually bound the deletion, not just parse.
+ for i := 0; i < 2; i++ {
+ if err := db.Create(&entity.TimeRecord{Data: "vb-mode"}).Error; err != nil {
+ t.Fatalf("seed time_record: %v", err)
+ }
+ }
+ defer db.Exec("DELETE FROM time_records WHERE data = ?", "vb-mode")
+ if err := NewTimeRecordDAO().DeleteOldest(ctx, db, 1); err != nil {
+ t.Fatalf("DeleteOldest (DELETE ... ORDER BY ... LIMIT): %v", err)
+ }
+ var remaining int64
+ if err := db.Raw(
+ "SELECT COUNT(*) FROM time_records WHERE data = ?", "vb-mode",
+ ).Scan(&remaining).Error; err != nil {
+ t.Fatalf("count time_records: %v", err)
+ }
+ if remaining != 1 {
+ t.Fatalf("remaining time_records = %d, want 1 (LIMIT must bound the delete)", remaining)
+ }
+}
diff --git a/internal/dao/vastbase_meta_test.go b/internal/dao/vastbase_meta_test.go
new file mode 100644
index 0000000000..b55481ce11
--- /dev/null
+++ b/internal/dao/vastbase_meta_test.go
@@ -0,0 +1,216 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package dao
+
+import (
+ "strings"
+ "testing"
+
+ "ragflow/internal/entity"
+ "ragflow/internal/server/config"
+
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+ "gorm.io/gorm/clause"
+ "gorm.io/gorm/logger"
+ "gorm.io/gorm/schema"
+)
+
+// openVastbaseMetaForSQL builds a gorm session speaking the vastbase dialect
+// without ever connecting: the postgres dialector defers sql.Open, and
+// DisableAutomaticPing keeps gorm.Open from touching the pool. DryRun then
+// exercises the SQL builders (ToSQL, DataTypeOf, the migrator) offline.
+func openVastbaseMetaForSQL(t *testing.T) *gorm.DB {
+ t.Helper()
+ db, err := gorm.Open(newVastbaseDialector(config.VastbaseMetaConfig{
+ DatabaseName: "rag_flow", Host: "vastbase", Port: 5432,
+ User: "ragflow", Password: "secret",
+ }), &gorm.Config{
+ DryRun: true,
+ DisableAutomaticPing: true,
+ Logger: logger.Default.LogMode(logger.Silent),
+ })
+ if err != nil {
+ t.Fatalf("open vastbase dialector: %v", err)
+ }
+ return db
+}
+
+// TestBuildVastbaseMetaDSN pins the DSN contract: quoted values, ssl resolved
+// by host when unset, explicit ssl preserved, password omitted when empty.
+func TestBuildVastbaseMetaDSN(t *testing.T) {
+ cfg := config.VastbaseMetaConfig{
+ DatabaseName: "rag_flow", Host: "vastbase", Port: 5432,
+ User: "ragflow", Password: "Infini_Rag@123",
+ }
+ got := buildVastbaseMetaDSN(cfg)
+ for _, want := range []string{
+ `host='vastbase'`, `port=5432`, `user='ragflow'`,
+ `dbname='rag_flow'`, `sslmode='verify-full'`,
+ `password='Infini_Rag@123'`, `connect_timeout=30`, `statement_timeout=300000`,
+ } {
+ if !strings.Contains(got, want) {
+ t.Errorf("remote-host DSN %q missing %q", got, want)
+ }
+ }
+
+ // Loopback hosts default to plaintext.
+ cfg.Host = "localhost"
+ if got := buildVastbaseMetaDSN(cfg); !strings.Contains(got, `sslmode='disable'`) {
+ t.Errorf("loopback DSN %q should default sslmode=disable", got)
+ }
+
+ // An explicit ssl_mode always wins.
+ cfg.SSLMode = "require"
+ if got := buildVastbaseMetaDSN(cfg); !strings.Contains(got, `sslmode='require'`) {
+ t.Errorf("explicit ssl DSN %q should keep sslmode=require", got)
+ }
+
+ // Values with quotes and backslashes stay inside the quoted keyword value.
+ cfg.Host = `va'st\host`
+ if got := buildVastbaseMetaDSN(cfg); !strings.Contains(got, `host='va\'st\\host'`) {
+ t.Errorf("special-character DSN %q should escape the host value", got)
+ }
+
+ // Empty password: the keyword is dropped rather than sending an empty one.
+ cfg.Host, cfg.Password = "vastbase", ""
+ if got := buildVastbaseMetaDSN(cfg); strings.Contains(got, "password") {
+ t.Errorf("empty-password DSN %q should omit the password keyword", got)
+ }
+}
+
+// TestVastbaseUpsertModelsHaveNoReturningFields is the guard for the B-mode
+// limitation Python worked around with returning_clause=False: Vastbase
+// rejects INSERT ... ON CONFLICT ... DO UPDATE combined with RETURNING. GORM
+// appends RETURNING exactly when the model's FieldsWithDefaultDBValue is
+// non-empty, so every upserted model must stay free of such fields — a new
+// field with a database-side default (including a blank one) would silently
+// break every vastbase upsert.
+func TestVastbaseUpsertModelsHaveNoReturningFields(t *testing.T) {
+ db := openVastbaseMetaForSQL(t)
+ for _, model := range []interface{}{
+ &entity.SystemSettings{}, // migration version marker upsert
+ &entity.TenantLangfuse{}, // SaveByTenantID upsert
+ &entity.CanvasTemplate{}, // template seeding upsert
+ } {
+ stmt := &gorm.Statement{DB: db, Dest: model}
+ if err := stmt.Parse(model); err != nil {
+ t.Fatalf("parse %T: %v", model, err)
+ }
+ for _, field := range stmt.Schema.FieldsWithDefaultDBValue {
+ t.Errorf("%s.%s has a database-side default, which makes the postgres dialect append RETURNING to the ON CONFLICT upsert", stmt.Schema.Table, field.DBName)
+ }
+ }
+}
+
+// TestVastbaseInsertSQLShape checks the generated upsert against PostgreSQL
+// grammar: double-quoted identifiers (never MySQL backticks), and no RETURNING
+// appended to the ON CONFLICT upsert — the combination Vastbase B mode
+// rejects.
+func TestVastbaseInsertSQLShape(t *testing.T) {
+ db := openVastbaseMetaForSQL(t)
+
+ setting := &entity.SystemSettings{Name: "marker", Value: "v0.27.2", DataType: "string", Source: "migration"}
+ sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
+ return tx.Clauses(clause.OnConflict{
+ Columns: []clause.Column{{Name: "name"}},
+ DoUpdates: clause.AssignmentColumns([]string{"value"}),
+ }).Create(setting)
+ })
+ if strings.Contains(sql, "`") {
+ t.Errorf("upsert SQL %q should not quote identifiers with backticks", sql)
+ }
+ if !strings.Contains(sql, `"system_settings"`) {
+ t.Errorf("upsert SQL %q should quote identifiers the PostgreSQL way", sql)
+ }
+ if strings.Contains(sql, "RETURNING") {
+ t.Errorf("upsert SQL %q combines ON CONFLICT with RETURNING, which Vastbase B mode rejects", sql)
+ }
+ if !strings.Contains(sql, "ON CONFLICT") {
+ t.Errorf("upsert SQL %q lost the ON CONFLICT clause", sql)
+ }
+}
+
+// TestVastbaseWithoutReturningEscapeHatch pins the config escape hatch: with
+// disable_returning set, no statement carries RETURNING — mirroring peewee's
+// returning_clause=False for Vastbase builds that reject it outright.
+func TestVastbaseWithoutReturningEscapeHatch(t *testing.T) {
+ db, err := gorm.Open(newVastbaseDialector(config.VastbaseMetaConfig{
+ DatabaseName: "rag_flow", Host: "vastbase", Port: 5432,
+ User: "ragflow", Password: "secret", DisableReturning: true,
+ }), &gorm.Config{
+ DryRun: true,
+ DisableAutomaticPing: true,
+ Logger: logger.Default.LogMode(logger.Silent),
+ })
+ if err != nil {
+ t.Fatalf("open vastbase dialector: %v", err)
+ }
+ sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB {
+ return tx.Create(&entity.SystemSettings{Name: "marker", Value: "v", DataType: "string", Source: "migration"})
+ })
+ if strings.Contains(sql, "RETURNING") {
+ t.Errorf("plain insert SQL %q should omit RETURNING under disable_returning", sql)
+ }
+}
+
+// TestVastbaseDialectorHandsOutGuardedMigrator pins the wiring the B-mode
+// phantom drop rides on: the dialector must hand out the migrator that skips
+// dropping uni_<table>_<column> constraints no code path created. The stock
+// postgres migrator aborts --migrate with 42704 on the first model whose
+// single-column unique index a manual migration has just created.
+func TestVastbaseDialectorHandsOutGuardedMigrator(t *testing.T) {
+ db := openVastbaseMetaForSQL(t)
+ if _, ok := db.Migrator().(vastbaseMigrator); !ok {
+ t.Fatalf("vastbase migrator = %T, want vastbaseMigrator", db.Migrator())
+ }
+}
+
+// TestVastbaseMigratorSkipsPhantomUniqueDrop pins the guard itself: a column
+// the database reports unique while the field carries no unique tag must not
+// reach the stock drop. The zero embed is deliberate — any delegation panics,
+// so removing the guard fails this test loudly instead of silently dropping.
+func TestVastbaseMigratorSkipsPhantomUniqueDrop(t *testing.T) {
+ field := &schema.Field{} // no unique tag, like entity.User.Email
+ if err := (vastbaseMigrator{}).MigrateColumnUnique(&entity.User{}, field, uniqueColumnType{unique: true}); err != nil {
+ t.Fatalf("phantom unique drop was not skipped: %v", err)
+ }
+}
+
+// TestIsVastbaseMetaDBTargetsTheWrapperType pins the precision of the skip
+// guard: it matches the vastbase dialector by type, so a future genuine
+// PostgreSQL connection keeps running the MySQL-flavored schema alignment
+// and fails loudly at its catalog probes instead of being silently skipped.
+func TestIsVastbaseMetaDBTargetsTheWrapperType(t *testing.T) {
+ if !isVastbaseMetaDB(openVastbaseMetaForSQL(t)) {
+ t.Fatal("vastbase dialector not recognized")
+ }
+
+ plain, err := gorm.Open(postgres.New(postgres.Config{
+ DSN: "host=localhost user=ragflow dbname=rag_flow",
+ }), &gorm.Config{
+ DryRun: true,
+ DisableAutomaticPing: true,
+ Logger: logger.Default.LogMode(logger.Silent),
+ })
+ if err != nil {
+ t.Fatalf("open plain postgres dialector: %v", err)
+ }
+ if isVastbaseMetaDB(plain) {
+ t.Fatal("a plain postgres dialector must not match the vastbase skip")
+ }
+}
diff --git a/internal/server/config.go b/internal/server/config.go
index ee3b790097..f8483cbe3f 100644
--- a/internal/server/config.go
+++ b/internal/server/config.go
@@ -198,6 +198,9 @@ func GetAllConfigs() ([]map[string]interface{}, error) {
mysqlConfig := globalConfig.GetMySQLConfig()
exportedMySQLConfigs := mysqlConfig.ExportConfigs()
allConfigs = append(allConfigs, exportedMySQLConfigs)
+ case "vastbase":
+ vastbaseConfig := globalConfig.GetVastbaseMetaConfig()
+ allConfigs = append(allConfigs, vastbaseConfig.ExportConfigs())
default:
return nil, fmt.Errorf("not supported database: %s", databaseType)
}
diff --git a/internal/server/config/database_config.go b/internal/server/config/database_config.go
index eb57232212..4f8997f255 100644
--- a/internal/server/config/database_config.go
+++ b/internal/server/config/database_config.go
@@ -25,7 +25,8 @@ import (
// DatabaseConfig database configuration
type DatabaseConfig struct {
- MySQL MySQLConfig `mapstructure:"mysql"`
+ MySQL MySQLConfig `mapstructure:"mysql"`
+ Vastbase VastbaseMetaConfig `mapstructure:"vastbase"`
}
type MySQLConfig struct {
@@ -40,6 +41,29 @@ type MySQLConfig struct {
Charset string `mapstructure:"charset"`
}
+// VastbaseMetaConfig is the Vastbase G100 metadata database configuration. It
+// reads the same `vastbase:` section the document engine reads: one instance
+// serves both the vector store (db_name) and the business metadata (name), so
+// host, port, user, password and ssl_mode are shared keys. Only `name` and the
+// pool knobs below are metadata-specific.
+type VastbaseMetaConfig struct {
+ DatabaseName string `mapstructure:"name"` // metadata database name
+ User string `mapstructure:"user"`
+ Password string `mapstructure:"password"`
+ Host string `mapstructure:"host"`
+ Port int `mapstructure:"port"`
+ MaxConnections int `mapstructure:"max_connections"`
+ StaleTimeout int `mapstructure:"stale_timeout"`
+ SSLMode string `mapstructure:"ssl_mode"`
+ // DisableReturning suppresses the INSERT/UPDATE/DELETE RETURNING clause
+ // the postgres dialect adds, mirroring peewee's returning_clause=False on
+ // the Python side. Vastbase B mode rejects RETURNING combined with
+ // ON CONFLICT; the default keeps plain RETURNING (needed to read back
+ // auto-increment ids) and only flips this when a deployment finds its
+ // Vastbase build rejects plain RETURNING too.
+ DisableReturning bool `mapstructure:"disable_returning"`
+}
+
func (c *Config) ParseDatabaseConfig(v *viper.Viper) error {
databaseType := c.general.Database
if envType := common.GetEnvSmall(common.EnvDBType); envType != "" {
@@ -50,6 +74,8 @@ func (c *Config) ParseDatabaseConfig(v *viper.Viper) error {
c.parseMySQLConfig(v)
case "oceanbase":
c.parseOceanBaseDatabaseConfig(v)
+ case "vastbase":
+ c.parseVastbaseDatabaseConfig(v)
default:
return fmt.Errorf("database type %s is not supported", databaseType)
}
@@ -94,8 +120,49 @@ func (c *Config) parseOceanBaseDatabaseConfig(v *viper.Viper) {
}
}
-func (c *Config) parseMySQLConfig(v *viper.Viper) {
+// parseVastbaseDatabaseConfig reads the metadata-facing keys of the shared
+// `vastbase:` section. Fully qualified keys rather than v.Sub("vastbase") for
+// the same reason as parseVastbaseConfig: Sub drops the root AutomaticEnv, so
+// an env-only deployment would silently keep every default.
+func (c *Config) parseVastbaseDatabaseConfig(v *viper.Viper) {
+ c.database.Vastbase.DatabaseName = "rag_flow"
+ c.database.Vastbase.Host = "vastbase"
+ c.database.Vastbase.Port = 5432
+ c.database.Vastbase.User = "ragflow"
+ c.database.Vastbase.Password = "Infini_Rag@123"
+ c.database.Vastbase.MaxConnections = 50
+ c.database.Vastbase.StaleTimeout = 300
+ if v.IsSet("vastbase.name") {
+ c.database.Vastbase.DatabaseName = v.GetString("vastbase.name")
+ }
+ if v.IsSet("vastbase.host") {
+ c.database.Vastbase.Host = v.GetString("vastbase.host")
+ }
+ if v.IsSet("vastbase.port") {
+ c.database.Vastbase.Port = v.GetInt("vastbase.port")
+ }
+ if v.IsSet("vastbase.user") {
+ c.database.Vastbase.User = v.GetString("vastbase.user")
+ }
+ if v.IsSet("vastbase.password") {
+ c.database.Vastbase.Password = v.GetString("vastbase.password")
+ }
+ if v.IsSet("vastbase.max_connections") {
+ c.database.Vastbase.MaxConnections = v.GetInt("vastbase.max_connections")
+ }
+ if v.IsSet("vastbase.stale_timeout") {
+ c.database.Vastbase.StaleTimeout = v.GetInt("vastbase.stale_timeout")
+ }
+ if v.IsSet("vastbase.ssl_mode") {
+ c.database.Vastbase.SSLMode = v.GetString("vastbase.ssl_mode")
+ }
+ if v.IsSet("vastbase.disable_returning") {
+ c.database.Vastbase.DisableReturning = v.GetBool("vastbase.disable_returning")
+ }
+}
+
+func (c *Config) parseMySQLConfig(v *viper.Viper) {
// Default MySQL config
c.database.MySQL.DatabaseName = "rag_flow"
c.database.MySQL.User = "root"
@@ -156,6 +223,24 @@ func (c *Config) GetMySQLConfig() MySQLConfig {
return c.database.MySQL
}
+// GetVastbaseMetaConfig returns the Vastbase metadata database configuration.
+func (c *Config) GetVastbaseMetaConfig() VastbaseMetaConfig {
+ return c.database.Vastbase
+}
+
+func (m *VastbaseMetaConfig) ExportConfigs() map[string]interface{} {
+ vastbaseConfigs := make(map[string]interface{})
+ vastbaseConfigs["database_name"] = m.DatabaseName
+ vastbaseConfigs["username"] = m.User
+ vastbaseConfigs["password"] = m.Password
+ vastbaseConfigs["host"] = m.Host
+ vastbaseConfigs["port"] = m.Port
+ vastbaseConfigs["ssl_mode"] = m.SSLMode
+ vastbaseConfigs["max_connections"] = m.MaxConnections
+ vastbaseConfigs["stale_timeout"] = m.StaleTimeout
+ return vastbaseConfigs
+}
+
func (m *MySQLConfig) ExportConfigs() map[string]interface{} {
var mysqlConfigs map[string]interface{}
mysqlConfigs = make(map[string]interface{})
diff --git a/internal/server/config/environments.go b/internal/server/config/environments.go
index 94baae6b7e..9842c85dce 100644
--- a/internal/server/config/environments.go
+++ b/internal/server/config/environments.go
@@ -173,7 +173,7 @@ func (c *Config) GetEnvironments() error {
databaseType := common.GetEnvSmall(common.EnvDBType)
if databaseType != "" {
switch databaseType {
- case "mysql", "oceanbase":
+ case "mysql", "oceanbase", "vastbase":
c.environments.DatabaseType = databaseType
default:
return fmt.Errorf("invalid database type: %s", databaseType)
diff --git a/internal/server/config/vastbase_config_test.go b/internal/server/config/vastbase_config_test.go
index 3b22879d04..195846e305 100644
--- a/internal/server/config/vastbase_config_test.go
+++ b/internal/server/config/vastbase_config_test.go
@@ -133,3 +133,111 @@ func TestVastbaseExportConfigsKeys(t *testing.T) {
}
}
}
+
+// TestParseVastbaseDatabaseConfigDefaultsAndOverrides checks the metadata-side
+// defaults of the shared vastbase: section and that file values override them
+// without disturbing the doc-engine keys.
+func TestParseVastbaseDatabaseConfigDefaultsAndOverrides(t *testing.T) {
+ t.Setenv("DB_TYPE", "vastbase")
+ v := viper.New()
+ c := &Config{}
+ if err := c.ParseGeneralConfig(v); err != nil {
+ t.Fatal(err)
+ }
+ if err := c.ParseDatabaseConfig(v); err != nil {
+ t.Fatal(err)
+ }
+ defaults := c.GetVastbaseMetaConfig()
+ if defaults.DatabaseName != "rag_flow" || defaults.Host != "vastbase" || defaults.Port != 5432 ||
+ defaults.User != "ragflow" || defaults.Password != "Infini_Rag@123" ||
+ defaults.MaxConnections != 50 || defaults.StaleTimeout != 300 ||
+ defaults.SSLMode != "" || defaults.DisableReturning {
+ t.Fatalf("vastbase metadata defaults = %#v", defaults)
+ }
+
+ v = viper.New()
+ v.Set("vastbase", map[string]interface{}{
+ "host": "vb-host", "port": 15432, "user": "vb-user",
+ "password": "vb-secret", "name": "meta_db",
+ "max_connections": 11, "stale_timeout": 22,
+ "ssl_mode": "require", "disable_returning": true,
+ })
+ c = &Config{}
+ if err := c.ParseDatabaseConfig(v); err != nil {
+ t.Fatal(err)
+ }
+ got := c.GetVastbaseMetaConfig()
+ if got.DatabaseName != "meta_db" || got.Host != "vb-host" || got.Port != 15432 ||
+ got.User != "vb-user" || got.Password != "vb-secret" ||
+ got.MaxConnections != 11 || got.StaleTimeout != 22 ||
+ got.SSLMode != "require" || !got.DisableReturning {
+ t.Fatalf("vastbase metadata overrides = %#v", got)
+ }
+}
+
+// TestParseDatabaseConfigAcceptsVastbaseType checks DB_TYPE=vastbase routes to
+// the vastbase parser instead of the "not supported" error.
+func TestParseDatabaseConfigAcceptsVastbaseType(t *testing.T) {
+ t.Setenv("DB_TYPE", "vastbase")
+ v := viper.New()
+ c := &Config{}
+ if err := c.ParseDatabaseConfig(v); err != nil {
+ t.Fatalf("ParseDatabaseConfig(vastbase): %v", err)
+ }
+ if got := c.GetVastbaseMetaConfig().DatabaseName; got != "rag_flow" {
+ t.Fatalf("metadata database name = %q, want rag_flow", got)
+ }
+}
+
+// TestParseVastbaseDatabaseConfigEnvironmentOnly mirrors the doc-engine env
+// test: the metadata keys must be readable from RAGFLOW_VASTBASE_* variables
+// through the parent viper's AutomaticEnv.
+func TestParseVastbaseDatabaseConfigEnvironmentOnly(t *testing.T) {
+ t.Setenv("DB_TYPE", "vastbase")
+ t.Setenv("RAGFLOW_VASTBASE_HOST", "env-host")
+ t.Setenv("RAGFLOW_VASTBASE_NAME", "env_meta")
+ t.Setenv("RAGFLOW_VASTBASE_MAX_CONNECTIONS", "17")
+ v := viper.New()
+ v.SetEnvPrefix("RAGFLOW")
+ v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
+ v.AutomaticEnv()
+ c := &Config{}
+ if err := c.ParseGeneralConfig(v); err != nil {
+ t.Fatal(err)
+ }
+ if err := c.ParseDatabaseConfig(v); err != nil {
+ t.Fatal(err)
+ }
+ got := c.GetVastbaseMetaConfig()
+ if got.Host != "env-host" || got.DatabaseName != "env_meta" || got.MaxConnections != 17 {
+ t.Fatalf("vastbase metadata env-only overrides = %#v", got)
+ }
+}
+
+// TestVastbaseMetaExportConfigsKeys checks the exported metadata configuration
+// carries exactly the expected keys.
+func TestVastbaseMetaExportConfigsKeys(t *testing.T) {
+ exported := (&VastbaseMetaConfig{
+ DatabaseName: "d", User: "u", Password: "p", Host: "h", Port: 1,
+ SSLMode: "disable", MaxConnections: 2, StaleTimeout: 3,
+ }).ExportConfigs()
+ for _, key := range []string{"database_name", "username", "password", "host", "port", "ssl_mode", "max_connections", "stale_timeout"} {
+ if _, present := exported[key]; !present {
+ t.Fatalf("ExportConfigs missing key %q: %#v", key, exported)
+ }
+ }
+}
+
+// TestGetEnvironmentsAcceptsVastbaseDBType guards the DB_TYPE env gate: the
+// server aborts startup with "invalid database type" before the vastbase
+// dialector is reached if this case list drifts from the supported set.
+func TestGetEnvironmentsAcceptsVastbaseDBType(t *testing.T) {
+ t.Setenv("DB_TYPE", "vastbase")
+ c := &Config{}
+ if err := c.GetEnvironments(); err != nil {
+ t.Fatalf("GetEnvironments with DB_TYPE=vastbase: %v", err)
+ }
+ if got := c.GetEnvDatabaseType(); got != "vastbase" {
+ t.Fatalf("env database type = %q, want vastbase", got)
+ }
+}