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 default values of two column not match (#2747)

h3n4lf418295e7c99
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index a0e762ce4b..f681b05d91 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -185,6 +185,63 @@ func TestColumnComment(t *testing.T) {
}
}
+func TestColumnDefaultValue(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' 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) DEFAULT 'Harry Potter' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT _UTF8MB4'Harry Potter' NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Holmes' NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter' NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT _UTF8MB4'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(id INT DEFAULT 0 NOT NULL);`,
+ new: `CREATE TABLE book(id INT NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `id` INT NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT NOT NULL);`,
+ new: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `id` INT DEFAULT 0 NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,
+ new: `CREATE TABLE book(id INT DEFAULT 1 NOT NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `id` INT DEFAULT 1 NOT NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,
+ new: `CREATE TABLE book(id INT DEFAULT 0 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 b974498b85..e7a98a9c79 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -136,6 +136,10 @@ func isColumnOptionsEqaul(old, new []*ast.ColumnOption) bool {
if oldOption.Expr.(ast.ValueExpr).GetString() != newOption.Expr.(ast.ValueExpr).GetString() {
return false
}
+ case ast.ColumnOptionDefaultValue:
+ if oldOption.Expr.(ast.ValueExpr).GetValue() != newOption.Expr.(ast.ValueExpr).GetValue() {
+ return false
+ }
default:
}
}