Skip to content

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

fix(structurer): align filterDatabaseMetadata and parseToMetadata for TiDB @h3n4l (#10448)

h3n4l33063e135fb6
diff --git a/backend/plugin/schema/tidb/parse_to_metadata.go b/backend/plugin/schema/tidb/parse_to_metadata.go
index 718effad1c..de84b83edd 100644
--- a/backend/plugin/schema/tidb/parse_to_metadata.go
+++ b/backend/plugin/schema/tidb/parse_to_metadata.go
@@ -136,6 +136,9 @@ type tableState struct {
indexes map[string]*indexState
foreignKeys map[string]*foreignKeyState
comment string
+ // Engine and collation are only supported in ParseToMetadata.
+ engine string
+ collation string
}
const (
@@ -212,6 +215,18 @@ func (t *tableState) toString(buf *strings.Builder) error {
return err
}
+ if t.engine != "" {
+ if _, err := buf.WriteString(fmt.Sprintf(" ENGINE=%s", t.engine)); err != nil {
+ return err
+ }
+ }
+
+ if t.collation != "" {
+ if _, err := buf.WriteString(fmt.Sprintf(" COLLATE=%s", t.collation)); err != nil {
+ return err
+ }
+ }
+
if t.comment != "" {
if _, err := buf.WriteString(fmt.Sprintf(" COMMENT '%s'", strings.ReplaceAll(t.comment, "'", "''"))); err != nil {
return err
@@ -236,6 +251,8 @@ func newTableState(id int, name string) *tableState {
func convertToTableState(id int, table *storepb.TableMetadata) *tableState {
state := newTableState(id, table.Name)
+ state.engine = table.Engine
+ state.collation = table.Collation
state.comment = table.Comment
for i, column := range table.Columns {
state.columns[column.Name] = convertToColumnState(i, column)
@@ -261,6 +278,10 @@ func (t *tableState) convertToTableMetadata() *storepb.TableMetadata {
for _, column := range columnStates {
columns = append(columns, column.convertToColumnMetadata())
}
+ // Backfill the column positions.
+ for i, column := range columns {
+ column.Position = int32(i + 1)
+ }
indexStates := []*indexState{}
for _, index := range t.indexes {
@@ -292,6 +313,8 @@ func (t *tableState) convertToTableMetadata() *storepb.TableMetadata {
Indexes: indexes,
ForeignKeys: fks,
Comment: t.comment,
+ Engine: t.engine,
+ Collation: t.collation,
}
}
@@ -693,8 +716,13 @@ func (t *tidbTransformer) Enter(in tidbast.Node) (tidbast.Node, bool) {
table.columns[columnName] = columnState
}
for _, tableOption := range node.Options {
- if tableOption.Tp == tidbast.TableOptionComment {
+ switch tableOption.Tp {
+ case tidbast.TableOptionComment:
table.comment = tableComment(tableOption)
+ case tidbast.TableOptionEngine:
+ table.engine = tableOption.StrValue
+ case tidbast.TableOptionCollate:
+ table.collation = tableOption.StrValue
}
}
diff --git a/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml b/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
index d1c8b992c8..3ab910ea28 100644
--- a/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
+++ b/backend/plugin/schema/tidb/testdata/parse_to_metadata.yaml
@@ -7,7 +7,7 @@
UNIQUE KEY `idx2` (`a`),
KEY `idx_length` (`b`(10)),
PRIMARY KEY (`a`)
- ) COMMENT 'this is comment with '' escaped';
+ ) ENGINE=InnoDB DEFAULT COLLATE=utf8mb4_bin COMMENT 'this is comment with '' escaped';
metadata: |-
{
"schemas": [
@@ -18,11 +18,13 @@
"columns": [
{
"name": "a",
+ "position": 1,
"type": "int(11)",
"comment": "this is comment"
},
{
"name": "b",
+ "position": 2,
"type": "varchar(20)"
}
],
@@ -59,6 +61,8 @@
"visible": true
}
],
+ "engine": "InnoDB",
+ "collation": "utf8mb4_bin",
"comment": "this is comment with ' escaped"
}
]
@@ -96,11 +100,13 @@
"columns": [
{
"name": "c",
+ "position": 1,
"nullable": true,
"type": "int(11)"
},
{
"name": "a",
+ "position": 2,
"defaultExpression": "1",
"nullable": true,
"type": "int(11)",
@@ -108,47 +114,56 @@
},
{
"name": "b",
+ "position": 3,
"defaultNull": true,
"nullable": true,
"type": "varchar(20)"
},
{
"name": "d",
+ "position": 4,
"nullable": true,
"type": "varbinary(500)"
},
{
"name": "e",
+ "position": 5,
"nullable": true,
"type": "binary(20)"
},
{
"name": "f",
+ "position": 6,
"nullable": true,
"type": "blob"
},
{
"name": "g",
+ "position": 7,
"nullable": true,
"type": "tinyblob"
},
{
"name": "h",
+ "position": 8,
"nullable": true,
"type": "mediumblob"
},
{
"name": "i",
+ "position": 9,
"nullable": true,
"type": "longblob"
},
{
"name": "j",
+ "position": 10,
"nullable": true,
"type": "text"
},
{
"name": "k",
+ "position": 11,
"nullable": true,
"type": "tinytext"
}
@@ -183,6 +198,7 @@
"columns": [
{
"name": "b",
+ "position": 1,
"defaultNull": true,
"type": "int(11)"
}
@@ -204,6 +220,7 @@
"columns": [
{
"name": "a",
+ "position": 1,
"defaultExpression": "AUTO_RANDOM",
"type": "bigint(20)"
}
@@ -225,6 +242,7 @@
"columns": [
{
"name": "a",
+ "position": 1,
"defaultExpression": "AUTO_RANDOM(6)",
"type": "bigint(20)"
}
@@ -246,6 +264,7 @@
"columns": [
{
"name": "a",
+ "position": 1,
"defaultExpression": "AUTO_RANDOM(6, 54)",
"type": "bigint(20)"
}