Skip to content

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

chore: handle tidb table comment (#8824)

Danny Xu1b6637d9ea58
diff --git a/backend/api/v1/schema_design_transformer_tidb.go b/backend/api/v1/schema_design_transformer_tidb.go
index 2467a615d8..e7872a999e 100644
--- a/backend/api/v1/schema_design_transformer_tidb.go
+++ b/backend/api/v1/schema_design_transformer_tidb.go
@@ -114,6 +114,11 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
table.columns[columnName] = columnState
}
+ for _, tableOption := range node.Options {
+ if tableOption.Tp == tidbast.TableOptionComment {
+ table.comment = tableComment(tableOption)
+ }
+ }
// primary and foreign key definition
for _, constraint := range node.Constraints {
@@ -218,6 +223,10 @@ func columnDefaultValue(column *tidbast.ColumnDef) (*string, error) {
return nil, nil
}
+func tableComment(option *tidbast.TableOption) string {
+ return option.StrValue
+}
+
func columnComment(column *tidbast.ColumnDef) (string, error) {
for _, option := range column.Options {
if option.Tp == tidbast.ColumnOptionComment {
@@ -240,6 +249,7 @@ func tidbRestoreNode(node tidbast.Node, flag tidbformat.RestoreFlags) (string, e
}
return buffer.String(), nil
}
+
func tidbRestoreNodeDefault(node tidbast.Node) (string, error) {
return tidbRestoreNode(node, tidbformat.DefaultRestoreFlags)
}
@@ -741,14 +751,47 @@ func (g *tidbDesignSchemaGenerator) Leave(in tidbast.Node) (tidbast.Node, bool)
}
// Table option.
+ hasTableComment := false
for _, option := range node.Options {
- if optionStr, err := tidbRestoreTableOption(option); err == nil {
- if _, err := g.result.WriteString(" " + optionStr); err != nil {
- g.err = err
- return in, true
+ if option.Tp == tidbast.TableOptionComment {
+ commentValue := tableComment(option)
+ if g.currentTable.comment == commentValue {
+ if commentStr, err := tidbRestoreNodeDefault(option.Value); err == nil {
+ if _, err := g.result.WriteString(" " + commentStr); err != nil {
+ g.err = err
+ return in, true
+ }
+ }
+ } else if g.currentTable.comment != "" {
+ if _, err := g.result.WriteString(" COMMENT"); err != nil {
+ g.err = err
+ return in, true
+ }
+ if _, err := g.result.WriteString(fmt.Sprintf(" '%s'", g.currentTable.comment)); err != nil {
+ g.err = err
+ return in, true
+ }
+ }
+ hasTableComment = true
+ } else {
+ if optionStr, err := tidbRestoreTableOption(option); err == nil {
+ if _, err := g.result.WriteString(" " + optionStr); err != nil {
+ g.err = err
+ return in, true
+ }
}
}
}
+ if !hasTableComment && g.currentTable.comment != "" {
+ if _, err := g.result.WriteString(" COMMENT"); err != nil {
+ g.err = err
+ return in, true
+ }
+ if _, err := g.result.WriteString(fmt.Sprintf(" '%s'", g.currentTable.comment)); err != nil {
+ g.err = err
+ return in, true
+ }
+ }
// Table partition.
if node.Partition != nil {
diff --git a/backend/api/v1/testdata/tidb/design.yaml b/backend/api/v1/testdata/tidb/design.yaml
index e4e9ce2915..7590877ba8 100644
--- a/backend/api/v1/testdata/tidb/design.yaml
+++ b/backend/api/v1/testdata/tidb/design.yaml
@@ -4,7 +4,7 @@
`a` int,
PRIMARY KEY (`a`),
CONSTRAINT fk1 FOREIGN KEY (`a`) REFERENCES `t2` (`b`)
- );
+ ) COMMENT 'hello';
CREATE TABLE `t3` (
`a` int
);
@@ -13,6 +13,7 @@
`c` int,
`d` int,
`e` char(10) NOT NULL DEFAULT 'NULL' comment 'this is comment',
+ `f` int comment 'this is comment',
PRIMARY KEY (`a`, `c`, `d`),
CONSTRAINT fk1 FOREIGN KEY (`a`) REFERENCES `t2` (`b`)
);
@@ -54,7 +55,8 @@
"primary": true,
"visible": true
}
- ]
+ ],
+ "comment": "hello"
},
{
"name": "t",
@@ -93,6 +95,11 @@
"hasDefault": true,
"defaultExpression": "1",
"comment": "col_e"
+ },
+ {
+ "name": "f",
+ "type": "int",
+ "nullable": true
}
],
"indexes": [
@@ -171,6 +178,7 @@
`a` varchar(10) NOT NULL DEFAULT 'default value' COMMENT 'this is comment, aha',
`c` int NOT NULL DEFAULT 1 COMMENT 'col_b',
`e` int NULL DEFAUL 1 COMMENT 'col_e',
+ `f` int,
`b` char(10) NOT NULL,
PRIMARY KEY (`a`, `b`, `c`),
CONSTRAINT `fk1` FOREIGN KEY (`b`) REFERENCES `t2` (`b`)
@@ -180,7 +188,7 @@
`age` varbinary(500) NULL,
`name` binary(10) NULL,
PRIMARY KEY(`id`) CLUSTERED
- ) ENGINE = InnoDB DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_BIN;
+ ) ENGINE = InnoDB DEFAULT CHARACTER SET = UTF8MB4 DEFAULT COLLATE = UTF8MB4_BIN COMMENT 'hello';
CREATE TABLE `t2` (
`b` int NULL,