Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: merge partition (#13282)
h3n4lef2c69440b8f
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.goindex 57d96c9303..9f66252881 100644--- a/backend/api/v1/schema_design_merge.go+++ b/backend/api/v1/schema_design_merge.go@@ -23,55 +23,61 @@ var (nowRegexp = regexp.MustCompile(`(?mi)NOW(\((?P<fsp>(\d+))?\))?`))+type timestampDefaultValue interface {+ isTimeStampDefaultValue()+ getFsp() int+}type currentTimestamp struct {// fsp is the fractional seconds precision.fsp int}+func (*currentTimestamp) isTimeStampDefaultValue() {}++func (c *currentTimestamp) getFsp() int { return c.fsp }+type now struct {fsp int}-func buildNow(input string) *now {- matches := nowRegexp.FindStringSubmatch(input)- if matches == nil {- return nil- }+func (*now) isTimeStampDefaultValue() {}- for i, name := range nowRegexp.SubexpNames() {- if name == "fsp" {- if matches[i] != "" {- fsp, err := strconv.ParseInt(matches[i], 10, 64)- if err != nil {- return nil+func (n *now) getFsp() int { return n.fsp }++func buildTimestampDefaultValue(input string) timestampDefaultValue {+ matches := currentTimestampRegexp.FindStringSubmatch(input)+ if matches != nil {+ for i, name := range currentTimestampRegexp.SubexpNames() {+ if name == "fsp" {+ if matches[i] != "" {+ fsp, err := strconv.ParseInt(matches[i], 10, 64)+ if err != nil {+ return nil+ }+ return ¤tTimestamp{int(fsp)}}- return &now{int(fsp)}}}+ return ¤tTimestamp{}}- return &now{}-}--func buildCurrentTimestamp(input string) *currentTimestamp {- matches := currentTimestampRegexp.FindStringSubmatch(input)- if matches == nil {- return nil- }-- for i, name := range currentTimestampRegexp.SubexpNames() {- if name == "fsp" {- if matches[i] != "" {- fsp, err := strconv.ParseInt(matches[i], 10, 64)- if err != nil {- return nil+ matches = nowRegexp.FindStringSubmatch(input)+ if matches != nil {+ for i, name := range nowRegexp.SubexpNames() {+ if name == "fsp" {+ if matches[i] != "" {+ fsp, err := strconv.ParseInt(matches[i], 10, 64)+ if err != nil {+ return nil+ }+ return &now{int(fsp)}}- return ¤tTimestamp{int(fsp)}}}+ return &now{}}- return ¤tTimestamp{}+ return nil}type diffAction string@@ -602,6 +608,7 @@ func (n *metadataDiffTableNode) applyDiffTo(target *storepb.SchemaMetadata) erroColumns: table.Columns,ForeignKeys: table.ForeignKeys,Indexes: table.Indexes,+ Partitions: table.Partitions,}for _, columnName := range n.columnNames {if columnNode, in := n.columnsMap[columnName]; in {@@ -750,15 +757,10 @@ func compareColumnDefaultValue(a, b any) bool {if strings.EqualFold(aExpr.DefaultExpression, "AUTO_INCREMENT") {return strings.EqualFold(bExpr.DefaultExpression, "AUTO_INCREMENT")}- aCurrentTimestamp := buildCurrentTimestamp(aExpr.DefaultExpression)- bCurrentTimestamp := buildCurrentTimestamp(bExpr.DefaultExpression)- if aCurrentTimestamp != nil && bCurrentTimestamp != nil {- return aCurrentTimestamp.fsp == bCurrentTimestamp.fsp- }- aNow := buildNow(aExpr.DefaultExpression)- bNow := buildNow(bExpr.DefaultExpression)- if aNow != nil && bNow != nil {- return aNow.fsp == bNow.fsp+ aTimestampDefaultValue := buildTimestampDefaultValue(aExpr.DefaultExpression)+ bTimestampDefaultValue := buildTimestampDefaultValue(bExpr.DefaultExpression)+ if aTimestampDefaultValue != nil && bTimestampDefaultValue != nil {+ return aTimestampDefaultValue.getFsp() == bTimestampDefaultValue.getFsp()}}@@ -1030,16 +1032,22 @@ func (n *metadataDiffFunctioNnode) tryMerge(other *metadataDiffFunctioNnode) (boreturn false, ""}if n.action == diffActionCreate {- if n.head.Definition != other.head.Definition {- return true, fmt.Sprintf("conflict function definition, one is %s, the other is %s", n.head.Definition, other.head.Definition)+ nHeadDefinition := strings.TrimRight(n.head.Definition, ";")+ otherHeadDefinition := strings.TrimRight(other.head.Definition, ";")+ if nHeadDefinition != otherHeadDefinition {+ return true, fmt.Sprintf("conflict function definition, one is %s, the other is %s", nHeadDefinition, otherHeadDefinition)}}if n.action == diffActionUpdate {- if other.base.Definition != other.head.Definition {- if n.base.Definition != n.head.Definition {- if n.head.Definition != other.head.Definition {- return true, fmt.Sprintf("conflict function definition, one is %s, the other is %s", n.head.Definition, other.head.Definition)+ otherBaseDefinition := strings.TrimRight(other.base.Definition, ";")+ otherHeadDefinition := strings.TrimRight(other.head.Definition, ";")+ if otherBaseDefinition != otherHeadDefinition {+ nHeadDefinition := strings.TrimRight(n.head.Definition, ";")+ nBaseDefinition := strings.TrimRight(n.base.Definition, ";")+ if nHeadDefinition != nBaseDefinition {+ if nHeadDefinition != otherHeadDefinition {+ return true, fmt.Sprintf("conflict function definition, one is %s, the other is %s", nHeadDefinition, otherHeadDefinition)}} else {n.head.Definition = other.head.Definition@@ -1761,37 +1769,44 @@ func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNod}}- basePartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ partitionNamesMap := make(map[string]bool)+ var partitionNameSlice []string++ basePartitionMap := make(map[string]int)if base != nil {- for _, partition := range base.Partitions {- basePartitionMap[partition.Name] = partition+ for idx, partition := range base.Partitions {+ basePartitionMap[partition.Name] = idx+ if _, ok := partitionNamesMap[partition.Name]; !ok {+ partitionNameSlice = append(partitionNameSlice, partition.Name)+ }+ partitionNamesMap[partition.Name] = true}}- headPartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ headPartitionMap := make(map[string]int)if head != nil {- for _, partition := range head.Partitions {- headPartitionMap[partition.Name] = partition+ for idx, partition := range head.Partitions {+ headPartitionMap[partition.Name] = idx+ if _, ok := partitionNamesMap[partition.Name]; !ok {+ partitionNameSlice = append(partitionNameSlice, partition.Name)+ }+ partitionNamesMap[partition.Name] = true}}- partitionNamesMap := make(map[string]bool)- var partitionNamesSlice []string-- for partitionName := range basePartitionMap {- partitionNamesMap[partitionName] = true- partitionNamesSlice = append(partitionNamesSlice, partitionName)- }-- for partitionName := range headPartitionMap {- if _, ok := partitionNamesMap[partitionName]; !ok {- partitionNamesSlice = append(partitionNamesSlice, partitionName)+ for _, partitionName := range partitionNameSlice {+ basePartitionIdx, basePartitionOk := basePartitionMap[partitionName]+ headPartitionIdx, headPartitionOk := headPartitionMap[partitionName]+ var basePartition, headPartition *storepb.TablePartitionMetadata+ if basePartitionOk {+ basePartition = base.Partitions[basePartitionIdx]+ }+ if headPartitionOk {+ headPartition = head.Partitions[headPartitionIdx]+ }+ if basePartitionIdx != 0 {+ basePartition = base.Partitions[basePartitionIdx]}- partitionNamesMap[partitionName] = true- }-- for _, partitionName := range partitionNamesSlice {- basePartition, headPartition := basePartitionMap[partitionName], headPartitionMap[partitionName]diffNode, err := diffPartitionMetadata(basePartition, headPartition)if err != nil {return nil, errors.Wrapf(err, "failed to diff partition %q", partitionName)@@ -1803,7 +1818,7 @@ func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNod}if action == diffActionUpdate {- if len(tableNode.columnsMap) == 0 && len(tableNode.foreignKeys) == 0 && len(tableNode.indexes) == 0 {+ if len(tableNode.columnsMap) == 0 && len(tableNode.foreignKeys) == 0 && len(tableNode.indexes) == 0 && len(tableNode.partitionsMap) == 0 {return nil, nil}}diff --git a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml b/backend/api/v1/testdata/schema_design_merge/try_merge.yamlindex e4d05a6a27..dbadb26d6b 100644--- a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml+++ b/backend/api/v1/testdata/schema_design_merge/try_merge.yaml@@ -386,6 +386,30 @@"type": "RANGE","expression": "`id`","value": "10 "+ },+ {+ "name": "p2",+ "type": "RANGE",+ "expression": "`id`",+ "value": "20 "+ },+ {+ "name": "p3",+ "type": "RANGE",+ "expression": "`id`",+ "value": "30 "+ },+ {+ "name": "p4",+ "type": "RANGE",+ "expression": "`id`",+ "value": "40 "+ },+ {+ "name": "p5",+ "type": "RANGE",+ "expression": "`id`",+ "value": "50 "}]}diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.goindex ef16a9089a..e6297dfdb1 100644--- a/backend/plugin/db/mysql/sync.go+++ b/backend/plugin/db/mysql/sync.go@@ -666,6 +666,17 @@ func (driver *Driver) getCreateFunctionStmt(ctx context.Context, databaseName, freturn "", err}+ functionSymbolIdx := strings.Index(resultRow.CreateFunction, " FUNCTION ")+ if functionSymbolIdx >= 0 {+ resultRow.CreateFunction = fmt.Sprintf("CREATE%s", resultRow.CreateFunction[functionSymbolIdx:])+ }++ if charsetIdx := strings.Index(resultRow.CreateFunction, " CHARSET "); charsetIdx != -1 {+ if newLineIdx := strings.Index(resultRow.CreateFunction, "\n"); newLineIdx != -1 {+ resultRow.CreateFunction = resultRow.CreateFunction[:charsetIdx] + resultRow.CreateFunction[newLineIdx:]+ }+ }+return resultRow.CreateFunction, nil}@@ -717,6 +728,11 @@ func (driver *Driver) getCreateProcedureStmt(ctx context.Context, databaseName,return "", err}+ procedureSymbolIdx := strings.Index(resultRow.CreateProcedure, " PROCEDURE ")+ if procedureSymbolIdx >= 0 {+ resultRow.CreateProcedure = fmt.Sprintf("CREATE%s", resultRow.CreateProcedure[procedureSymbolIdx:])+ }+return resultRow.CreateProcedure, nil}diff --git a/backend/plugin/schema/mysql/state.go b/backend/plugin/schema/mysql/state.goindex a0827a4db6..6cea7bdf77 100644--- a/backend/plugin/schema/mysql/state.go+++ b/backend/plugin/schema/mysql/state.go@@ -503,6 +503,7 @@ func convertToProcedureState(id int, procedure *storepb.ProcedureMetadata) *procfunc (f *procedureState) toString(buf io.StringWriter) error {def := f.definitionif !strings.HasSuffix(def, " ;;") {+ def = strings.TrimRight(def, ";")def += " ;;"}@@ -537,6 +538,7 @@ func convertToFunctionState(id int, function *storepb.FunctionMetadata) *functiofunc (f *functionState) toString(buf io.StringWriter) error {def := f.definitionif !strings.HasSuffix(def, " ;;") {+ def = strings.TrimRight(def, ";")def += " ;;"}diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex feb4de70b9..727219bb29 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -557,7 +557,7 @@ func buildTablesMetadata(table *storepb.TableMetadata) ([]*TableMetadata, []striname = append(name, table.Name)if table.Partitions != nil {- partitionTables, partitionNames := buildTablesMetadataRecursive(table.Columns, table.Partitions, tableMetadata)+ partitionTables, partitionNames := buildTablesMetadataRecursive(table.Columns, table.Partitions, tableMetadata, table)result = append(result, partitionTables...)name = append(name, partitionNames...)}@@ -566,7 +566,7 @@ func buildTablesMetadata(table *storepb.TableMetadata) ([]*TableMetadata, []stri// buildTablesMetadataRecursive builds the partition tables recursively,// returns the table metadata and the partition names, the length of them must be the same.-func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, partitions []*storepb.TablePartitionMetadata, root *TableMetadata) ([]*TableMetadata, []string) {+func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, partitions []*storepb.TablePartitionMetadata, root *TableMetadata, proto *storepb.TableMetadata) ([]*TableMetadata, []string) {if partitions == nil {return nil, nil}@@ -578,6 +578,7 @@ func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, partpartitionMetadata := &TableMetadata{partitionOf: root,internal: make(map[string]*storepb.ColumnMetadata),+ proto: proto,}for _, column := range originalColumn {partitionMetadata.internal[column.Name] = column@@ -586,7 +587,7 @@ func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, parttables = append(tables, partitionMetadata)names = append(names, partition.Name)if partition.Subpartitions != nil {- subTables, subNames := buildTablesMetadataRecursive(originalColumn, partition.Subpartitions, partitionMetadata)+ subTables, subNames := buildTablesMetadataRecursive(originalColumn, partition.Subpartitions, partitionMetadata, proto)tables = append(tables, subTables...)names = append(names, subNames...)}diff --git a/backend/utils/database_config_merge.go b/backend/utils/database_config_merge.goindex fcc49c4890..57142298d3 100644--- a/backend/utils/database_config_merge.go+++ b/backend/utils/database_config_merge.go@@ -136,7 +136,7 @@ func mergeSchemaConfig(target, baseline, current *storepb.SchemaConfig) *storepb}func mergeFunctionConfig(target, baseline, current *storepb.FunctionConfig) *storepb.FunctionConfig {- lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.Updater, target.UpdateTime, target.SourceBranch, baseline.Updater, current.Updater, current.UpdateTime, current.SourceBranch)+ lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.GetUpdater(), target.GetUpdateTime(), target.GetSourceBranch(), baseline.GetUpdater(), current.GetUpdater(), current.GetUpdateTime(), current.GetSourceBranch())return &storepb.FunctionConfig{Name: current.Name,Updater: lastUpdater,@@ -146,7 +146,7 @@ func mergeFunctionConfig(target, baseline, current *storepb.FunctionConfig) *sto}func mergeProcedureConfig(target, baseline, current *storepb.ProcedureConfig) *storepb.ProcedureConfig {- lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.Updater, target.UpdateTime, target.SourceBranch, baseline.Updater, current.Updater, current.UpdateTime, current.SourceBranch)+ lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.GetUpdater(), target.GetUpdateTime(), target.GetSourceBranch(), baseline.GetUpdater(), current.GetUpdater(), current.GetUpdateTime(), current.GetSourceBranch())return &storepb.ProcedureConfig{Name: current.Name,Updater: lastUpdater,@@ -156,7 +156,7 @@ func mergeProcedureConfig(target, baseline, current *storepb.ProcedureConfig) *s}func mergeViewConfig(target, baseline, current *storepb.ViewConfig) *storepb.ViewConfig {- lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.Updater, target.UpdateTime, target.SourceBranch, baseline.Updater, current.Updater, current.UpdateTime, current.SourceBranch)+ lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.GetUpdater(), target.GetUpdateTime(), target.GetSourceBranch(), baseline.GetUpdater(), current.GetUpdater(), current.GetUpdateTime(), current.GetSourceBranch())return &storepb.ViewConfig{Name: current.Name,Updater: lastUpdater,@@ -197,7 +197,7 @@ func mergeTableConfig(target, baseline, current *storepb.TableConfig) *storepb.T}result := &storepb.TableConfig{Name: current.Name, ClassificationId: current.ClassificationId}- lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.Updater, target.UpdateTime, target.SourceBranch, baseline.Updater, current.Updater, current.UpdateTime, current.SourceBranch)+ lastUpdater, lastUpdateTime, sourceBranch := getLastUpdaterAndSourceBranch(target.GetUpdater(), target.GetUpdateTime(), target.GetSourceBranch(), baseline.GetUpdater(), current.GetUpdater(), current.GetUpdateTime(), current.GetSourceBranch())result.Updater = lastUpdaterresult.UpdateTime = lastUpdateTimeresult.SourceBranch = sourceBranchdiff --git a/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue b/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vueindex f02f91fc2b..4987ac60c8 100644--- a/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue+++ b/frontend/src/components/SchemaEditorLite/Modals/FunctionNameModal.vue@@ -98,9 +98,8 @@ const handleConfirmButtonClick = async () => {const func = FunctionMetadata.fromPartial({name: state.functionName,definition: [- "CREATE FUNCTION `" +- state.functionName +- "` ( ... ) RETURNS ... DETERMINISTIC",+ "CREATE FUNCTION `" + state.functionName + "`(...) RETURNS ...",+ " DETERMINISTIC","BEGIN"," ...","END",diff --git a/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue b/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vueindex d5a751ea39..5f6ba90854 100644--- a/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue+++ b/frontend/src/components/SchemaEditorLite/Modals/ProcedureNameModal.vue@@ -98,7 +98,7 @@ const handleConfirmButtonClick = async () => {const procedure = ProcedureMetadata.fromPartial({name: state.procedureName,definition: [- "CREATE PROCEDURE `" + state.procedureName + "` ( ... )",+ "CREATE PROCEDURE `" + state.procedureName + "`(...)","BEGIN"," ...","END",