Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: structural 3-way merge of partition for MySQL (#11190)
h3n4l824ff0c73df1
diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.goindex 5caa3a0ff5..88cc9c99f3 100644--- a/backend/api/v1/schema_design_merge.go+++ b/backend/api/v1/schema_design_merge.go@@ -219,6 +219,12 @@ type metadataDiffTableNode struct {foreignKeys map[string]*metadataDiffForeignKeyNodeindexes map[string]*metadataDiffIndexNode// TableMetaData contains other object types, likes trigger, index etc. But we do not support them yet.++ // partitionNames is designed to help to handle the partition orders.+ // The size of partitionNames is always equal to the size of partitionsMap,+ // and all the value appeared in partitionNames is also the key of partitionsMap.+ partitionNames []string+ partitionsMap map[string]*metadataDiffPartitionNode}func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, string) {@@ -324,6 +330,19 @@ func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, stdelete(other.indexes, indexName)}+ for _, partitionName := range n.partitionNames {+ partitionNode := n.partitionsMap[partitionName]+ otherPartitionNode, in := other.partitionsMap[partitionName]+ if !in {+ continue+ }+ conflict, msg := partitionNode.tryMerge(otherPartitionNode)+ if conflict {+ return true, msg+ }+ delete(other.partitionsMap, partitionName)+ }+for _, columnName := range other.columnNames {// We had deleted the column node which appeared in both table nodes.if columnNode, in := other.columnsMap[columnName]; in {@@ -333,6 +352,15 @@ func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, st}}+ for _, partition := range other.partitionNames {+ // We had deleted the partition node which appeared in both table nodes.+ if partitionNode, in := other.partitionsMap[partition]; in {+ n.partitionsMap[partition] = partitionNode+ n.partitionNames = append(n.partitionNames, partition)+ continue+ }+ }+for _, remainingForeignKey := range other.foreignKeys {n.foreignKeys[remainingForeignKey.name] = remainingForeignKey}@@ -397,6 +425,14 @@ func (n *metadataDiffTableNode) applyDiffTo(target *storepb.SchemaMetadata) erroreturn errors.Wrapf(err, "failed to apply diff to index %q", index.name)}}++ for _, partitionName := range n.partitionNames {+ partition := n.partitionsMap[partitionName]+ if err := partition.applyDiffTo(newTable); err != nil {+ return errors.Wrapf(err, "failed to apply diff to partition %q", partition.name)+ }+ }+target.Tables = append(target.Tables, newTable)case diffActionDrop:for i, table := range target.Tables {@@ -445,6 +481,13 @@ func (n *metadataDiffTableNode) applyDiffTo(target *storepb.SchemaMetadata) errofor idx := range newTable.Columns {newTable.Columns[idx].Position = int32(idx + 1)}++ for _, partitionName := range n.partitionNames {+ partition := n.partitionsMap[partitionName]+ if err := partition.applyDiffTo(newTable); err != nil {+ return errors.Wrapf(err, "failed to apply diff to partition %q", partition.name)+ }+ }target.Tables[idx] = newTable}}@@ -690,6 +733,118 @@ func (n *metadataDiffIndexNode) applyDiffTo(target *storepb.TableMetadata) errorreturn nil}+type metadataDiffPartitionNode struct {+ metadataDiffBaseNode+ name string+ // nolint+ base *storepb.TablePartitionMetadata+ head *storepb.TablePartitionMetadata++ // subpartitionNames is designed to help to handle the subpartition orders.+ subpartitionNames []string+ subpartitions map[string]*metadataDiffPartitionNode+}++func (n *metadataDiffPartitionNode) tryMerge(other *metadataDiffPartitionNode) (bool, string) {+ if other == nil {+ return true, "other node check conflict with partition node must not be nil"+ }++ if n.name != other.name {+ return true, fmt.Sprintf("non-expected partition node pair, one is %s, the other is %s", n.name, other.name)+ }+ if n.action != other.action {+ return true, fmt.Sprintf("conflict partition action, one is %s, the other is %s", n.action, other.action)+ }+ if n.action == diffActionDrop {+ return false, ""+ }+ if n.action == diffActionCreate {+ if n.head.Type != other.head.Type {+ return true, fmt.Sprintf("conflict partition type, one is %s, the other is %s", n.head.Type, other.head.Type)+ }+ if n.head.Expression != other.head.Expression {+ return true, fmt.Sprintf("conflict partition expression, one is %s, the other is %s", n.head.Expression, other.head.Expression)+ }+ if n.head.Value != other.head.Value {+ return true, fmt.Sprintf("conflict partition value, one is %s, the other is %s", n.head.Value, other.head.Value)+ }+ }++ if n.action == diffActionUpdate {+ if other.base.Type != other.head.Type {+ if n.base.Type != n.head.Type {+ if n.head.Type != other.head.Type {+ return true, fmt.Sprintf("conflict partition type, one is %s, the other is %s", n.head.Type, other.head.Type)+ }+ } else {+ n.head.Type = other.head.Type+ }+ }++ if other.base.Expression != other.head.Expression {+ if n.base.Expression != n.head.Expression {+ if n.head.Expression != other.head.Expression {+ return true, fmt.Sprintf("conflict partition expression, one is %s, the other is %s", n.head.Expression, other.head.Expression)+ }+ } else {+ n.head.Expression = other.head.Expression+ }+ }++ if other.base.Value != other.head.Value {+ if n.base.Value != n.head.Value {+ if n.head.Value != other.head.Value {+ return true, fmt.Sprintf("conflict partition value, one is %s, the other is %s", n.head.Value, other.head.Value)+ }+ } else {+ n.head.Value = other.head.Value+ }+ }+ }++ for _, subpartitionName := range n.subpartitionNames {+ subpartitionNode := n.subpartitions[subpartitionName]+ otherSubpartitionNode, in := other.subpartitions[subpartitionName]+ if !in {+ continue+ }+ conflict, msg := subpartitionNode.tryMerge(otherSubpartitionNode)+ if conflict {+ return true, msg+ }+ delete(other.subpartitions, subpartitionName)+ }++ for _, remainingSubpartition := range other.subpartitions {+ n.subpartitions[remainingSubpartition.name] = remainingSubpartition+ }++ return false, ""+}++func (n *metadataDiffPartitionNode) applyDiffTo(target *storepb.TableMetadata) error {+ switch n.action {+ case diffActionCreate:+ target.Partitions = append(target.Partitions, n.head)+ case diffActionDrop:+ for i, partition := range target.Partitions {+ if partition.Name == n.name {+ target.Partitions = append(target.Partitions[:i], target.Partitions[i+1:]...)+ break+ }+ }+ case diffActionUpdate:+ for i, partition := range target.Partitions {+ if partition.Name == n.name {+ target.Partitions[i] = n.head+ break+ }+ }+ }+ return nil+}+// Foreign Key related.type metadataDiffForeignKeyNode struct {metadataDiffBaseNode@@ -936,12 +1091,13 @@ func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNodmetadataDiffBaseNode: metadataDiffBaseNode{action: action,},- name: name,- base: base,- head: head,- columnsMap: make(map[string]*metadataDiffColumnNode),- foreignKeys: make(map[string]*metadataDiffForeignKeyNode),- indexes: make(map[string]*metadataDiffIndexNode),+ name: name,+ base: base,+ head: head,+ columnsMap: make(map[string]*metadataDiffColumnNode),+ foreignKeys: make(map[string]*metadataDiffForeignKeyNode),+ indexes: make(map[string]*metadataDiffIndexNode),+ partitionsMap: make(map[string]*metadataDiffPartitionNode),}columnNamesMap := make(map[string]bool)@@ -1050,6 +1206,47 @@ func diffTableMetadata(base, head *storepb.TableMetadata) (*metadataDiffTableNod}}+ basePartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ if base != nil {+ for _, partition := range base.Partitions {+ basePartitionMap[partition.Name] = partition+ }+ }++ headPartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ if head != nil {+ for _, partition := range head.Partitions {+ headPartitionMap[partition.Name] = partition+ }+ }++ 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)+ }+ 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)+ }+ if diffNode != nil {+ tableNode.partitionsMap[partitionName] = diffNode+ tableNode.partitionNames = append(tableNode.partitionNames, partitionName)+ }+ }+if action == diffActionUpdate {if len(tableNode.columnsMap) == 0 && len(tableNode.foreignKeys) == 0 && len(tableNode.indexes) == 0 {return nil, nil@@ -1163,3 +1360,81 @@ func diffIndexMetadata(base, head *storepb.IndexMetadata) (*metadataDiffIndexNod}return indexNode, nil}++func diffPartitionMetadata(base, head *storepb.TablePartitionMetadata) (*metadataDiffPartitionNode, error) {+ if base == nil && head == nil {+ return nil, errors.New("base and head partition metadata cannot be nil both")+ }++ var name string+ action := diffActionUpdate+ if base == nil {+ action = diffActionCreate+ name = head.Name+ } else if head == nil {+ action = diffActionDrop+ name = base.Name+ } else {+ name = base.Name+ }++ partitionNode := &metadataDiffPartitionNode{+ metadataDiffBaseNode: metadataDiffBaseNode{+ action: action,+ },+ name: name,+ base: base,+ head: head,+ subpartitions: make(map[string]*metadataDiffPartitionNode),+ }++ baseSubpartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ if base != nil {+ for _, partition := range base.Subpartitions {+ baseSubpartitionMap[partition.Name] = partition+ }+ }++ headSubpartitionMap := make(map[string]*storepb.TablePartitionMetadata)+ if head != nil {+ for _, partition := range head.Subpartitions {+ headSubpartitionMap[partition.Name] = partition+ }+ }++ subpartitionNamesMap := make(map[string]bool)+ var subpartitionNamesSlice []string++ for subpartitionName := range baseSubpartitionMap {+ subpartitionNamesMap[subpartitionName] = true+ subpartitionNamesSlice = append(subpartitionNamesSlice, subpartitionName)+ }++ for subpartitionName := range headSubpartitionMap {+ if _, ok := subpartitionNamesMap[subpartitionName]; !ok {+ subpartitionNamesSlice = append(subpartitionNamesSlice, subpartitionName)+ }+ subpartitionNamesMap[subpartitionName] = true+ }++ for _, subpartitionName := range subpartitionNamesSlice {+ baseSubpartition, headSubpartition := baseSubpartitionMap[subpartitionName], headSubpartitionMap[subpartitionName]+ diffNode, err := diffPartitionMetadata(baseSubpartition, headSubpartition)+ if err != nil {+ return nil, errors.Wrapf(err, "failed to diff subpartition %q", subpartitionName)+ }+ if diffNode != nil {+ partitionNode.subpartitions[subpartitionName] = diffNode+ partitionNode.subpartitionNames = append(partitionNode.subpartitionNames, subpartitionName)+ }+ }++ if action == diffActionUpdate {+ if len(partitionNode.subpartitions) > 0 || !proto.Equal(base, head) {+ return partitionNode, nil+ }+ return nil, nil+ }++ return partitionNode, 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 45271c31d3..c5bd159789 100644--- a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml+++ b/backend/api/v1/testdata/schema_design_merge/try_merge.yaml@@ -1,3 +1,236 @@+- description: |-+ Add Partition:+ Head:+ - Table `employee`:+ - ADD PARTITION `p2` VALUES LESS THAN (20).+ - ADD PARTITION `p3` VALUES LESS THAN (30).+ - ADD PARTITION `p4` VALUES LESS THAN (40).+ Base:+ - Table `employee`:+ - ADD PARTITION `p2` VALUES LESS THAN (20).+ - ADD PARTITION `p3` VALUES LESS THAN (30).+ - ADD PARTITION `p4` VALUES LESS THAN (40).+ - ADD PARTITION `p5` VALUES LESS THAN (50).+ What we expect:+ - Table `employee`:+ - ADD PARTITION `p2` VALUES LESS THAN (20).+ - ADD PARTITION `p3` VALUES LESS THAN (30).+ - ADD PARTITION `p4` VALUES LESS THAN (40).+ - ADD PARTITION `p5` VALUES LESS THAN (50).+ ancestor: |-+ {+ "schemas": [+ {+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "INT"+ }+ ],+ "indexes": [+ {+ "name": "PRIMARY",+ "expressions": [+ "id"+ ],+ "keyLength": [+ "-1"+ ],+ "unique": true,+ "primary": true,+ "visible": true+ }+ ],+ "partitions": [+ {+ "name": "p1",+ "type": "RANGE",+ "expression": "`id`",+ "value": "10 "+ }+ ]+ }+ ]+ }+ ]+ }+ head: |-+ {+ "schemas": [+ {+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "INT"+ }+ ],+ "indexes": [+ {+ "name": "PRIMARY",+ "expressions": [+ "id"+ ],+ "keyLength": [+ "-1"+ ],+ "unique": true,+ "primary": true,+ "visible": true+ }+ ],+ "partitions": [+ {+ "name": "p1",+ "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 "+ }+ ]+ }+ ]+ }+ ]+ }+ base: |-+ {+ "schemas": [+ {+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "INT"+ }+ ],+ "indexes": [+ {+ "name": "PRIMARY",+ "expressions": [+ "id"+ ],+ "keyLength": [+ "-1"+ ],+ "unique": true,+ "primary": true,+ "visible": true+ }+ ],+ "partitions": [+ {+ "name": "p1",+ "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 "+ }+ ]+ }+ ]+ }+ ]+ }+ expected: |-+ {+ "schemas": [+ {+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "INT"+ }+ ],+ "indexes": [+ {+ "name": "PRIMARY",+ "expressions": [+ "id"+ ],+ "keyLength": [+ "-1"+ ],+ "unique": true,+ "primary": true,+ "visible": true+ }+ ],+ "partitions": [+ {+ "name": "p1",+ "type": "RANGE",+ "expression": "`id`",+ "value": "10 "+ }+ ]+ }+ ]+ }+ ]+ }- description: |-Complex Case:Head:@@ -330,121 +563,121 @@}expected: |-{- "schemas": [+ "schemas": [{- "name": "public",- "tables": [+ "name": "public",+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"},{- "name": "c",- "position": 2,- "type": "int"+ "name": "c",+ "position": 2,+ "type": "int"},{- "name": "gender",- "position": 3,- "type": "char(1)"+ "name": "gender",+ "position": 3,+ "type": "char(1)"},{- "name": "debit_card_number",- "position": 4,- "type": "varchar(255)"+ "name": "debit_card_number",+ "position": 4,+ "type": "varchar(255)"},{- "name": "credit_card_number",- "position": 5,- "type": "varchar(255)"+ "name": "credit_card_number",+ "position": 5,+ "type": "varchar(255)"}],- "indexes": [+ "indexes": [{- "name": "PRIMARY",- "expressions": [+ "name": "PRIMARY",+ "expressions": ["id"],- "type": "btree",- "unique": true,- "primary": true+ "type": "btree",+ "unique": true,+ "primary": true},{- "name": "t_c_plus_5_uniq_idx",- "expressions": [+ "name": "t_c_plus_5_uniq_idx",+ "expressions": ["((`c` + 5))"],- "unique": true,- "visible": true+ "unique": true,+ "visible": true}]},{- "name": "address",- "columns": [+ "name": "address",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"},{- "name": "address",- "position": 2,- "type": "varchar(255)"+ "name": "address",+ "position": 2,+ "type": "varchar(255)"},{- "name": "employee_id",- "position": 3,- "type": "integer"+ "name": "employee_id",+ "position": 3,+ "type": "integer"}],- "indexes": [+ "indexes": [{- "name": "PRIMARY",- "expressions": [+ "name": "PRIMARY",+ "expressions": ["id"],- "primary": true+ "primary": true}],- "foreignKeys": [+ "foreignKeys": [{- "name": "address-fk-sc4rcmkp",- "columns": [+ "name": "address-fk-sc4rcmkp",+ "columns": ["employee_id"],- "referencedSchema": "public",- "referencedTable": "employee",- "referencedColumns": [+ "referencedSchema": "public",+ "referencedTable": "employee",+ "referencedColumns": ["id"]}]},{- "name": "hiring_date",- "columns": [+ "name": "hiring_date",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"},{- "name": "employee_id",- "position": 2,- "type": "integer"+ "name": "employee_id",+ "position": 2,+ "type": "integer"}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}- description: Add different column on the same tableancestor: |-@@ -525,35 +758,35 @@}expected: |-{- "schemas": [+ "schemas": [{- "name": "public",- "tables": [+ "name": "public",+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"},{- "name": "gender",- "position": 2,- "type": "char(1)"+ "name": "gender",+ "position": 2,+ "type": "char(1)"},{- "name": "name",- "position": 3,- "type": "varchar(255)"+ "name": "name",+ "position": 3,+ "type": "varchar(255)"}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}- description: Add index in baseancestor: |-@@ -648,39 +881,39 @@}expected: |-{- "schemas": [+ "schemas": [{- "name": "public",- "tables": [+ "name": "public",+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"},{- "name": "name",- "position": 2,- "type": "text"+ "name": "name",+ "position": 2,+ "type": "text"}],- "indexes": [+ "indexes": [{- "name": "address_name_idx",- "expressions": [+ "name": "address_name_idx",+ "expressions": ["name"],- "visible": true+ "visible": true}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}- description: Create different tableancestor: |-@@ -769,45 +1002,45 @@}expected: |-{- "schemas": [+ "schemas": [{- "name": "public",- "tables": [+ "name": "public",+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"}]},{- "name": "address",- "columns": [+ "name": "address",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"}]},{- "name": "salary",- "columns": [+ "name": "salary",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}- description: no changeancestor: |-@@ -878,23 +1111,23 @@}expected: |-{- "schemas": [+ "schemas": [{- "name": "public",- "tables": [+ "name": "public",+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "type": "integer"+ "name": "id",+ "position": 1,+ "type": "integer"}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}