Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(branch): deal with key length (#10450)

rebelice2b43922c349f
diff --git a/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml b/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
index cde14432b9..eace6796b0 100644
--- a/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
+++ b/backend/plugin/parser/tidb/test-data/test_strings_manipulate.yaml
@@ -3,7 +3,8 @@
CREATE TABLE `t` (
`id` int /*T![auto_rand] AUTO_RANDOM(6, 54) */,
`name` varchar(10) NOT NULL,
- PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
+ PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
+ KEY `idx_name` (`name`(5))
);
actions:
- type: modifyColumnType
@@ -16,7 +17,8 @@
CREATE TABLE `t` (
`id` varchar(10) /*T![auto_rand] AUTO_RANDOM(6, 54) */,
`name` varchar(10) NOT NULL,
- PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
+ PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
+ KEY `idx_name` (`name`(5))
);
- input: |-
-- Table structure for `t`
diff --git a/backend/plugin/schema/tidb/get_design_schema.go b/backend/plugin/schema/tidb/get_design_schema.go
index 0a0e8fdaee..eaa2ca9ce0 100644
--- a/backend/plugin/schema/tidb/get_design_schema.go
+++ b/backend/plugin/schema/tidb/get_design_schema.go
@@ -235,7 +235,7 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
for _, key := range constraint.Keys {
keys = append(keys, key.Column.Name.String())
}
- if equalKeys(keys, g.currentTable.indexes["PRIMARY"].keys) {
+ if equalKeys(keys, nil /* length */, g.currentTable.indexes["PRIMARY"].keys, nil /* length */) {
delete(g.currentTable.indexes, "PRIMARY")
continue
}
@@ -259,13 +259,16 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
index := g.currentTable.indexes[indexName]
var columns []string
+ var length []int64
for _, key := range constraint.Keys {
var keyString string
var err error
if key.Column != nil {
keyString = key.Column.Name.String()
if key.Length > 0 {
- keyString = fmt.Sprintf("`%s`(%d)", keyString, key.Length)
+ length = append(length, int64(key.Length))
+ } else {
+ length = append(length, -1)
}
} else {
keyString, err = tidbRestoreNode(key, tidbformat.RestoreKeyWordLowercase|tidbformat.RestoreStringSingleQuotes|tidbformat.RestoreNameBackQuotes)
@@ -277,7 +280,7 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
columns = append(columns, keyString)
}
- if equalKeys(columns, index.keys) && !index.unique && !index.primary {
+ if equalKeys(columns, length, index.keys, index.length) && !index.unique && !index.primary {
delete(g.currentTable.indexes, indexName)
continue
}
@@ -301,13 +304,16 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
index := g.currentTable.indexes[indexName]
var columns []string
+ var length []int64
for _, key := range constraint.Keys {
var keyString string
var err error
if key.Column != nil {
keyString = key.Column.Name.String()
if key.Length > 0 {
- keyString = fmt.Sprintf("`%s`(%d)", keyString, key.Length)
+ length = append(length, int64(key.Length))
+ } else {
+ length = append(length, -1)
}
} else {
keyString, err = tidbRestoreNode(key, tidbformat.RestoreKeyWordLowercase|tidbformat.RestoreStringSingleQuotes|tidbformat.RestoreNameBackQuotes)
@@ -319,7 +325,7 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
columns = append(columns, keyString)
}
- if equalKeys(columns, index.keys) && index.unique && !index.primary {
+ if equalKeys(columns, length, index.keys, index.length) && index.unique && !index.primary {
delete(g.currentTable.indexes, indexName)
continue
}
@@ -352,7 +358,8 @@ func (g *tidbDesignSchemaGenerator) Enter(in tidbast.Node) (tidbast.Node, bool)
referencedColumnList = append(referencedColumnList, spec.Column.Name.String())
}
referencedTable := constraint.Refer.Table.Name.String()
- if equalKeys(columns, fk.columns) && referencedTable == fk.referencedTable && equalKeys(referencedColumnList, fk.referencedColumns) {
+ if equalKeys(columns, nil /* length */, fk.columns, nil /* length */) && referencedTable == fk.referencedTable &&
+ equalKeys(referencedColumnList, nil /* length */, fk.referencedColumns, nil /* length */) {
delete(g.currentTable.foreignKeys, fkName)
continue
}
diff --git a/backend/plugin/schema/tidb/parse_to_metadata.go b/backend/plugin/schema/tidb/parse_to_metadata.go
index de84b83edd..b5b08a4c17 100644
--- a/backend/plugin/schema/tidb/parse_to_metadata.go
+++ b/backend/plugin/schema/tidb/parse_to_metadata.go
@@ -406,6 +406,7 @@ type indexState struct {
id int
name string
keys []string
+ length []int64
primary bool
unique bool
}
@@ -414,6 +415,7 @@ func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {
return &storepb.IndexMetadata{
Name: i.name,
Expressions: i.keys,
+ KeyLength: i.length,
Primary: i.primary,
Unique: i.unique,
// Unsupported, for tests only.
@@ -426,6 +428,7 @@ func convertToIndexState(id int, index *storepb.IndexMetadata) *indexState {
id: id,
name: index.Name,
keys: index.Expressions,
+ length: index.KeyLength,
primary: index.Primary,
unique: index.Unique,
}
@@ -475,7 +478,11 @@ func (i *indexState) toString(buf *strings.Builder) error {
return err
}
} else {
- if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
+ columnText := fmt.Sprintf("`%s`", key)
+ if len(i.length) > j && i.length[j] > 0 {
+ columnText = fmt.Sprintf("`%s`(%d)", key, i.length[j])
+ }
+ if _, err := buf.WriteString(columnText); err != nil {
return err
}
}
@@ -780,13 +787,16 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
table.foreignKeys[fkName] = fk
case tidbast.ConstraintIndex, tidbast.ConstraintUniq, tidbast.ConstraintUniqKey, tidbast.ConstraintUniqIndex, tidbast.ConstraintKey:
var referencingColumnList []string
+ var lengthList []int64
for _, spec := range constraint.Keys {
var specString string
var err error
if spec.Column != nil {
specString = spec.Column.Name.String()
if spec.Length > 0 {
- specString = fmt.Sprintf("`%s`(%d)", specString, spec.Length)
+ lengthList = append(lengthList, int64(spec.Length))
+ } else {
+ lengthList = append(lengthList, -1)
}
} else {
specString, err = tidbRestoreNode(spec, tidbformat.RestoreKeyWordLowercase|tidbformat.RestoreStringSingleQuotes|tidbformat.RestoreNameBackQuotes)
@@ -815,6 +825,7 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
id: len(table.indexes),
name: indexName,
keys: referencingColumnList,
+ length: lengthList,
primary: false,
unique: constraintType == tidbast.ConstraintUniq || constraintType == tidbast.ConstraintUniqKey || constraintType == tidbast.ConstraintUniqIndex,
}
@@ -870,7 +881,7 @@ func restoreComment(expr tidbast.ExprNode) (string, error) {
return comment, nil
}
-func equalKeys(a, b []string) bool {
+func equalKeys(a []string, aLength []int64, b []string, bLength []int64) bool {
if len(a) != len(b) {
return false
}
@@ -878,6 +889,17 @@ func equalKeys(a, b []string) bool {
if key != b[i] {
return false
}
+ lenA := int64(-1)
+ lenB := int64(-1)
+ if len(aLength) > i {
+ lenA = aLength[i]
+ }
+ if len(bLength) > i {
+ lenB = bLength[i]
+ }
+ if lenA != lenB {
+ return false
+ }
}
return true
}
diff --git a/backend/plugin/schema/tidb/testdata/get_design_schema.yaml b/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
index 4ecd6d2644..6c512748a2 100644
--- a/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema/tidb/testdata/get_design_schema.yaml
@@ -1,3 +1,60 @@
+- baseline: |-
+ CREATE TABLE `t` (
+ `a` int,
+ `b` varchar(255),
+ KEY `idx_t_b` (`b`(20)) COMMENT 'this is comment with',
+ KEY `delete_idx` (`b`(20)) COMMENT 'need to delete'
+ );
+ target: |-
+ {
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "t",
+ "columns": [
+ {
+ "name": "a",
+ "type": "int"
+ },
+ {
+ "name": "b",
+ "type": "varchar(255)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "idx_t_b",
+ "expressions": [
+ "b"
+ ],
+ "keyLength": [
+ "20"
+ ],
+ "comment": "this is comment with"
+ },
+ {
+ "name": "idx_t_b2",
+ "expressions": [
+ "b"
+ ],
+ "keyLength": [
+ "20"
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ result: |
+ CREATE TABLE `t` (
+ `a` int NOT NULL,
+ `b` varchar(255) NOT NULL,
+ KEY `idx_t_b` (`b`(20)) COMMENT 'this is comment with',
+ KEY `idx_t_b2` (`b`(20))
+ );
- baseline: |-
CREATE TABLE `t` (
`a` int,
diff --git a/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml b/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
index 3ab910ea28..7ee147e612 100644
--- a/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
+++ b/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
@@ -10,60 +10,66 @@
) ENGINE=InnoDB DEFAULT COLLATE=utf8mb4_bin COMMENT 'this is comment with '' escaped';
metadata: |-
{
- "schemas": [
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t",
- "columns": [
+ "name": "t",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "type": "int(11)",
- "comment": "this is comment"
+ "name": "a",
+ "position": 1,
+ "type": "int(11)",
+ "comment": "this is comment"
},
{
- "name": "b",
- "position": 2,
- "type": "varchar(20)"
+ "name": "b",
+ "position": 2,
+ "type": "varchar(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "idx",
- "expressions": [
+ "name": "idx",
+ "expressions": [
"((`a`+20))"
],
- "visible": true
+ "visible": true
},
{
- "name": "idx2",
- "expressions": [
+ "name": "idx2",
+ "expressions": [
"a"
],
- "unique": true,
- "visible": true
+ "keyLength": [
+ "-1"
+ ],
+ "unique": true,
+ "visible": true
},
{
- "name": "idx_length",
- "expressions": [
- "`b`(10)"
+ "name": "idx_length",
+ "expressions": [
+ "b"
+ ],
+ "keyLength": [
+ "10"
],
- "visible": true
+ "visible": true
},
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
],
- "engine": "InnoDB",
- "collation": "utf8mb4_bin",
- "comment": "this is comment with ' escaped"
+ "engine": "InnoDB",
+ "collation": "utf8mb4_bin",
+ "comment": "this is comment with ' escaped"
}
]
}
@@ -92,192 +98,192 @@
create table t5(a bigint NOT NULL /*T![auto_rand] AUTO_RANDOM(6, 54) */, primary key (a));
metadata: |-
{
- "schemas": [
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t",
- "columns": [
+ "name": "t",
+ "columns": [
{
- "name": "c",
- "position": 1,
- "nullable": true,
- "type": "int(11)"
+ "name": "c",
+ "position": 1,
+ "nullable": true,
+ "type": "int(11)"
},
{
- "name": "a",
- "position": 2,
- "defaultExpression": "1",
- "nullable": true,
- "type": "int(11)",
- "comment": "abcdefg"
+ "name": "a",
+ "position": 2,
+ "defaultExpression": "1",
+ "nullable": true,
+ "type": "int(11)",
+ "comment": "abcdefg"
},
{
- "name": "b",
- "position": 3,
- "defaultNull": true,
- "nullable": true,
- "type": "varchar(20)"
+ "name": "b",
+ "position": 3,
+ "defaultNull": true,
+ "nullable": true,
+ "type": "varchar(20)"
},
{
- "name": "d",
- "position": 4,
- "nullable": true,
- "type": "varbinary(500)"
+ "name": "d",
+ "position": 4,
+ "nullable": true,
+ "type": "varbinary(500)"
},
{
- "name": "e",
- "position": 5,
- "nullable": true,
- "type": "binary(20)"
+ "name": "e",
+ "position": 5,
+ "nullable": true,
+ "type": "binary(20)"
},
{
- "name": "f",
- "position": 6,
- "nullable": true,
- "type": "blob"
+ "name": "f",
+ "position": 6,
+ "nullable": true,
+ "type": "blob"
},
{
- "name": "g",
- "position": 7,
- "nullable": true,
- "type": "tinyblob"
+ "name": "g",
+ "position": 7,
+ "nullable": true,
+ "type": "tinyblob"
},
{
- "name": "h",
- "position": 8,
- "nullable": true,
- "type": "mediumblob"
+ "name": "h",
+ "position": 8,
+ "nullable": true,
+ "type": "mediumblob"
},
{
- "name": "i",
- "position": 9,
- "nullable": true,
- "type": "longblob"
+ "name": "i",
+ "position": 9,
+ "nullable": true,
+ "type": "longblob"
},
{
- "name": "j",
- "position": 10,
- "nullable": true,
- "type": "text"
+ "name": "j",
+ "position": 10,
+ "nullable": true,
+ "type": "text"
},
{
- "name": "k",
- "position": 11,
- "nullable": true,
- "type": "tinytext"
+ "name": "k",
+ "position": 11,
+ "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",
- "position": 1,
- "defaultNull": true,
- "type": "int(11)"
+ "name": "b",
+ "position": 1,
+ "defaultNull": true,
+ "type": "int(11)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"b"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
},
{
- "name": "t3",
- "columns": [
+ "name": "t3",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "defaultExpression": "AUTO_RANDOM",
- "type": "bigint(20)"
+ "name": "a",
+ "position": 1,
+ "defaultExpression": "AUTO_RANDOM",
+ "type": "bigint(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
},
{
- "name": "t4",
- "columns": [
+ "name": "t4",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "defaultExpression": "AUTO_RANDOM(6)",
- "type": "bigint(20)"
+ "name": "a",
+ "position": 1,
+ "defaultExpression": "AUTO_RANDOM(6)",
+ "type": "bigint(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
},
{
- "name": "t5",
- "columns": [
+ "name": "t5",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "defaultExpression": "AUTO_RANDOM(6, 54)",
- "type": "bigint(20)"
+ "name": "a",
+ "position": 1,
+ "defaultExpression": "AUTO_RANDOM(6, 54)",
+ "type": "bigint(20)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
}