Skip to content

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

feat: generate alter table stmt modify column specs if the collate of two columns not match (#2781)

h3n4ld7c342992565
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index 18909a987f..ac7c282ecf 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -235,3 +235,39 @@ func TestColumnDefaultValue(t *testing.T) {
a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)
}
}
+
+func TestColumnCollate(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_bin DEFAULT 'Harry Potter' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COLLATE utf8mb4_polish_ci DEFAULT 'Harry Potter' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) 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) COLLATE utf8mb4_bin DEFAULT 'Holmes' NOT NULL);`,
+ want: "",
+ },
+ }
+ a := require.New(t)
+ mysqlDiffer := &SchemaDiffer{}
+ for _, test := range tests {
+ out, err := mysqlDiffer.SchemaDiff(test.old, test.new)
+ a.NoError(err)
+ a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)
+ }
+}
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index 0a7dcd23c9..edb59c00fe 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -141,7 +141,7 @@ func isColumnEqual(old, new *ast.ColumnDef) bool {
if !isColumnTypesEqual(old, new) {
return false
}
- if !isColumnOptionsEqaul(old.Options, new.Options) {
+ if !isColumnOptionsEqual(old.Options, new.Options) {
return false
}
return true
@@ -151,7 +151,7 @@ func isColumnTypesEqual(old, new *ast.ColumnDef) bool {
return old.Tp.String() == new.Tp.String()
}
-func isColumnOptionsEqaul(old, new []*ast.ColumnOption) bool {
+func isColumnOptionsEqual(old, new []*ast.ColumnOption) bool {
oldNormalizeOptions := normalizeColumnOptions(old)
newNormalizeOptions := normalizeColumnOptions(new)
if len(oldNormalizeOptions) != len(newNormalizeOptions) {
@@ -164,12 +164,12 @@ func isColumnOptionsEqaul(old, new []*ast.ColumnOption) bool {
}
// TODO(zp): it's not enough to compare the type for some options.
switch oldOption.Tp {
- case ast.ColumnOptionComment:
- if oldOption.Expr.(ast.ValueExpr).GetString() != newOption.Expr.(ast.ValueExpr).GetString() {
+ case ast.ColumnOptionComment, ast.ColumnOptionDefaultValue:
+ if oldOption.Expr.(ast.ValueExpr).GetValue() != newOption.Expr.(ast.ValueExpr).GetValue() {
return false
}
- case ast.ColumnOptionDefaultValue:
- if oldOption.Expr.(ast.ValueExpr).GetValue() != newOption.Expr.(ast.ValueExpr).GetValue() {
+ case ast.ColumnOptionCollate:
+ if oldOption.StrValue != newOption.StrValue {
return false
}
default: