Skip to content

Commit 94cdfee

Browse files
kenlee1988“Ken.li”
andauthored
refactor(catalog): remove connector config immutable check, allow partial update (#476)
移除了 connector_config 的不可变字段校验逻辑,放开对数据库、host等配置字段的修改限制,修改后旧资源会在下次发现对齐时标记为stale,新增对应测试用例验证更新行为 Co-authored-by: “Ken.li” <“Ken.li@kweaver.ai”>
1 parent 8e8918b commit 94cdfee

3 files changed

Lines changed: 45 additions & 43 deletions

File tree

adp/docs/api/vega/vega-backend-api/catalog.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ paths:
275275
`enabled` 不能通过 PUT 切换;请求体中的 `enabled` 必须与当前 catalog 状态一致。
276276
如需启用/禁用,使用 `POST /catalogs/{id}/enable` 或 `POST /catalogs/{id}/disable`。
277277
278+
`connector_config` 可修改;修改影响发现范围的字段后,旧 Resource 会在下一次
279+
discover 对齐时标记为 `stale`。
280+
278281
**extensions**:请求体若包含 **`extensions` 键**(含空对象 `{}`),则对该 catalog **整包替换**
279282
`t_entity_extension` 中 `f_entity_id = id` 的全部行;**键未出现**则不修改副表。
280283
requestBody:

adp/vega/vega-backend/server/driveradapters/catalog_handler.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"context"
1111
"fmt"
1212
"net/http"
13-
"reflect"
1413
"strconv"
1514
"strings"
1615
"vega-backend/common"
@@ -400,7 +399,6 @@ func (r *restHandler) updateCatalog(c *gin.Context, visitor hydra.Visitor) {
400399
return
401400
}
402401

403-
// Validate immutable fields
404402
// connector_type cannot be modified
405403
if req.ConnectorType != catalog.ConnectorType {
406404
span.SetStatus(codes.Error, "Connector type cannot be modified")
@@ -419,47 +417,6 @@ func (r *restHandler) updateCatalog(c *gin.Context, visitor hydra.Visitor) {
419417
return
420418
}
421419

422-
// connector_config immutable fields: host, port, database, databases, schemas, paths, protocol
423-
// These fields cannot be modified or removed if they exist in the original catalog
424-
immutableFields := []string{"host", "port", "database", "databases", "schemas", "paths", "protocol", "concurrent"}
425-
for _, field := range immutableFields {
426-
if _, existsInCatalog := catalog.ConnectorCfg[field]; existsInCatalog {
427-
if _, existsInReq := req.ConnectorCfg[field]; existsInReq {
428-
// Field exists in both, check if it's being modified
429-
// Handle different types: string, number, array
430-
catalogValue := catalog.ConnectorCfg[field]
431-
reqValue := req.ConnectorCfg[field]
432-
433-
var isModified bool
434-
switch v := catalogValue.(type) {
435-
case []interface{}:
436-
// Compare arrays using reflect.DeepEqual
437-
isModified = !reflect.DeepEqual(v, reqValue)
438-
default:
439-
// Compare other types (string, number, etc.)
440-
isModified = (reqValue != catalogValue)
441-
}
442-
443-
if isModified {
444-
span.SetStatus(codes.Error, fmt.Sprintf("Connector config field %s cannot be modified", field))
445-
httpErr := rest.NewHTTPError(ctx, http.StatusBadRequest, verrors.VegaBackend_Catalog_InvalidParameter_ConnectorConfig).
446-
WithErrorDetails(fmt.Sprintf("connector_config.%s cannot be modified", field))
447-
oteltrace.AddHttpAttrs4HttpError(span, httpErr)
448-
rest.ReplyError(c, httpErr)
449-
return
450-
}
451-
} else {
452-
// Field exists in catalog but not in request - cannot remove immutable fields
453-
span.SetStatus(codes.Error, fmt.Sprintf("Connector config field %s cannot be removed", field))
454-
httpErr := rest.NewHTTPError(ctx, http.StatusBadRequest, verrors.VegaBackend_Catalog_InvalidParameter_ConnectorConfig).
455-
WithErrorDetails(fmt.Sprintf("connector_config.%s cannot be removed", field))
456-
oteltrace.AddHttpAttrs4HttpError(span, httpErr)
457-
rest.ReplyError(c, httpErr)
458-
return
459-
}
460-
}
461-
}
462-
463420
// Apply updates
464421
if req.Name != catalog.Name {
465422
exists, err := r.cs.CheckExistByName(ctx, req.Name)

adp/vega/vega-backend/server/driveradapters/catalog_handler_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ func Test_CatalogRestHandler_UpdateRejectsEnabledChange(t *testing.T) {
210210
})
211211
}
212212

213+
func Test_CatalogRestHandler_UpdateAllowsDatabaseChange(t *testing.T) {
214+
Convey("Test CatalogHandler Update allows database change\n", t, func() {
215+
test := setGinMode()
216+
defer test()
217+
218+
engine := gin.New()
219+
engine.Use(gin.Recovery())
220+
221+
mockCtrl := gomock.NewController(t)
222+
defer mockCtrl.Finish()
223+
224+
cs := vmock.NewMockCatalogService(mockCtrl)
225+
handler := MockNewRestHandler(&common.AppSetting{}, nil, cs, nil, nil, nil, nil, nil, nil, nil, nil)
226+
handler.RegisterPublic(engine)
227+
228+
cs.EXPECT().GetByID(gomock.Any(), "catalog-1", false).
229+
Return(&interfaces.Catalog{
230+
ID: "catalog-1",
231+
Name: "catalog",
232+
Enabled: true,
233+
ConnectorType: "mariadb",
234+
ConnectorCfg: interfaces.ConnectorConfig{
235+
"host": "localhost",
236+
"database": "db1",
237+
},
238+
}, nil)
239+
cs.EXPECT().Update(gomock.Any(), gomock.Any(), gomock.Any()).
240+
DoAndReturn(func(_ context.Context, _ *interfaces.Catalog, req *interfaces.CatalogRequest) error {
241+
So(req.ConnectorCfg["database"], ShouldEqual, "db2")
242+
return nil
243+
})
244+
245+
body := `{"id":"catalog-1","name":"catalog","enabled":true,"connector_type":"mariadb","connector_config":{"host":"localhost","database":"db2"}}`
246+
req := httptest.NewRequest(http.MethodPut, "/api/vega-backend/in/v1/catalogs/catalog-1", strings.NewReader(body))
247+
req.Header.Set("Content-Type", "application/json")
248+
w := httptest.NewRecorder()
249+
engine.ServeHTTP(w, req)
250+
251+
So(w.Result().StatusCode, ShouldEqual, http.StatusNoContent)
252+
})
253+
}
254+
213255
func Test_CatalogRestHandler_DiscoverRejectsDisabledCatalog(t *testing.T) {
214256
Convey("Test CatalogHandler Discover rejects disabled catalog\n", t, func() {
215257
test := setGinMode()

0 commit comments

Comments
 (0)