Skip to content

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

feat(GetDesignSchema): handle table constraint in get design schema for mysql (#10179)

h3n4l617f33222c72
diff --git a/backend/plugin/schema/mysql/structurer.go b/backend/plugin/schema/mysql/structurer.go
index 49b2556bf0..4f633cfa88 100644
--- a/backend/plugin/schema/mysql/structurer.go
+++ b/backend/plugin/schema/mysql/structurer.go
@@ -382,6 +382,7 @@ type indexState struct {
keys []string
primary bool
unique bool
+ tp string
}
func (i *indexState) convertToIndexMetadata() *storepb.IndexMetadata {
@@ -402,6 +403,7 @@ func convertToIndexState(id int, index *storepb.IndexMetadata) *indexState {
keys: index.Expressions,
primary: index.Primary,
unique: index.Unique,
+ tp: index.Type,
}
}
@@ -424,7 +426,15 @@ func (i *indexState) toString(buf *strings.Builder) error {
return err
}
} else {
- if i.unique {
+ if strings.ToUpper(i.tp) == "FULLTEXT" {
+ if _, err := buf.WriteString("FULLTEXT KEY "); err != nil {
+ return err
+ }
+ } else if strings.ToUpper(i.tp) == "SPATIAL" {
+ if _, err := buf.WriteString("SPATIAL KEY "); err != nil {
+ return err
+ }
+ } else if i.unique {
if _, err := buf.WriteString("UNIQUE KEY "); err != nil {
return err
}
@@ -457,6 +467,16 @@ func (i *indexState) toString(buf *strings.Builder) error {
if _, err := buf.WriteString(")"); err != nil {
return err
}
+
+ if strings.ToUpper(i.tp) == "BTREE" {
+ if _, err := buf.WriteString(" USING BTREE"); err != nil {
+ return err
+ }
+ } else if strings.ToUpper(i.tp) == "HASH" {
+ if _, err := buf.WriteString(" USING HASH"); err != nil {
+ return err
+ }
+ }
}
return nil
}
@@ -676,6 +696,46 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
referencedColumns: referencedColumns,
}
table.foreignKeys[name] = fk
+ case "FULLTEXT":
+ var name string
+ if ctx.IndexName() != nil {
+ name = mysqlparser.NormalizeMySQLIdentifier(ctx.IndexName().Identifier())
+ }
+ 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: false,
+ tp: symbol,
+ }
+ table.indexes[name] = idx
+ case "SPATIAL":
+ var name string
+ if ctx.IndexName() != nil {
+ name = mysqlparser.NormalizeMySQLIdentifier(ctx.IndexName().Identifier())
+ }
+ 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: false,
+ tp: symbol,
+ }
+ table.indexes[name] = idx
case "KEY", "INDEX", "UNIQUE":
var name string
if v := ctx.IndexNameAndType(); v != nil {
@@ -689,12 +749,17 @@ func (t *mysqlTransformer) EnterTableConstraintDef(ctx *mysql.TableConstraintDef
t.err = errors.New("multiple indexes found: " + name)
return
}
+ tp := "BTREE"
+ if v := ctx.IndexNameAndType(); v != nil && v.IndexType() != nil {
+ tp = strings.ToUpper(v.IndexType().GetText())
+ }
idx := &indexState{
id: len(table.indexes),
name: name,
keys: list,
primary: false,
unique: symbol == "UNIQUE",
+ tp: tp,
}
table.indexes[name] = idx
}
diff --git a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
index 4a48c751cc..93e64a9758 100644
--- a/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
+++ b/backend/plugin/schema/mysql/testdata/get_design_schema.yaml
@@ -116,6 +116,81 @@
]
}
]
+ },
+ {
+ "name": "t5",
+ "engine": "InnoDB",
+ "columns": [
+ {
+ "name": "id",
+ "type": "int",
+ "position": 1
+ },
+ {
+ "name": "a",
+ "type": "int",
+ "nullable": true,
+ "position": 2,
+ "defaultNull": true
+ },
+ {
+ "name": "title",
+ "type": "text",
+ "nullable": true,
+ "position": 3,
+ "collation": "utf8mb4_general_ci",
+ "defaultNull": true,
+ "characterSet": "utf8mb4"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true,
+ "expressions": [
+ "id"
+ ]
+ },
+ {
+ "name": "t5_a_plus_5_t_a_plus_3_uniq_idx",
+ "type": "BTREE",
+ "unique": true,
+ "visible": true,
+ "expressions": [
+ "((`a` + 5))",
+ "((`a` + 3))"
+ ]
+ },
+ {
+ "name": "t5_a_plus_5_uniq_idx",
+ "type": "BTREE",
+ "unique": true,
+ "visible": true,
+ "expressions": [
+ "((`a` + 5))"
+ ]
+ },
+ {
+ "name": "t5_a_uniq_idx",
+ "type": "BTREE",
+ "unique": true,
+ "visible": true,
+ "expressions": [
+ "a"
+ ]
+ },
+ {
+ "name": "t5_title_fulltext_idx",
+ "type": "FULLTEXT",
+ "visible": true,
+ "expressions": [
+ "title"
+ ]
+ }
+ ]
}
]
}
@@ -140,6 +215,16 @@
PRIMARY KEY (`b`),
CONSTRAINT `fk1` FOREIGN KEY (`b`) REFERENCES `t4` (`b`)
);
+ CREATE TABLE `t5` (
+ `id` int NOT NULL,
+ `a` int NULL DEFAULT NULL,
+ `title` text NULL DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY `t5_a_plus_5_t_a_plus_3_uniq_idx` (((`a` + 5)),((`a` + 3))) USING BTREE,
+ UNIQUE KEY `t5_a_plus_5_uniq_idx` (((`a` + 5))) USING BTREE,
+ UNIQUE KEY `t5_a_uniq_idx` (`a`) USING BTREE,
+ FULLTEXT KEY `t5_title_fulltext_idx` (`title`)
+ );
- baseline: |-
CREATE TABLE `t` (