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: considering mysql synonymous type in tryMerge (#12525)
h3n4lf70bc7601c54
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.goindex f500bfe8a7..1a15f176ed 100644--- a/backend/api/v1/branch_service.go+++ b/backend/api/v1/branch_service.go@@ -455,12 +455,12 @@ func (s *BranchService) MergeBranch(ctx context.Context, request *v1pb.MergeBransanitizeCommentForSchemaMetadata(headBranch.Head.Metadata, model.NewDatabaseConfig(headBranch.Head.DatabaseConfig), classificationConfig.ClassificationFromConfig)sanitizeCommentForSchemaMetadata(baseBranch.Head.Metadata, model.NewDatabaseConfig(baseBranch.Head.DatabaseConfig), classificationConfig.ClassificationFromConfig)- adHead, err := tryMerge(headBranch.Base.Metadata, headBranch.Head.Metadata, baseBranch.Head.Metadata)+ adHead, err := tryMerge(headBranch.Base.Metadata, headBranch.Head.Metadata, baseBranch.Head.Metadata, baseBranch.Engine)if err != nil {slog.Info("cannot merge branches", log.BBError(err))return nil, status.Errorf(codes.Aborted, "cannot merge branches without conflict, error: %v", err)}- mergedMetadata, err := tryMerge(baseBranch.Head.Metadata, adHead, baseBranch.Head.Metadata)+ mergedMetadata, err := tryMerge(baseBranch.Head.Metadata, adHead, baseBranch.Head.Metadata, baseBranch.Engine)if err != nil {slog.Info("cannot merge branches", log.BBError(err))return nil, status.Errorf(codes.Aborted, "cannot merge branches without conflict, error: %v", err)@@ -581,7 +581,7 @@ func (s *BranchService) RebaseBranch(ctx context.Context, request *v1pb.RebaseBrsanitizeCommentForSchemaMetadata(newHeadMetadata, modelNewHeadConfig, classificationConfig.ClassificationFromConfig)reconcileMetadata(newHeadMetadata, baseBranch.Engine)} else {- newHeadMetadata, err = tryMerge(baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata)+ newHeadMetadata, err = tryMerge(baseBranch.Base.Metadata, baseBranch.Head.Metadata, filteredNewBaseMetadata, baseBranch.Engine)if err != nil {slog.Info("cannot rebase branches", log.BBError(err))conflictSchema, err := diff3.Merge(diff --git a/backend/api/v1/schema_design_merge.go b/backend/api/v1/schema_design_merge.goindex 095bec77cf..435ecab30d 100644--- a/backend/api/v1/schema_design_merge.go+++ b/backend/api/v1/schema_design_merge.go@@ -13,6 +13,7 @@ import ("google.golang.org/protobuf/proto""google.golang.org/protobuf/testing/protocmp"+ "github.com/bytebase/bytebase/backend/plugin/db/mysql"storepb "github.com/bytebase/bytebase/proto/generated-go/store")@@ -81,7 +82,7 @@ const ()// tryMerge merges other metadata to current metadata, always returns a non-nil metadata if no error occurs.-func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata) (*storepb.DatabaseSchemaMetadata, error) {+func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata, engine storepb.Engine) (*storepb.DatabaseSchemaMetadata, error) {ancestor, head, base = proto.Clone(ancestor).(*storepb.DatabaseSchemaMetadata), proto.Clone(head).(*storepb.DatabaseSchemaMetadata), proto.Clone(base).(*storepb.DatabaseSchemaMetadata)if ancestor == nil {@@ -98,7 +99,7 @@ func tryMerge(ancestor, head, base *storepb.DatabaseSchemaMetadata) (*storepb.Dareturn nil, errors.Wrap(err, "failed to diff between ancestor and base")}- if conflict, msg := diffBetweenAncestorAndBase.tryMerge(diffBetweenAncestorAndHead); conflict {+ if conflict, msg := diffBetweenAncestorAndBase.tryMerge(diffBetweenAncestorAndHead, engine); conflict {return nil, errors.Errorf("merge conflict: %s", msg)}@@ -118,13 +119,13 @@ type metadataDiffRootNode struct {}// tryMerge merges other root node to current root node, stop and return error if conflict occurs.-func (mr *metadataDiffRootNode) tryMerge(other *metadataDiffRootNode) (bool, string) {+func (mr *metadataDiffRootNode) tryMerge(other *metadataDiffRootNode, engine storepb.Engine) (bool, string) {for _, schema := range mr.schemas {otherSchema, in := other.schemas[schema.name]if !in {continue}- conflict, msg := schema.tryMerge(otherSchema)+ conflict, msg := schema.tryMerge(otherSchema, engine)if conflict {return true, msg}@@ -168,7 +169,7 @@ type metadataDiffSchemaNode struct {// SchemaMetadata contains other object types, likes function, view etc. But we do not support them yet.}-func (n *metadataDiffSchemaNode) tryMerge(other *metadataDiffSchemaNode) (bool, string) {+func (n *metadataDiffSchemaNode) tryMerge(other *metadataDiffSchemaNode, engine storepb.Engine) (bool, string) {if other == nil {return true, "other node check conflict with schema node not be nil"}@@ -199,7 +200,7 @@ func (n *metadataDiffSchemaNode) tryMerge(other *metadataDiffSchemaNode) (bool,if !in {continue}- conflict, msg := tableNode.tryMerge(otherTableNode)+ conflict, msg := tableNode.tryMerge(otherTableNode, engine)if conflict {return true, msg}@@ -386,7 +387,7 @@ type metadataDiffTableNode struct {partitionsMap map[string]*metadataDiffPartitionNode}-func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, string) {+func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode, engine storepb.Engine) (bool, string) {if other == nil {return true, "other node check conflict with table node must not be nil"}@@ -445,7 +446,7 @@ func (n *metadataDiffTableNode) tryMerge(other *metadataDiffTableNode) (bool, stif !in {continue}- conflict, msg := columnNode.tryMerge(otherColumnNode)+ conflict, msg := columnNode.tryMerge(otherColumnNode, engine)if conflict {return true, msg}@@ -648,7 +649,7 @@ type metadataDiffColumnNode struct {head *storepb.ColumnMetadata}-func (n *metadataDiffColumnNode) tryMerge(other *metadataDiffColumnNode) (bool, string) {+func (n *metadataDiffColumnNode) tryMerge(other *metadataDiffColumnNode, engine storepb.Engine) (bool, string) {if other == nil {return true, "other node check conflict with column node must not be nil"}@@ -665,7 +666,7 @@ func (n *metadataDiffColumnNode) tryMerge(other *metadataDiffColumnNode) (bool,}if n.action == diffActionCreate {- if n.head.Type != other.head.Type {+ if !isColumnTypeEqual(n.head.Type, other.head.Type, engine) {return true, fmt.Sprintf("conflict column type, one is %s, the other is %s", n.head.Type, other.head.Type)}if !compareColumnDefaultValue(n.head.DefaultValue, other.head.DefaultValue) {@@ -682,9 +683,9 @@ func (n *metadataDiffColumnNode) tryMerge(other *metadataDiffColumnNode) (bool,}}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 {+ if !isColumnTypeEqual(other.base.Type, other.head.Type, engine) {+ if !isColumnTypeEqual(n.base.Type, n.head.Type, engine) {+ if !isColumnTypeEqual(n.head.Type, other.head.Type, engine) {return true, fmt.Sprintf("conflict column type, one is %s, the other is %s", n.head.Type, other.head.Type)}} else {@@ -1991,3 +1992,14 @@ func diffPartitionMetadata(base, head *storepb.TablePartitionMetadata) (*metadatreturn partitionNode, nil}++func isColumnTypeEqual(a, b string, engine storepb.Engine) bool {+ switch engine {+ case storepb.Engine_MYSQL:+ canonicalA := mysql.GetColumnTypeCanonicalSynonym(a)+ canonicalB := mysql.GetColumnTypeCanonicalSynonym(b)+ return strings.EqualFold(canonicalA, canonicalB)+ default:+ return strings.EqualFold(a, b)+ }+}diff --git a/backend/api/v1/schema_design_merge_test.go b/backend/api/v1/schema_design_merge_test.goindex ea8b94734f..ca310210fe 100644--- a/backend/api/v1/schema_design_merge_test.go+++ b/backend/api/v1/schema_design_merge_test.go@@ -50,7 +50,7 @@ func TestTryMerge(t *testing.T) {baseSchemaMetadata := new(storepb.DatabaseSchemaMetadata)err := common.ProtojsonUnmarshaler.Unmarshal([]byte(tc.Base), baseSchemaMetadata)a.NoErrorf(err, "test case %d: %s", idx, tc.Description)- mergedSchemaMetadata, err := tryMerge(ancestorSchemaMetadata, headSchemaMetadata, baseSchemaMetadata)+ mergedSchemaMetadata, err := tryMerge(ancestorSchemaMetadata, headSchemaMetadata, baseSchemaMetadata, storepb.Engine_MYSQL)a.NoErrorf(err, "test case %d: %s", idx, tc.Description)a.NotNil(mergedSchemaMetadata, "test case %d: %s, mergedSchemaMetadata should not be nil if there is no error", idx, tc.Description)diff --git a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml b/backend/api/v1/testdata/schema_design_merge/try_merge.yamlindex 30c8198952..e4d05a6a27 100644--- a/backend/api/v1/testdata/schema_design_merge/try_merge.yaml+++ b/backend/api/v1/testdata/schema_design_merge/try_merge.yaml@@ -116,45 +116,45 @@}expected: |-{- "schemas": [+ "schemas": [{- "tables": [+ "tables": [{- "name": "t",- "columns": [+ "name": "t",+ "columns": [{- "name": "id",- "position": 1,- "defaultNull": true,- "nullable": true,- "type": "int"+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "int"}],- "engine": "InnoDB",- "collation": "utf8mb4_0900_ai_ci",- "dataSize": "16384"+ "engine": "InnoDB",+ "collation": "utf8mb4_0900_ai_ci",+ "dataSize": "16384"}],- "views": [+ "views": [{- "name": "v1",- "definition": "select `t`.`id` AS `id` from `t`"+ "name": "v1",+ "definition": "select `t`.`id` AS `id` from `t`"}],- "functions": [+ "functions": [{- "name": "CalcIncome2",- "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"+ "name": "CalcIncome2",+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome2`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"},{- "name": "CalcIncome",- "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"+ "name": "CalcIncome",+ "definition": "CREATE DEFINER=`root`@`%` FUNCTION `CalcIncome`( starting_value INT ) RETURNS int(11)\n DETERMINISTIC\nBEGIN\n\n DECLARE income INT;\n\n SET income = 0;\n\n label1: WHILE income <= 3000 DO\n SET income = income + starting_value;\n END WHILE label1;\n\n RETURN income;\n\nEND"}],- "procedures": [+ "procedures": [{- "name": "citycount",- "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"+ "name": "citycount",+ "definition": "CREATE DEFINER=`root`@`%` PROCEDURE `citycount`(IN country CHAR(3), OUT cities INT)\nBEGIN\n SELECT 1000 INTO cities;\n END"}]}@@ -352,40 +352,40 @@}expected: |-{- "schemas": [+ "schemas": [{- "tables": [+ "tables": [{- "name": "employee",- "columns": [+ "name": "employee",+ "columns": [{- "name": "id",- "position": 1,- "defaultNull": true,- "nullable": true,- "type": "INT"+ "name": "id",+ "position": 1,+ "defaultNull": true,+ "nullable": true,+ "type": "INT"}],- "indexes": [+ "indexes": [{- "name": "PRIMARY",- "expressions": [+ "name": "PRIMARY",+ "expressions": ["id"],- "keyLength": [+ "keyLength": ["-1"],- "unique": true,- "primary": true,- "visible": true+ "unique": true,+ "primary": true,+ "visible": true}],- "partitions": [+ "partitions": [{- "name": "p1",- "type": "RANGE",- "expression": "`id`",- "value": "10 "+ "name": "p1",+ "type": "RANGE",+ "expression": "`id`",+ "value": "10 "}]}@@ -725,121 +725,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: |-@@ -920,35 +920,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: |-@@ -1043,39 +1043,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: |-@@ -1163,6 +1163,49 @@"collation": "en_US.utf8"}expected: |-+ {+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "type": "integer"+ }+ ]+ },+ {+ "name": "address",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "type": "integer"+ }+ ]+ },+ {+ "name": "salary",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "type": "integer"+ }+ ]+ }+ ]+ }+ ],+ "characterSet": "UTF8",+ "collation": "en_US.utf8"+ }+- description: no change+ ancestor: |-{"schemas": [{@@ -1177,9 +1220,21 @@"type": "integer"}]- },+ }+ ]+ }+ ],+ "characterSet": "UTF8",+ "collation": "en_US.utf8"+ }+ head: |-+ {+ "schemas": [+ {+ "name": "public",+ "tables": [{- "name": "address",+ "name": "employee","columns": [{"name": "id",@@ -1187,9 +1242,21 @@"type": "integer"}]- },+ }+ ]+ }+ ],+ "characterSet": "UTF8",+ "collation": "en_US.utf8"+ }+ base: |-+ {+ "schemas": [+ {+ "name": "public",+ "tables": [{- "name": "salary",+ "name": "employee","columns": [{"name": "id",@@ -1204,7 +1271,29 @@"characterSet": "UTF8","collation": "en_US.utf8"}-- description: no change+ expected: |-+ {+ "schemas": [+ {+ "name": "public",+ "tables": [+ {+ "name": "employee",+ "columns": [+ {+ "name": "id",+ "position": 1,+ "type": "integer"+ }+ ]+ }+ ]+ }+ ],+ "characterSet": "UTF8",+ "collation": "en_US.utf8"+ }+- description: Integer Synonymsancestor: |-{"schemas": [@@ -1217,7 +1306,7 @@{"name": "id","position": 1,- "type": "integer"+ "type": "int"}]}@@ -1239,7 +1328,7 @@{"name": "id","position": 1,- "type": "integer"+ "type": "int"}]}@@ -1261,7 +1350,7 @@{"name": "id","position": 1,- "type": "integer"+ "type": "int(11)"}]}@@ -1273,23 +1362,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": "int(11)"}]}]}],- "characterSet": "UTF8",- "collation": "en_US.utf8"+ "characterSet": "UTF8",+ "collation": "en_US.utf8"}diff --git a/backend/plugin/db/mysql/synonyms.go b/backend/plugin/db/mysql/synonyms.gonew file mode 100644index 0000000000..b94e19684b--- /dev/null+++ b/backend/plugin/db/mysql/synonyms.go@@ -0,0 +1,32 @@+package mysql++import "strings"++var columnTypeCanonicalSynonyms = map[string]string{+ // Display width default, the display width attribute is deprecated for integer data types, but exist in+ // MySQL 5.7.+ "tinyint(4)": "tinyint",+ "smallint(6)": "smallint",+ "mediumint(9)": "mediumint",+ "int(11)": "int",+ "bigint(20)": "bigint",++ // Numeric Data Type Synonyms.+ "integer": "int",+ "boolean": "tinyint(1)",+ "decimal(10, 0)": "decimal",+ "dec": "decimal",++ // TODO(zp): handle other column type synonyms and considering handling+ // variant display width match, for example, dec(20, 0) are equivalent with decimal(20, 0).+}++// GetColumnTypeCanonicalSynonym returns the canonical synonyms of the given column types,+// returns the original one if no synonym was found.+func GetColumnTypeCanonicalSynonym(columnType string) string {+ if canonical, ok := columnTypeCanonicalSynonyms[strings.ToLower(columnType)]; ok {+ return canonical+ }++ return columnType+}