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 comments of two columns not match (#2741)

h3n4le5acafe06ef1
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index 562ca8dc4a..a0e762ce4b 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -138,6 +138,53 @@ func TestColumnOption(t *testing.T) {
}
}
+func TestColumnComment(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Author Name' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'Book Name' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Author Name' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'AUTHOR NAME' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'AUTHOR NAME' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) COMMENT 'Book Name' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ want: "",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) COMMENT 'Book Name' NOT NULL);`,
+ want: "",
+ },
+ }
+ a := require.New(t)
+ for _, test := range tests {
+ oldNodes := getStmtNodes(t, test.old)
+ newNodes := getStmtNodes(t, test.new)
+ out, err := SchemaDiff(oldNodes, newNodes)
+ a.NoError(err)
+ a.Equalf(test.want, out, "old: %s\nnew: %s\n", test.old, test.new)
+ }
+}
+
func getStmtNodes(t *testing.T, sql string) []ast.StmtNode {
nodes, _, err := parser.New().Parse(sql, "", "")
require.NoError(t, err)
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index c506b6dbdd..b974498b85 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -130,7 +130,14 @@ func isColumnOptionsEqaul(old, new []*ast.ColumnOption) bool {
if oldOption.Tp != newOption.Tp {
return false
}
- // TODO(zp): it's not enough to compare the type for some option.
+ // 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() {
+ return false
+ }
+ default:
+ }
}
return true
}