Skip to content

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

chore(sync): add table collation in dump for mysql (#10537)

h3n4le542380d5d4b
diff --git a/backend/plugin/db/mysql/dump.go b/backend/plugin/db/mysql/dump.go
index 6c8bba6f4d..f3105fc146 100644
--- a/backend/plugin/db/mysql/dump.go
+++ b/backend/plugin/db/mysql/dump.go
@@ -411,6 +411,10 @@ type triggerSchema struct {
// getTablesTx gets all tables of a database using the provided transaction.
func getTablesTx(txn *sql.Tx, dbType storepb.Engine, dbName string) ([]*TableSchema, error) {
+ collations, err := getTableCollation(txn, dbType, dbName)
+ if err != nil {
+ slog.Error("failed to get table collations", log.BBError(err))
+ }
var tables []*TableSchema
query := fmt.Sprintf("SHOW FULL TABLES FROM `%s`;", dbName)
rows, err := txn.Query(query)
@@ -430,7 +434,7 @@ func getTablesTx(txn *sql.Tx, dbType storepb.Engine, dbName string) ([]*TableSch
return nil, err
}
for _, tbl := range tables {
- stmt, err := getTableStmt(txn, dbType, dbName, tbl.Name, tbl.TableType)
+ stmt, err := getTableStmt(txn, dbType, dbName, tbl.Name, tbl.TableType, collations)
if err != nil {
return nil, errors.Wrapf(err, "failed to call getTableStmt(%q, %q, %q)", dbName, tbl.Name, tbl.TableType)
}
@@ -456,8 +460,38 @@ func trimAfterLastParenthesis(sql string) string {
return sql
}
+func getTableCollation(txn *sql.Tx, dbType storepb.Engine, dbName string) (map[string]string, error) {
+ if dbType != storepb.Engine_MYSQL {
+ return nil, nil
+ }
+ query := "SELECT TABLE_NAME, TABLE_COLLATION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_TYPE = 'BASE TABLE';"
+ collations := make(map[string]string)
+ rows, err := txn.Query(query, dbName)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+ for rows.Next() {
+ var tableName, collation sql.NullString
+ if err := rows.Scan(
+ &tableName,
+ &collation,
+ ); err != nil {
+ return nil, err
+ }
+ if tableName.Valid && collation.Valid {
+ collations[tableName.String] = collation.String
+ }
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+
+ return collations, nil
+}
+
// getTableStmt gets the create statement of a table.
-func getTableStmt(txn *sql.Tx, dbType storepb.Engine, dbName, tblName, tblType string) (string, error) {
+func getTableStmt(txn *sql.Tx, dbType storepb.Engine, dbName, tblName, tblType string, collations map[string]string) (string, error) {
switch tblType {
case baseTableType:
query := fmt.Sprintf("SHOW CREATE TABLE `%s`.`%s`;", dbName, tblName)
@@ -468,6 +502,46 @@ func getTableStmt(txn *sql.Tx, dbType storepb.Engine, dbName, tblName, tblType s
}
return "", err
}
+
+ tableCollation, collationOk := collations[tblName]
+ // MySQL version before 8.0.11 doesn't includes the table collation and column collation
+ // in the SHOW CREATE TABLE statement if they are the same as the database/table collation.
+ // https://bugs.mysql.com/bug.php?id=46239
+ // It causes the schema string is not consistent with the schema metadata. For example,
+ // it will cause table colored as yello because we cannot get the table collation from the schema string.
+ if dbType == storepb.Engine_MYSQL && collationOk {
+ lines := strings.Split(stmt, "\n")
+ for i := len(lines) - 1; i >= 0; i-- {
+ if strings.TrimSpace(lines[i]) == "" {
+ continue
+ }
+ if strings.Contains(lines[i], "COLLATE=") {
+ // We do not need to backfill the collation if it's already included in the statement.
+ break
+ }
+ // We always append the collation after the CHARSET options, so we do not
+ // append the collation if the CHARSET option is not included.
+ charsetPos := strings.Index(lines[i], "CHARSET=")
+ if charsetPos == -1 {
+ break
+ }
+ for pos, r := range lines[i] {
+ if pos < charsetPos+len("CHARSET=") {
+ continue
+ }
+ if (pos == len(lines[i])-1 && r == ';') || r == ' ' {
+ lines[i] = fmt.Sprintf("%s COLLATE=%s%s", lines[i][:pos], tableCollation, lines[i][pos:])
+ break
+ }
+ if pos == len(lines[i])-1 {
+ lines[i] = fmt.Sprintf("%s COLLATE=%s", lines[i], tableCollation)
+ break
+ }
+ }
+ break
+ }
+ stmt = strings.Join(lines, "\n")
+ }
if dbType == storepb.Engine_OCEANBASE {
stmt = trimAfterLastParenthesis(stmt)
}
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index 9d29dcb5c8..1fdf18b004 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -408,7 +408,7 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databas
rawDump = dbSchema.GetSchema()
}
- if !cmp.Equal(oldDatabaseMetadata, databaseMetadata, protocmp.Transform()) {
+ if force || !cmp.Equal(oldDatabaseMetadata, databaseMetadata, protocmp.Transform()) {
// Avoid updating dump everytime by dumping the schema only when the database metadata is changed.
// if oldDatabaseMetadata is nil and databaseMetadata is not, they are not equal resulting a sync.
if force || !equalDatabaseMetadata(oldDatabaseMetadata, databaseMetadata) {