Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions internal/dms/biz/db_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,13 @@ func (d *DBServiceUsecase) GetDriverParamsByDBType(ctx context.Context, dbType s
if err != nil {
return nil, err
}
// 同时接受调用方传入的主形态与别名(如 "GaussDB / openGauss" / "GaussDB" 都接受),
// 与 driver_options 接口实际暴露的字面值进行同形态比对。详见 #2877 bug-C。
normalized := pkgConst.NormalizeDBType(dbType)
for _, driverOptions := range databaseOptions {
if driverOptions.DBType == dbType {
if driverOptions.DBType == dbType ||
driverOptions.DBType == normalized ||
pkgConst.NormalizeDBType(driverOptions.DBType) == normalized {
return convertAdditionParamsToParams(driverOptions.Params), nil
}

Expand Down Expand Up @@ -715,7 +720,9 @@ func (d *DBServiceUsecase) UpdateDBServiceByArgs(ctx context.Context, dbServiceU

// check
{
if ds.DBType != updateDBService.DBType {
// 允许调用方传入 alias(如 "GaussDB"),与落库的主形态(如
// "GaussDB / openGauss")做归一化比较,详见 #2877 bug-C。
if pkgConst.NormalizeDBType(ds.DBType) != pkgConst.NormalizeDBType(updateDBService.DBType) {
return fmt.Errorf("update db service db type is unsupported")
}

Expand Down
29 changes: 29 additions & 0 deletions internal/dms/pkg/constant/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,35 @@ func ParseDBType(s string) (DBType, error) {
}
}

// dbTypeAliasMap 将上下游(如 SQLE plugin / 前端规范化后的 db_type)使用的别名映射回
// DMS 内部落库使用的主形态。例如 GaussDB / openGauss 的 DBType 常量为
// "GaussDB / openGauss",但 SQLE plugin 与 driver_options 接口暴露给前端的字面值是
// "GaussDB",前端再以 "GaussDB" 作为 filter 条件回查 db_services / 在编辑数据源时回写
// db_type,会因字面值不一致而失败(issue #2877 bug-A / bug-C)。
//
// 当前只覆盖 GaussDB;如未来新增其它需要 alias 的 DBType,在此处补充。
var dbTypeAliasMap = map[string]DBType{
"GaussDB": DBTypeGaussDB,
"GaussDB / openGauss": DBTypeGaussDB,
"openGauss": DBTypeGaussDB,
}

// NormalizeDBType 将传入的 db_type 字符串归一化为 DMS 内部落库使用的主形态。
// - 命中 DBType 枚举常量本身(如 "MySQL")直接返回;
// - 命中已知 alias(如 "GaussDB" / "openGauss")映射为主形态 DBTypeGaussDB;
// - 其它字符串保持原样返回,由调用方决定是否走 ParseDBType / 白名单校验。
//
// 该函数只做"形态归一",不做合法性校验;不会返回 error。
func NormalizeDBType(s string) string {
if s == "" {
return s
}
if v, ok := dbTypeAliasMap[s]; ok {
return string(v)
}
return s
}

const (
DBTypeMySQL DBType = "MySQL"
DBTypePostgreSQL DBType = "PostgreSQL"
Expand Down
30 changes: 30 additions & 0 deletions internal/dms/pkg/constant/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,33 @@ func TestParseDBType(t *testing.T) {
})
}
}

// TestNormalizeDBType 验证 db_type alias 归一化覆盖 #2877 bug-A / bug-C 的核心契约。
func TestNormalizeDBType(t *testing.T) {
cases := []struct {
name string
in string
want string
}{
// GaussDB 别名归一为主形态
{"alias-GaussDB", "GaussDB", string(DBTypeGaussDB)},
{"alias-openGauss", "openGauss", string(DBTypeGaussDB)},
// 主形态保持不变
{"canonical-GaussDB", string(DBTypeGaussDB), string(DBTypeGaussDB)},
{"canonical-MySQL", string(DBTypeMySQL), string(DBTypeMySQL)},
// 其它已存在的 DBType 不受影响
{"PostgreSQL", "PostgreSQL", "PostgreSQL"},
{"DM", string(DBTypeDM), string(DBTypeDM)},
// 未知值 / 空串保持原样
{"unknown", "UnknownDB", "UnknownDB"},
{"empty", "", ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := NormalizeDBType(tc.in)
if got != tc.want {
t.Errorf("NormalizeDBType(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
4 changes: 3 additions & 1 deletion internal/dms/service/db_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,12 @@ func (d *DMSService) ListDBServices(ctx context.Context, req *dmsCommonV2.ListDB
}

if req.FilterByDBType != "" {
// 将上下游传入的 db_type 别名(如 "GaussDB")归一为 DMS 内部落库的主形态
// (如 "GaussDB / openGauss"),避免字面值不一致导致 filter 命中失败。详见 #2877 bug-A。
andConditions = append(andConditions, pkgConst.FilterCondition{
Field: string(biz.DBServiceFieldDBType),
Operator: pkgConst.FilterOperatorEqual,
Value: req.FilterByDBType,
Value: pkgConst.NormalizeDBType(req.FilterByDBType),
})
}

Expand Down