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(branch): get type string by restoring for tidb (#9722)
rebelicefa0a5e279c41
diff --git a/backend/api/v1/schema_design_transformer_tidb.go b/backend/api/v1/schema_design_transformer_tidb.goindex 199b08f91d..6ffaa723d8 100644--- a/backend/api/v1/schema_design_transformer_tidb.go+++ b/backend/api/v1/schema_design_transformer_tidb.go@@ -2,8 +2,8 @@ package v1import ("fmt"+ "log/slog""sort"- "strconv""strings""github.com/pkg/errors"@@ -200,14 +200,12 @@ func columnTypeStr(tp *tidbtypes.FieldType) string {// tp.String() return bigint(20)return "bigint"default:- str := tp.String()- if strings.Contains(str, "binary") {- tp.SetFlag(tidbmysql.BinaryFlag)- tp.SetCharset("binary")- tp.SetCollate("binary")- return tp.CompactStr()- }- return tp.String()+ text, err := tidbRestoreFieldType(tp)+ if err != nil {+ slog.Debug("tidbRestoreFieldType failed", "err", err, "type", tp.String())+ return tp.String()+ }+ return text}}@@ -266,9 +264,6 @@ func tidbRestoreNodeDefault(node tidbast.Node) (string, error) {}func tidbRestoreFieldType(fieldType *tidbtypes.FieldType) (string, error) {- if strings.Contains(fieldType.String(), "binary") {- return fieldType.CompactStr(), nil- }var buffer strings.Builder// we want to use Default format flags but with lowercase keyword.flag := tidbformat.RestoreKeyWordLowercase | tidbformat.RestoreStringSingleQuotes | tidbformat.RestoreNameBackQuotes@@ -468,14 +463,17 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)// Compare column types.dataType := columnTypeStr(column.Tp)if !strings.EqualFold(dataType, stateColumn.tp) {- // write lower case column type for tidb- column.Tp = tidbNewFieldType(stateColumn.tp)- }- if typeStr, err := tidbRestoreFieldType(column.Tp); err == nil {- if _, err := g.columnDefine.WriteString(typeStr); err != nil {+ if _, err := g.columnDefine.WriteString(stateColumn.tp); err != nil {g.err = errreturn in, true}+ } else {+ if typeStr, err := tidbRestoreFieldType(column.Tp); err == nil {+ if _, err := g.columnDefine.WriteString(typeStr); err != nil {+ g.err = err+ return in, true+ }+ }}// Column attributes.@@ -946,45 +944,3 @@ func tidbGetAttrOrder(option *tidbast.ColumnOption) int {}return len(columnAttrOrder) + 1}--func tidbNewFieldType(tp string) *tidbtypes.FieldType {- tpStr := strings.ToLower(tp)- var s []byte- var flen []byte- var decimal []byte- stage := 1- for i := 0; i < len(tpStr); i++ {- if tpStr[i] == '(' {- stage = 2- continue- } else if tpStr[i] == ',' {- stage = 3- continue- } else if tpStr[i] == ')' {- continue- }-- if stage == 1 {- s = append(s, tpStr[i])- } else if stage == 2 {- flen = append(flen, tpStr[i])- } else if stage == 3 {- decimal = append(decimal, tpStr[i])- }- }- ft := tidbtypes.NewFieldType(tidbtypes.StrToType(string(s)))- flenInt, _ := strconv.Atoi(string(flen))- if flenInt > 0 {- ft.SetFlen(flenInt)- }- decimalInt, _ := strconv.Atoi(string(decimal))- if decimalInt > 0 {- ft.SetDecimal(decimalInt)- }- if strings.Contains(tpStr, "binary") {- ft.SetFlag(tidbmysql.BinaryFlag)- ft.SetCharset("binary")- ft.SetCollate("binary")- }- return ft-}diff --git a/backend/api/v1/testdata/tidb/design.yaml b/backend/api/v1/testdata/tidb/design.yamlindex cde23e6bba..4209376c6a 100644--- a/backend/api/v1/testdata/tidb/design.yaml+++ b/backend/api/v1/testdata/tidb/design.yaml@@ -14,6 +14,7 @@`d` int,`e` char(10) NOT NULL DEFAULT 'NULL' comment 'this is comment',`f` int comment 'this is comment',+ `g` blob,PRIMARY KEY (`a`, `c`, `d`),CONSTRAINT fk1 FOREIGN KEY (`a`) REFERENCES `t2` (`b`));@@ -44,6 +45,11 @@"nullable": true,"type": "enum('ROW','MIXED','STATEMENT','')"},+ {+ "name": "x1",+ "nullable": true,+ "type": "blob"+ },{"name": "c1","nullable": true,@@ -126,6 +132,11 @@"name": "f","type": "int","nullable": true+ },+ {+ "name": "g",+ "type": "blob",+ "nullable": true}],"indexes": [@@ -205,6 +216,7 @@`c` int NOT NULL DEFAULT 1 COMMENT 'col_b',`e` int NULL DEFAULT 1 COMMENT 'col_e',`f` int,+ `g` blob,`b` char(10) NOT NULL,PRIMARY KEY (`a`, `b`, `c`),CONSTRAINT `fk1` FOREIGN KEY (`b`) REFERENCES `t2` (`b`)@@ -218,7 +230,8 @@CREATE TABLE `rds` (`id` bigint(20) NOT NULL COMMENT 'ID',`binlog_format` enum('ROW','MIXED','STATEMENT',''),- `c1` int AUTO_INCREMENT+ `c1` int AUTO_INCREMENT,+ `x1` blob NULL) COMMENT 'hello';CREATE TABLE `t2` (diff --git a/backend/api/v1/testdata/tidb/schema.yaml b/backend/api/v1/testdata/tidb/schema.yamlindex 630f15590f..cec3dbd48c 100644--- a/backend/api/v1/testdata/tidb/schema.yaml+++ b/backend/api/v1/testdata/tidb/schema.yaml@@ -7,91 +7,127 @@b varchar(20) default NULL comment '',d varbinary(500),e binary(20),+ f blob,+ g tinyblob,+ h mediumblob,+ i longblob,+ j text,+ k tinytext,primary key (a, b),constraint fk1 foreign key (a) references t2 (b));create table t2(b int NOT NULL default NULL, primary key (b));metadata: |-{- "schemas": [+ "schemas": [{- "tables": [+ "tables": [{- "name": "t",- "columns": [+ "name": "t",+ "columns": [{- "name": "c",- "nullable": true,- "type": "int"+ "name": "c",+ "nullable": true,+ "type": "int"},{- "name": "a",- "defaultExpression": "1",- "nullable": true,- "type": "int",- "comment": "abcdefg",- "userComment": "abcdefg"+ "name": "a",+ "defaultExpression": "1",+ "nullable": true,+ "type": "int",+ "comment": "abcdefg",+ "userComment": "abcdefg"},{- "name": "b",- "defaultNull": true,- "nullable": true,- "type": "varchar(20)"+ "name": "b",+ "defaultNull": true,+ "nullable": true,+ "type": "varchar(20)"},{- "name": "d",- "nullable": true,- "type": "varbinary(500)"+ "name": "d",+ "nullable": true,+ "type": "varbinary(500)"},{- "name": "e",- "nullable": true,- "type": "binary(20)"+ "name": "e",+ "nullable": true,+ "type": "binary(20)"+ },+ {+ "name": "f",+ "nullable": true,+ "type": "blob"+ },+ {+ "name": "g",+ "nullable": true,+ "type": "tinyblob"+ },+ {+ "name": "h",+ "nullable": true,+ "type": "mediumblob"+ },+ {+ "name": "i",+ "nullable": true,+ "type": "longblob"+ },+ {+ "name": "j",+ "nullable": true,+ "type": "text"+ },+ {+ "name": "k",+ "nullable": true,+ "type": "tinytext"}],- "indexes": [+ "indexes": [{- "name": "PRIMARY",- "expressions": [+ "name": "PRIMARY",+ "expressions": ["a","b"],- "unique": true,- "primary": true,- "visible": true+ "unique": true,+ "primary": true,+ "visible": true}],- "foreignKeys": [+ "foreignKeys": [{- "name": "fk1",- "columns": [+ "name": "fk1",+ "columns": ["a"],- "referencedTable": "t2",- "referencedColumns": [+ "referencedTable": "t2",+ "referencedColumns": ["b"]}]},{- "name": "t2",- "columns": [+ "name": "t2",+ "columns": [{- "name": "b",- "defaultNull": true,- "type": "int"+ "name": "b",+ "defaultNull": true,+ "type": "int"}],- "indexes": [+ "indexes": [{- "name": "PRIMARY",- "expressions": [+ "name": "PRIMARY",+ "expressions": ["b"],- "unique": true,- "primary": true,- "visible": true+ "unique": true,+ "primary": true,+ "visible": true}]}