Skip to content

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

chore: ignore redundant column collate and table options in normalize and differ (#5055)

rebelice59e9f5415ba4
diff --git a/backend/plugin/parser/differ/mysql/column_test.go b/backend/plugin/parser/differ/mysql/column_test.go
index bc59b4ef9c..af4c594022 100644
--- a/backend/plugin/parser/differ/mysql/column_test.go
+++ b/backend/plugin/parser/differ/mysql/column_test.go
@@ -228,6 +228,11 @@ func TestColumnCollate(t *testing.T) {
new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,
want: "",
},
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,
+ want: "",
+ },
}
testDiffWithoutDisableForeignKeyCheck(t, tests)
}
diff --git a/backend/plugin/parser/differ/mysql/differ.go b/backend/plugin/parser/differ/mysql/differ.go
index 3b4796c2ce..69ed048422 100644
--- a/backend/plugin/parser/differ/mysql/differ.go
+++ b/backend/plugin/parser/differ/mysql/differ.go
@@ -878,11 +878,19 @@ func isColumnTypesEqual(old, new *ast.ColumnDef) bool {
}
func isColumnOptionsEqual(old, new []*ast.ColumnOption) bool {
- oldNormalizeOptions := normalizeColumnOptions(old)
- newNormalizeOptions := normalizeColumnOptions(new)
+ oldCollate, oldNormalizeOptions := normalizeColumnOptions(old)
+ newCollate, newNormalizeOptions := normalizeColumnOptions(new)
if len(oldNormalizeOptions) != len(newNormalizeOptions) {
return false
}
+ if newCollate != nil {
+ if oldCollate == nil {
+ return false
+ }
+ if oldCollate.StrValue != newCollate.StrValue {
+ return false
+ }
+ }
for idx, oldOption := range oldNormalizeOptions {
oldOptionStr, err := toString(oldOption)
if err != nil {
@@ -904,10 +912,14 @@ func isColumnOptionsEqual(old, new []*ast.ColumnOption) bool {
// normalizeColumnOptions normalizes the column options.
// It skips the NULL option, NO option and then order the options by OptionType.
-func normalizeColumnOptions(options []*ast.ColumnOption) []*ast.ColumnOption {
+func normalizeColumnOptions(options []*ast.ColumnOption) (*ast.ColumnOption, []*ast.ColumnOption) {
var retOptions []*ast.ColumnOption
+ var collateOption *ast.ColumnOption
for _, option := range options {
switch option.Tp {
+ case ast.ColumnOptionCollate:
+ collateOption = option
+ continue
case ast.ColumnOptionNull:
continue
case ast.ColumnOptionNoOption:
@@ -922,7 +934,7 @@ func normalizeColumnOptions(options []*ast.ColumnOption) []*ast.ColumnOption {
sort.Slice(retOptions, func(i, j int) bool {
return retOptions[i].Tp < retOptions[j].Tp
})
- return retOptions
+ return collateOption, retOptions
}
// isIndexEqual returns true if definitions of two indexes are the same.
@@ -1198,6 +1210,12 @@ func diffTableOptions(tableName *ast.TableName, old, new []*ast.TableOption) *as
for oldTp, oldOption := range oldOptionsMap {
newOption, ok := newOptionsMap[oldTp]
if !ok {
+ switch oldOption.Tp {
+ // For table engine, table charset and table collation, if oldTable has but newTable doesn't,
+ // we skip drop them.
+ case ast.TableOptionEngine, ast.TableOptionCharset, ast.TableOptionCollate:
+ continue
+ }
// We should drop the table option if it doesn't exist in the new table options.
if astOption := dropTableOption(oldOption); astOption != nil {
options = append(options, astOption)
diff --git a/backend/plugin/parser/differ/mysql/table_test.go b/backend/plugin/parser/differ/mysql/table_test.go
index b19e8d0e37..ca48c4f13b 100644
--- a/backend/plugin/parser/differ/mysql/table_test.go
+++ b/backend/plugin/parser/differ/mysql/table_test.go
@@ -91,7 +91,7 @@ func TestTableOption(t *testing.T) {
{
old: `CREATE TABLE book(id INT) DEFAULT CHARACTER SET = utf8;`,
new: `CREATE TABLE book(id INT);`,
- want: "ALTER TABLE `book` DEFAULT CHARACTER SET=UTF8MB4;\n\n",
+ want: "",
},
// DEFAULT COLLATE
{
@@ -102,7 +102,7 @@ func TestTableOption(t *testing.T) {
{
old: `CREATE TABLE book(id INT) DEFAULT COLLATE = latin1_swedish_ci;`,
new: `CREATE TABLE book(id INT);`,
- want: "ALTER TABLE `book` DEFAULT COLLATE=UTF8MB4_GENERAL_CI;\n\n",
+ want: "",
},
// CHECKSUM
{
diff --git a/backend/plugin/parser/transform/mysql/transform.go b/backend/plugin/parser/transform/mysql/transform.go
index c3b9d3f1a1..bf5fa31492 100644
--- a/backend/plugin/parser/transform/mysql/transform.go
+++ b/backend/plugin/parser/transform/mysql/transform.go
@@ -141,6 +141,7 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
if table, exists := tableSet[node.Table.Name.String()]; exists {
table.missing = false
removeRedundantTableOption(table.createTable, node)
+ removeRedundantColumnOption(table.createTable, node)
}
case *ast.CreateIndexStmt:
if table, exists := tableSet[node.Table.Name.String()]; exists {
@@ -233,6 +234,38 @@ func (t *SchemaTransformer) Normalize(schema string, standard string) (string, e
return strings.Join(remainingStatement, ""), nil
}
+func removeRedundantColumnOption(table *ast.CreateTableStmt, standard *ast.CreateTableStmt) {
+ columnSet := make(map[string]*ast.ColumnDef)
+ for _, column := range standard.Cols {
+ columnSet[column.Name.Name.O] = column
+ }
+
+ for _, column := range table.Cols {
+ var newOptionList []*ast.ColumnOption
+ if standardColumn, exists := columnSet[column.Name.Name.O]; exists {
+ for _, option := range column.Options {
+ if option.Tp == ast.ColumnOptionCollate {
+ standardCollate := extractColumnCollate(standardColumn.Options)
+ if standardCollate == nil {
+ continue
+ }
+ }
+ newOptionList = append(newOptionList, option)
+ }
+ }
+ column.Options = newOptionList
+ }
+}
+
+func extractColumnCollate(list []*ast.ColumnOption) *ast.ColumnOption {
+ for _, option := range list {
+ if option.Tp == ast.ColumnOptionCollate {
+ return option
+ }
+ }
+ return nil
+}
+
func removeRedundantTableOption(table *ast.CreateTableStmt, standard *ast.CreateTableStmt) {
engine, charset, collation := extractEngineCharsetAndCollation(standard)
var newOptionList []*ast.TableOption
diff --git a/backend/plugin/parser/transform/mysql/transform_test.go b/backend/plugin/parser/transform/mysql/transform_test.go
index f887fec943..dffd489278 100644
--- a/backend/plugin/parser/transform/mysql/transform_test.go
+++ b/backend/plugin/parser/transform/mysql/transform_test.go
@@ -34,7 +34,7 @@ func TestCreateTableSeparateIndex(t *testing.T) {
func TestNormalize(t *testing.T) {
input := `
- create table t(a int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
+ create table t(a int, b varchar(20) COLLATE utf8mb4_general_ci) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
create index idx_a on t(a);
create unique index uk_t_a on t(a);
create index idx_xxx on t(a);
@@ -44,7 +44,7 @@ func TestNormalize(t *testing.T) {
standard := `
create table t4(a int);
create table t2(a int) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
- create table t(a int);
+ create table t(a int, b varchar(20));
create unique index uk_t_a on t(a);
create index idx_a on t(a);
create index idx_yyy on t(a);
@@ -54,7 +54,8 @@ func TestNormalize(t *testing.T) {
" `a` INT\n" +
") ENGINE=InnoDB DEFAULT CHARACTER SET=UTF8MB4 DEFAULT COLLATE=UTF8MB4_GENERAL_CI;\n\n" +
"CREATE TABLE `t` (\n" +
- " `a` INT\n" +
+ " `a` INT,\n" +
+ " `b` VARCHAR(20)\n" +
");\n\n" +
"CREATE INDEX `idx_xxx` ON `t` (`a`);\n\n" +
"CREATE UNIQUE INDEX `uk_t_a` ON `t` (`a`);\n\n" +