Skip to content

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

chore: differentiate index component column name or expresssion (#9930)

Danny Xu9ec36ded1644
diff --git a/backend/api/v1/schema_design_transformer.go b/backend/api/v1/schema_design_transformer.go
index 8196c9086f..4751f88912 100644
--- a/backend/api/v1/schema_design_transformer.go
+++ b/backend/api/v1/schema_design_transformer.go
@@ -469,12 +469,19 @@ func (i *indexState) toString(buf *strings.Builder) error {
}
for j, key := range i.keys {
if j > 0 {
- if _, err := buf.WriteString(", "); err != nil {
+ if _, err := buf.WriteString(","); err != nil {
return err
}
}
- if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
- return err
+ if len(key) > 2 && key[0] == '(' && key[len(key)-1] == ')' {
+ // Expressions are surrounded by parentheses.
+ if _, err := buf.WriteString(key); err != nil {
+ return err
+ }
+ } else {
+ if _, err := buf.WriteString(fmt.Sprintf("`%s`", key)); err != nil {
+ return err
+ }
}
}
if _, err := buf.WriteString(")"); err != nil {
diff --git a/backend/api/v1/testdata/deparse.yaml b/backend/api/v1/testdata/deparse.yaml
index 5915ac523d..06ab87fd3b 100644
--- a/backend/api/v1/testdata/deparse.yaml
+++ b/backend/api/v1/testdata/deparse.yaml
@@ -37,6 +37,18 @@
"unique": true,
"primary": true,
"visible": true
+ },
+ {
+ "name": "idx2",
+ "expressions": [
+ "((`a` + `b`))",
+ "((`a` - `b`))",
+ "a"
+ ],
+ "unique": false,
+ "primary": false,
+ "visible": true,
+ "type": "BTREE"
}
],
"foreignKeys": [
@@ -83,6 +95,7 @@
`a` int NULL DEFAULT 1 COMMENT 'abcdefg',
`b` varchar(20) NULL DEFAULT NULL,
PRIMARY KEY (`a`, `b`),
+ INDEX `idx2` (((`a` + `b`)),((`a` - `b`)),`a`),
CONSTRAINT `fk1` FOREIGN KEY (`a`) REFERENCES `t2` (`b`)
);
CREATE TABLE `t2` (
diff --git a/backend/plugin/db/mysql/sync.go b/backend/plugin/db/mysql/sync.go
index c884ece7ca..0a44b239b0 100644
--- a/backend/plugin/db/mysql/sync.go
+++ b/backend/plugin/db/mysql/sync.go
@@ -201,7 +201,9 @@ func (driver *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchema
if columnName.Valid {
expression = columnName.String
} else if expressionName.Valid {
- expression = expressionName.String
+ // It's a bit late or not necessary to differentiate the column name or expression.
+ // We add parentheses around expression.
+ expression = fmt.Sprintf("(%s)", expressionName.String)
}
key := db.TableKey{Schema: "", Table: tableName}