Skip to content

Commit 6978015

Browse files
authored
Merge pull request #639 from actiontech/dev/redis-plugin-873
feat: support Redis SQL workbench datasource #873
2 parents 5783501 + 29b7571 commit 6978015

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

internal/dms/pkg/constant/const.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ func ParseDBType(s string) (DBType, error) {
258258
return DBTypePolarDBForMySQL, nil
259259
case "MongoDB":
260260
return DBTypeMongoDB, nil
261+
case "Redis":
262+
return DBTypeRedis, nil
261263
case "OceanBase For Oracle":
262264
return DBTypeOceanBaseOracle, nil
263265

@@ -284,6 +286,7 @@ const (
284286
DBTypeHANA DBType = "HANA"
285287
DBTypePolarDBForMySQL DBType = "PolarDB For MySQL"
286288
DBTypeMongoDB DBType = "MongoDB"
289+
DBTypeRedis DBType = "Redis"
287290
DBTypeOceanBaseOracle DBType = "OceanBase For Oracle"
288291
)
289292

internal/dms/pkg/constant/const_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func TestParseDBType(t *testing.T) {
128128
// PolarDB-MySQL 新增 (Issue #826)
129129
"PolarDB For MySQL": {input: "PolarDB For MySQL", expected: DBTypePolarDBForMySQL},
130130
"MongoDB": {input: "MongoDB", expected: DBTypeMongoDB},
131+
"Redis": {input: "Redis", expected: DBTypeRedis},
131132
// "PolarDB" 单独不应匹配
132133
"PolarDB only": {input: "PolarDB", expectError: true},
133134
"invalid type": {input: "UnknownDB", expectError: true},

internal/sql_workbench/service/sql_workbench_service.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,7 @@ const (
863863
mongoTLSEnabledParam = "tls"
864864
mongoDirectConnectionParam = "direct_connection"
865865
mongoTLSSkipVerifyParam = "tls_skip_verify"
866+
redisDefaultDatabaseParam = "default_database"
866867
)
867868

868869
// buildDatasourceBaseInfo 构建数据源基础信息
@@ -898,6 +899,10 @@ func (sqlWorkbenchService *SqlWorkbenchService) fillDatasourceBaseInfo(datasourc
898899
baseInfo.DefaultSchema, baseInfo.Properties, baseInfo.JDBCParams = buildMongoDatasourceOptions(dbService)
899900
}
900901

902+
if dbService.DBType == string(pkgConst.DBTypeRedis) {
903+
baseInfo.DefaultSchema, baseInfo.Properties, baseInfo.JDBCParams = buildRedisDatasourceOptions(dbService)
904+
}
905+
901906
// DB2 特殊处理:从 AdditionalParams.database_name 取默认 schema 透传到 ODC
902907
if dbService.DBType == "DB2" {
903908
databaseNameParam := dbService.AdditionalParams.GetParam("database_name")
@@ -993,6 +998,8 @@ func (sqlWorkbenchService *SqlWorkbenchService) convertDBType(dmsDBType string)
993998
return "MYSQL"
994999
case "MongoDB":
9951000
return "MONGODB"
1001+
case "Redis":
1002+
return "REDIS"
9961003
case "DB2":
9971004
return "DB2"
9981005
default:
@@ -1010,7 +1017,8 @@ func (sqlWorkbenchService *SqlWorkbenchService) SupportDBType(dbType pkgConst.DB
10101017
dbType == pkgConst.DBTypeGoldenDB ||
10111018
dbType == pkgConst.DBTypePolarDBForMySQL ||
10121019
dbType == pkgConst.DBTypeGaussDB ||
1013-
dbType == pkgConst.DBTypePostgreSQL
1020+
dbType == pkgConst.DBTypePostgreSQL ||
1021+
dbType == pkgConst.DBTypeRedis
10141022
}
10151023

10161024
func buildMongoDatasourceOptions(dbService *biz.DBService) (*string, interface{}, map[string]interface{}) {
@@ -1050,6 +1058,18 @@ func buildMongoDatasourceOptions(dbService *biz.DBService) (*string, interface{}
10501058
return defaultSchema, nil, jdbcParams
10511059
}
10521060

1061+
func buildRedisDatasourceOptions(dbService *biz.DBService) (*string, interface{}, map[string]interface{}) {
1062+
defaultDatabase := dbService.AdditionalParams.GetParam(redisDefaultDatabaseParam).String()
1063+
if defaultDatabase == "" {
1064+
return nil, nil, nil
1065+
}
1066+
defaultSchema := &defaultDatabase
1067+
jdbcParams := map[string]interface{}{
1068+
"defaultDatabase": defaultDatabase,
1069+
}
1070+
return defaultSchema, nil, jdbcParams
1071+
}
1072+
10531073
func interfacePtr(v interface{}) *interface{} {
10541074
if v == nil {
10551075
return nil

internal/sql_workbench/service/sql_workbench_service_test.go

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func Test_convertDBType(t *testing.T) {
2828
"PolarDB For MySQL": {input: "PolarDB For MySQL", expected: "MYSQL"},
2929
"GaussDB": {input: "GaussDB", expected: "GAUSSDB"},
3030
"MongoDB": {input: "MongoDB", expected: "MONGODB"},
31+
"Redis": {input: "Redis", expected: "REDIS"},
3132
"DB2": {input: "DB2", expected: "DB2"},
3233
"Unknown passthrough": {input: "UnknownDB", expected: "UnknownDB"},
3334
}
@@ -55,6 +56,7 @@ func Test_SupportDBType(t *testing.T) {
5556
"TDSQL supported": {input: pkgConst.DBTypeTDSQLForInnoDB, expected: true},
5657
"GoldenDB supported": {input: pkgConst.DBTypeGoldenDB, expected: true},
5758
"MongoDB unsupported": {input: pkgConst.DBTypeMongoDB, expected: false},
59+
"Redis supported": {input: pkgConst.DBTypeRedis, expected: true},
5860
"PostgreSQL supported": {input: pkgConst.DBTypePostgreSQL, expected: true},
5961
"SQL Server unsupported": {input: pkgConst.DBTypeSQLServer, expected: false},
6062
"PolarDB For MySQL supported": {input: pkgConst.DBTypePolarDBForMySQL, expected: true},
@@ -140,6 +142,44 @@ func Test_buildMongoDatasourceOptions_tlsOnly(t *testing.T) {
140142
}
141143
}
142144

145+
func Test_buildRedisDatasourceOptions(t *testing.T) {
146+
defaultDB := "2"
147+
defaultSchema, propertiesValue, jdbcParams := buildRedisDatasourceOptions(&biz.DBService{
148+
DBType: string(pkgConst.DBTypeRedis),
149+
Host: "127.0.0.1",
150+
Port: "6379",
151+
AdditionalParams: pkgParams.Params{
152+
&pkgParams.Param{Key: redisDefaultDatabaseParam, Value: defaultDB, Type: pkgParams.ParamTypeString},
153+
},
154+
})
155+
if defaultSchema == nil || *defaultSchema != defaultDB {
156+
t.Fatalf("unexpected default schema: %#v", defaultSchema)
157+
}
158+
if propertiesValue != nil {
159+
t.Fatalf("expected nil properties, got %#v", propertiesValue)
160+
}
161+
if jdbcParams["defaultDatabase"] != defaultDB {
162+
t.Fatalf("unexpected redis jdbc params: %#v", jdbcParams)
163+
}
164+
}
165+
166+
func Test_buildRedisDatasourceOptions_noSensitiveProperties(t *testing.T) {
167+
_, propertiesValue, jdbcParams := buildRedisDatasourceOptions(&biz.DBService{
168+
DBType: string(pkgConst.DBTypeRedis),
169+
User: "default",
170+
Password: "secret",
171+
AdditionalParams: pkgParams.Params{
172+
&pkgParams.Param{Key: redisDefaultDatabaseParam, Value: "0", Type: pkgParams.ParamTypeString},
173+
},
174+
})
175+
if propertiesValue != nil {
176+
t.Fatalf("expected redis properties to stay nil, got %#v", propertiesValue)
177+
}
178+
if _, ok := jdbcParams["password"]; ok {
179+
t.Fatalf("redis password leaked into jdbc params: %#v", jdbcParams)
180+
}
181+
}
182+
143183
// Test_buildDatasourceBaseInfo_DB2 覆盖 buildDatasourceBaseInfo 中 DB2 / 回归 4 组 case:
144184
//
145185
// (a) DB2 正例:AdditionalParam database_name=testdb → baseInfo.DefaultSchema=="testdb"
@@ -154,11 +194,11 @@ func Test_buildDatasourceBaseInfo_DB2(t *testing.T) {
154194
const datasourceName = "proj:ds"
155195

156196
cases := map[string]struct {
157-
dbService *biz.DBService
158-
expectErr bool
159-
expectErrSubstr string
160-
expectDefaultSchema *string
161-
expectServiceName *string
197+
dbService *biz.DBService
198+
expectErr bool
199+
expectErrSubstr string
200+
expectDefaultSchema *string
201+
expectServiceName *string
162202
}{
163203
"DB2 happy path": {
164204
dbService: &biz.DBService{

0 commit comments

Comments
 (0)