Skip to content

Commit 686f8ea

Browse files
authored
dm: refine openapi table migrate rule source/target (#11933)
close #4287
1 parent 1dd87cf commit 686f8ea

5 files changed

Lines changed: 100 additions & 123 deletions

File tree

dm/config/task_converters.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -610,21 +610,14 @@ func SubTaskConfigsToOpenAPITask(subTaskConfigList []*SubTaskConfig) *openapi.Ta
610610
ruleMap := map[string]struct{}{}
611611
appendOneRule := func(sourceName, schemaPattern, tablePattern, targetSchema, targetTable string) {
612612
tableMigrateRule := openapi.TaskTableMigrateRule{
613-
Source: struct {
614-
Schema string `json:"schema"`
615-
SourceName string `json:"source_name"`
616-
Table string `json:"table"`
617-
}{
613+
Source: openapi.TaskTableMigrateRuleSource{
618614
Schema: schemaPattern,
619615
SourceName: sourceName,
620616
Table: tablePattern,
621617
},
622618
}
623619
if targetSchema != "" {
624-
tableMigrateRule.Target = &struct {
625-
Schema *string `json:"schema,omitempty"`
626-
Table *string `json:"table,omitempty"`
627-
}{
620+
tableMigrateRule.Target = &openapi.TaskTableMigrateRuleTarget{
628621
Schema: &targetSchema,
629622
}
630623
if targetTable != "" {

dm/config/task_converters_test.go

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,11 @@ func TestConvertBetweenOpenAPITaskAndTaskConfig(t *testing.T) {
459459
sourceSchema := task.TableMigrateRule[0].Source.Schema
460460
targetSchema := *task.TableMigrateRule[0].Target.Schema
461461
// only route schema
462-
task.TableMigrateRule[0].Source = struct {
463-
Schema string `json:"schema"`
464-
SourceName string `json:"source_name"`
465-
Table string `json:"table"`
466-
}{
462+
task.TableMigrateRule[0].Source = openapi.TaskTableMigrateRuleSource{
467463
SourceName: source1Name,
468464
Schema: sourceSchema,
469465
}
470-
task.TableMigrateRule[0].Target = &struct {
471-
Schema *string `json:"schema,omitempty"`
472-
Table *string `json:"table,omitempty"`
473-
}{
466+
task.TableMigrateRule[0].Target = &openapi.TaskTableMigrateRuleTarget{
474467
Schema: &targetSchema,
475468
}
476469
taskCfg, err = OpenAPITaskToTaskConfig(&task, sourceCfgMap)
@@ -504,38 +497,24 @@ func TestConvertBetweenOpenAPITaskAndTaskConfig(t *testing.T) {
504497
// only route table will meet error
505498
sourceTable := "tb"
506499
targetTable := "tb1"
507-
task.TableMigrateRule[0].Source = struct {
508-
Schema string `json:"schema"`
509-
SourceName string `json:"source_name"`
510-
Table string `json:"table"`
511-
}{
500+
task.TableMigrateRule[0].Source = openapi.TaskTableMigrateRuleSource{
512501
SourceName: source1Name,
513502
Schema: sourceSchema,
514503
Table: sourceTable,
515504
}
516-
task.TableMigrateRule[0].Target = &struct {
517-
Schema *string `json:"schema,omitempty"`
518-
Table *string `json:"table,omitempty"`
519-
}{
505+
task.TableMigrateRule[0].Target = &openapi.TaskTableMigrateRuleTarget{
520506
Table: &targetTable,
521507
}
522508
_, err = OpenAPITaskToTaskConfig(&task, sourceCfgMap)
523509
require.True(t, terror.ErrConfigGenTableRouter.Equal(err))
524510

525511
// route both
526-
task.TableMigrateRule[0].Source = struct {
527-
Schema string `json:"schema"`
528-
SourceName string `json:"source_name"`
529-
Table string `json:"table"`
530-
}{
512+
task.TableMigrateRule[0].Source = openapi.TaskTableMigrateRuleSource{
531513
SourceName: source1Name,
532514
Schema: sourceSchema,
533515
Table: sourceTable,
534516
}
535-
task.TableMigrateRule[0].Target = &struct {
536-
Schema *string `json:"schema,omitempty"`
537-
Table *string `json:"table,omitempty"`
538-
}{
517+
task.TableMigrateRule[0].Target = &openapi.TaskTableMigrateRuleTarget{
539518
Schema: &targetSchema,
540519
Table: &targetTable,
541520
}
@@ -578,11 +557,7 @@ func TestConvertBetweenOpenAPITaskAndTaskConfig(t *testing.T) {
578557
require.EqualValues(t, taskAfterConvert, &task)
579558

580559
// no route and sync one schema
581-
task.TableMigrateRule[0].Source = struct {
582-
Schema string `json:"schema"`
583-
SourceName string `json:"source_name"`
584-
Table string `json:"table"`
585-
}{
560+
task.TableMigrateRule[0].Source = openapi.TaskTableMigrateRuleSource{
586561
SourceName: source1Name,
587562
Schema: sourceSchema,
588563
Table: "",

dm/openapi/gen.server.go

Lines changed: 37 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dm/openapi/gen.types.go

Lines changed: 21 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dm/openapi/spec/dm.yaml

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,6 +1412,37 @@ components:
14121412
- "user"
14131413
- "enable_gtid"
14141414
- "enable"
1415+
TaskTableMigrateRuleSource:
1416+
description: source-related configuration
1417+
properties:
1418+
source_name:
1419+
type: string
1420+
description: source name
1421+
example: "source-name"
1422+
schema:
1423+
type: string
1424+
description: schema name, wildcard support
1425+
example: "db-*"
1426+
table:
1427+
type: string
1428+
description: table name, wildcard support
1429+
example: "tb-*"
1430+
required:
1431+
- "source_name"
1432+
- "schema"
1433+
- "table"
1434+
TaskTableMigrateRuleTarget:
1435+
type: object
1436+
description: downstream-related configuration
1437+
properties:
1438+
schema:
1439+
type: string
1440+
description: schema name, does not support wildcards
1441+
example: "db1"
1442+
table:
1443+
type: string
1444+
description: table name, does not support wildcards
1445+
example: "tb1"
14151446
ShardingGroup:
14161447
type: object
14171448
properties:
@@ -1646,37 +1677,9 @@ components:
16461677
description: "upstream table to downstream migrate rules"
16471678
properties:
16481679
source:
1649-
type: object
1650-
description: source-related configuration
1651-
properties:
1652-
source_name:
1653-
type: string
1654-
description: source name
1655-
example: "source-name"
1656-
schema:
1657-
type: string
1658-
description: schema name, wildcard support
1659-
example: "db-*"
1660-
table:
1661-
type: string
1662-
description: table name, wildcard support
1663-
example: "tb-*"
1664-
required:
1665-
- "source_name"
1666-
- "schema"
1667-
- "table"
1680+
$ref: "#/components/schemas/TaskTableMigrateRuleSource"
16681681
target:
1669-
type: object
1670-
description: downstream-related configuration
1671-
properties:
1672-
schema:
1673-
type: string
1674-
description: schema name, does not support wildcards
1675-
example: "db1"
1676-
table:
1677-
type: string
1678-
description: table name, does not support wildcards
1679-
example: "tb1"
1682+
$ref: "#/components/schemas/TaskTableMigrateRuleTarget"
16801683
binlog_filter_rule:
16811684
type: array
16821685
description: "filter rule name"

0 commit comments

Comments
 (0)