Skip to content

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

chore: fix tidb table charset (#15956)

Danny Xu9bc7f324c358
diff --git a/backend/plugin/db/tidb/sync.go b/backend/plugin/db/tidb/sync.go
index 4c11f086cf..d1a7a00a31 100644
--- a/backend/plugin/db/tidb/sync.go
+++ b/backend/plugin/db/tidb/sync.go
@@ -436,6 +436,7 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
DataFree: dataFree,
CreateOptions: createOptions,
Comment: comment,
+ Charset: convertCollationToCharset(collation),
Partitions: partitionTables[key],
}
if tableCollation.Valid {
@@ -487,6 +488,12 @@ func (d *Driver) SyncDBSchema(ctx context.Context) (*storepb.DatabaseSchemaMetad
return databaseMetadata, err
}
+func convertCollationToCharset(collation string) string {
+ // See mappings from "SHOW CHARACTER SET;".
+ tokens := strings.Split(collation, "_")
+ return tokens[0]
+}
+
func setColumnMetadataDefault(column *storepb.ColumnMetadata, defaultStr sql.NullString, nullableBool bool, extra string) {
if defaultStr.Valid {
// In TiDB 7, the extra value is empty for a column with CURRENT_TIMESTAMP default.
diff --git a/backend/plugin/db/tidb/sync_test.go b/backend/plugin/db/tidb/sync_test.go
index 57420021b7..fc3f313057 100644
--- a/backend/plugin/db/tidb/sync_test.go
+++ b/backend/plugin/db/tidb/sync_test.go
@@ -242,3 +242,41 @@ func TestSetColumnMetadataDefault(t *testing.T) {
a.Equal(tc.want, column, tc.name)
}
}
+
+func TestConvertCollationToCharset(t *testing.T) {
+ tests := []struct {
+ collation string
+ want string
+ }{
+ {
+ collation: "ascii_bin",
+ want: "ascii",
+ },
+ {
+ collation: "binary",
+ want: "binary",
+ },
+ {
+ collation: "gbk_chinese_ci",
+ want: "gbk",
+ },
+ {
+ collation: "latin1_bin",
+ want: "latin1",
+ },
+ {
+ collation: "utf8_bin",
+ want: "utf8",
+ },
+ {
+ collation: "utf8mb4_bin",
+ want: "utf8mb4",
+ },
+ }
+
+ a := require.New(t)
+ for _, tc := range tests {
+ got := convertCollationToCharset(tc.collation)
+ a.Equal(tc.want, got, tc.collation)
+ }
+}