Skip to content

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

feat: transform index/key/unique to metadata for tidb (#10091)

h3n4l8ad02f077600
diff --git a/backend/api/v1/schema_design_transformer_tidb.go b/backend/api/v1/schema_design_transformer_tidb.go
index 9ae3e4b919..4861dc424e 100644
--- a/backend/api/v1/schema_design_transformer_tidb.go
+++ b/backend/api/v1/schema_design_transformer_tidb.go
@@ -149,7 +149,8 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
// primary and foreign key definition
for _, constraint := range node.Constraints {
- switch constraint.Tp {
+ constraintType := constraint.Tp
+ switch constraintType {
case tidbast.ConstraintPrimaryKey:
var pkList []string
for _, constraint := range node.Constraints {
@@ -198,6 +199,46 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
referencedColumns: referencedColumnList,
}
table.foreignKeys[fkName] = fk
+ case tidbast.ConstraintIndex, tidbast.ConstraintUniq, tidbast.ConstraintUniqKey, tidbast.ConstraintUniqIndex, tidbast.ConstraintKey:
+ var referencingColumnList []string
+ 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)
+ }
+ } else {
+ specString, err = tidbRestoreNode(spec, tidbformat.RestoreKeyWordLowercase|tidbformat.RestoreStringSingleQuotes|tidbformat.RestoreNameBackQuotes)
+ if err != nil {
+ t.err = err
+ return in, true
+ }
+ }
+ referencingColumnList = append(referencingColumnList, specString)
+ }
+
+ var indexName string
+ if constraint.Name != "" {
+ indexName = constraint.Name
+ } else {
+ t.err = errors.New("empty index name")
+ return in, true
+ }
+
+ if table.indexes[indexName] != nil {
+ t.err = errors.New("multiple foreign keys found: " + indexName)
+ return in, true
+ }
+
+ table.indexes[indexName] = &indexState{
+ id: len(table.indexes),
+ name: indexName,
+ keys: referencingColumnList,
+ primary: false,
+ unique: constraintType == tidbast.ConstraintUniq || constraintType == tidbast.ConstraintUniqKey || constraintType == tidbast.ConstraintUniqIndex,
+ }
}
}
}
diff --git a/backend/api/v1/testdata/tidb/schema.yaml b/backend/api/v1/testdata/tidb/schema.yaml
index b4a95c7c6b..47681dcf68 100644
--- a/backend/api/v1/testdata/tidb/schema.yaml
+++ b/backend/api/v1/testdata/tidb/schema.yaml
@@ -1,3 +1,73 @@
+- engine: 6
+ schema: |-
+ create table t
+ (
+ `a` int NOT NULL COMMENT 'this is comment',
+ `b` VARCHAR(20) NOT NULL,
+ KEY `idx` (((`a` + 20))),
+ UNIQUE KEY `idx2` (`a`),
+ KEY `idx_length` (`b`(10)),
+ PRIMARY KEY (`a`)
+ ) COMMENT 'this is comment with '' escaped';
+ metadata: |-
+ {
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "t",
+ "columns": [
+ {
+ "name": "a",
+ "type": "int",
+ "comment": "this is comment",
+ "userComment": "this is comment"
+ },
+ {
+ "name": "b",
+ "type": "varchar(20)"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "idx",
+ "expressions": [
+ "((`a`+20))"
+ ],
+ "visible": true
+ },
+ {
+ "name": "idx2",
+ "expressions": [
+ "a"
+ ],
+ "unique": true,
+ "visible": true
+ },
+ {
+ "name": "idx_length",
+ "expressions": [
+ "`b`(10)"
+ ],
+ "visible": true
+ },
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "a"
+ ],
+ "unique": true,
+ "primary": true,
+ "visible": true
+ }
+ ],
+ "comment": "this is comment with ' escaped",
+ "userComment": "this is comment with ' escaped"
+ }
+ ]
+ }
+ ]
+ }
- engine: 6
schema: |
create table t
@@ -22,178 +92,178 @@
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",
- "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": "f",
+ "nullable": true,
+ "type": "blob"
},
{
- "name": "g",
- "nullable": true,
- "type": "tinyblob"
+ "name": "g",
+ "nullable": true,
+ "type": "tinyblob"
},
{
- "name": "h",
- "nullable": true,
- "type": "mediumblob"
+ "name": "h",
+ "nullable": true,
+ "type": "mediumblob"
},
{
- "name": "i",
- "nullable": true,
- "type": "longblob"
+ "name": "i",
+ "nullable": true,
+ "type": "longblob"
},
{
- "name": "j",
- "nullable": true,
- "type": "text"
+ "name": "j",
+ "nullable": true,
+ "type": "text"
},
{
- "name": "k",
- "nullable": true,
- "type": "tinytext"
+ "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
}
]
},
{
- "name": "t3",
- "columns": [
+ "name": "t3",
+ "columns": [
{
- "name": "a",
- "defaultExpression": "AUTO_RANDOM",
- "type": "bigint"
+ "name": "a",
+ "defaultExpression": "AUTO_RANDOM",
+ "type": "bigint"
}
],
- "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",
- "defaultExpression": "AUTO_RANDOM(6)",
- "type": "bigint"
+ "name": "a",
+ "defaultExpression": "AUTO_RANDOM(6)",
+ "type": "bigint"
}
],
- "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",
- "defaultExpression": "AUTO_RANDOM(6, 54)",
- "type": "bigint"
+ "name": "a",
+ "defaultExpression": "AUTO_RANDOM(6, 54)",
+ "type": "bigint"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
]
}