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 types of two column with the same name are different (#2691)

h3n4le5f019afeee4
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index 72a19d1988..dcd3740c26 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -13,8 +13,9 @@ func TestColumn(t *testing.T) {
new string
want string
}{
+ // Missing columns
{
- old: `CREATE TABLE book(id INT, PRIMARY KEY(id))`,
+ old: `CREATE TABLE book(id INT, PRIMARY KEY(id));`,
new: `CREATE TABLE book(id INT, price INT, PRIMARY KEY(id));`,
want: "ALTER TABLE `book` ADD COLUMN (`price` INT);\n",
},
@@ -33,6 +34,24 @@ func TestColumn(t *testing.T) {
new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,
want: "",
},
+
+ // Different types.
+ {
+ old: `CREATE TABLE book(id INT);`,
+ new: `CREATE TABLE book(id VARCHAR(50));`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `id` VARCHAR(50);\n",
+ },
+
+ {
+ old: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,
+ new: `CREATE TABLE book(id VARCHAR(50), isbn VARCHAR(100));`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `id` VARCHAR(50), MODIFY COLUMN `isbn` VARCHAR(100);\n",
+ },
+ {
+ old: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,
+ new: `CREATE TABLE book(id INT, isbn VARCHAR(50));`,
+ want: "",
+ },
}
a := require.New(t)
for _, test := range tests {
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index effade6a67..0aafa1799d 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -27,6 +27,7 @@ func SchemaDiff(old, new []ast.StmtNode) (string, error) {
}
var alterTableAddColumnSpecs []*ast.AlterTableSpec
+ var alterTableModifyColumnSpecs []*ast.AlterTableSpec
var oldColumnMap = make(map[string]*ast.ColumnDef)
for _, oldColumnDef := range oldStmt.Cols {
oldColumnName := oldColumnDef.Name.Name.String()
@@ -35,11 +36,21 @@ func SchemaDiff(old, new []ast.StmtNode) (string, error) {
for _, columnDef := range newStmt.Cols {
newColumnName := columnDef.Name.Name.String()
- if _, ok := oldColumnMap[newColumnName]; !ok {
+ oldColumnDef, ok := oldColumnMap[newColumnName]
+ if !ok {
alterTableAddColumnSpecs = append(alterTableAddColumnSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableAddColumns,
NewColumns: []*ast.ColumnDef{columnDef},
})
+ continue
+ }
+ // We need to compare the two column definitions.
+ if !isTwoColumnsSame(oldColumnDef, columnDef) {
+ alterTableModifyColumnSpecs = append(alterTableModifyColumnSpecs, &ast.AlterTableSpec{
+ Tp: ast.AlterTableModifyColumn,
+ NewColumns: []*ast.ColumnDef{columnDef},
+ Position: &ast.ColumnPosition{Tp: ast.ColumnPositionNone},
+ })
}
}
@@ -51,6 +62,14 @@ func SchemaDiff(old, new []ast.StmtNode) (string, error) {
Specs: alterTableAddColumnSpecs,
})
}
+ if len(alterTableModifyColumnSpecs) > 0 {
+ diff = append(diff, &ast.AlterTableStmt{
+ Table: &ast.TableName{
+ Name: model.NewCIStr(tableName),
+ },
+ Specs: alterTableModifyColumnSpecs,
+ })
+ }
default:
}
}
@@ -83,3 +102,8 @@ func buildTableMap(nodes []ast.StmtNode) map[string]*ast.CreateTableStmt {
}
return oldTableMap
}
+
+// isTwoColumnsSame returns true if the two columns are the same.
+func isTwoColumnsSame(old, new *ast.ColumnDef) bool {
+ return old.Tp.String() == new.Tp.String()
+}