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 MySQL (#10089)

h3n4l8c2f405a5dc4
diff --git a/backend/api/v1/schema_design_transformer.go b/backend/api/v1/schema_design_transformer.go
index c2e204a979..dbf81a8fff 100644
--- a/backend/api/v1/schema_design_transformer.go
+++ b/backend/api/v1/schema_design_transformer.go
@@ -672,7 +672,8 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
}
if ctx.GetType_() != nil {
- switch strings.ToUpper(ctx.GetType_().GetText()) {
+ symbol := strings.ToUpper(ctx.GetType_().GetText())
+ switch symbol {
case "PRIMARY":
list := extractKeyListVariants(ctx.KeyListVariants())
table := t.state.schemas[""].tables[t.currentTable]
@@ -705,6 +706,27 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
referencedColumns: referencedColumns,
}
table.foreignKeys[name] = fk
+ case "KEY", "INDEX", "UNIQUE":
+ var name string
+ if v := ctx.IndexNameAndType(); v != nil {
+ name = mysqlparser.NormalizeMySQLIdentifier(v.IndexName().Identifier())
+ } else {
+ t.err = errors.New("index name not found")
+ }
+ list := extractKeyListVariants(ctx.KeyListVariants())
+ table := t.state.schemas[""].tables[t.currentTable]
+ if table.indexes[name] != nil {
+ t.err = errors.New("multiple indexes found: " + name)
+ return
+ }
+ idx := &indexState{
+ id: len(table.indexes),
+ name: name,
+ keys: list,
+ primary: false,
+ unique: symbol == "UNIQUE",
+ }
+ table.indexes[name] = idx
}
}
}
diff --git a/backend/api/v1/testdata/schema.yaml b/backend/api/v1/testdata/schema.yaml
index 63faea0f23..b70f9bfbee 100644
--- a/backend/api/v1/testdata/schema.yaml
+++ b/backend/api/v1/testdata/schema.yaml
@@ -1,3 +1,60 @@
+- engine: 2
+ schema: |-
+ create table t
+ (
+ `a` int NOT NULL COMMENT 'this is comment',
+ KEY `idx` (((`a` + 20))),
+ UNIQUE KEY `idx2` (`a`),
+ 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"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "idx",
+ "expressions": [
+ "((`a` + 20))"
+ ],
+ "visible": true
+ },
+ {
+ "name": "idx2",
+ "expressions": [
+ "a"
+ ],
+ "unique": true,
+ "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: 2
schema: |
create table t
@@ -11,78 +68,78 @@
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",
- "defaultExpression": "AUTO_INCREMENT",
- "nullable": true,
- "type": "int"
+ "name": "c",
+ "defaultExpression": "AUTO_INCREMENT",
+ "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)"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a",
"b"
],
- "unique": true,
- "primary": true,
- "visible": true
+ "unique": true,
+ "primary": true,
+ "visible": true
}
],
- "comment": "this is comment with ' escaped",
- "userComment": "this is comment with ' escaped",
- "foreignKeys": [
+ "comment": "this is comment with ' escaped",
+ "userComment": "this is comment with ' escaped",
+ "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
}
]
}