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 NULL options of two columns not match (#2731)

h3n4l8f26a23dab94
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.go
index dcd3740c26..562ca8dc4a 100644
--- a/plugin/parser/differ/mysql/column_test.go
+++ b/plugin/parser/differ/mysql/column_test.go
@@ -4,10 +4,11 @@ import (
"testing"
"github.com/pingcap/tidb/parser"
+ "github.com/pingcap/tidb/parser/ast"
"github.com/stretchr/testify/require"
)
-func TestColumn(t *testing.T) {
+func TestColumnExist(t *testing.T) {
tests := []struct {
old string
new string
@@ -34,7 +35,23 @@ func TestColumn(t *testing.T) {
new: `CREATE TABLE book(id INT, price INT, code VARCHAR(50), PRIMARY KEY(id));`,
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 TestColumnType(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
// Different types.
{
old: `CREATE TABLE book(id INT);`,
@@ -55,12 +72,74 @@ func TestColumn(t *testing.T) {
}
a := require.New(t)
for _, test := range tests {
- oldNodes, _, err := parser.New().Parse(test.old, "", "")
- a.NoError(err)
- newNodes, _, err := parser.New().Parse(test.new, "", "")
+ 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 TestColumnOption(t *testing.T) {
+ tests := []struct {
+ old string
+ new string
+ want string
+ }{
+ // NULL option not match.
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) NULL);`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NULL;\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50));`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50);\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL DEFAULT 'Harry Potter');`,
+ new: `CREATE TABLE book(name VARCHAR(50) NULL DEFAULT 'Harry Potter');`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) NULL DEFAULT _UTF8MB4'Harry Potter';\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL DEFAULT 'Harry Potter');`,
+ new: `CREATE TABLE book(name VARCHAR(50) DEFAULT 'Harry Potter');`,
+ want: "ALTER TABLE `book` MODIFY COLUMN `name` VARCHAR(50) DEFAULT _UTF8MB4'Harry Potter';\n",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50));`,
+ want: "",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50));`,
+ new: `CREATE TABLE book(name VARCHAR(50) NULL);`,
+ want: "",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) NULL);`,
+ want: "",
+ },
+ {
+ old: `CREATE TABLE book(name VARCHAR(50) NOT NULL);`,
+ new: `CREATE TABLE book(name VARCHAR(50) 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.Equal(test.want, out)
+ 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)
+ return nodes
+}
diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.go
index 0aafa1799d..c506b6dbdd 100644
--- a/plugin/parser/differ/mysql/differ.go
+++ b/plugin/parser/differ/mysql/differ.go
@@ -3,6 +3,7 @@ package mysql
import (
"bytes"
+ "sort"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
@@ -45,7 +46,7 @@ func SchemaDiff(old, new []ast.StmtNode) (string, error) {
continue
}
// We need to compare the two column definitions.
- if !isTwoColumnsSame(oldColumnDef, columnDef) {
+ if !isColumnEqual(oldColumnDef, columnDef) {
alterTableModifyColumnSpecs = append(alterTableModifyColumnSpecs, &ast.AlterTableSpec{
Tp: ast.AlterTableModifyColumn,
NewColumns: []*ast.ColumnDef{columnDef},
@@ -103,7 +104,49 @@ 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 {
+// isColumnEqual returns true if definitions of two columns with the same name are the same.
+func isColumnEqual(old, new *ast.ColumnDef) bool {
+ if !isColumnTypesEqual(old, new) {
+ return false
+ }
+ if !isColumnOptionsEqaul(old.Options, new.Options) {
+ return false
+ }
+ return true
+}
+
+func isColumnTypesEqual(old, new *ast.ColumnDef) bool {
return old.Tp.String() == new.Tp.String()
}
+
+func isColumnOptionsEqaul(old, new []*ast.ColumnOption) bool {
+ oldNormalizeOptions := normalizeColumnOptions(old)
+ newNormalizeOptions := normalizeColumnOptions(new)
+ if len(oldNormalizeOptions) != len(newNormalizeOptions) {
+ return false
+ }
+ for idx, oldOption := range oldNormalizeOptions {
+ newOption := newNormalizeOptions[idx]
+ if oldOption.Tp != newOption.Tp {
+ return false
+ }
+ // TODO(zp): it's not enough to compare the type for some option.
+ }
+ return true
+}
+
+// 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 {
+ var retOptions []*ast.ColumnOption
+ for _, option := range options {
+ if option.Tp == ast.ColumnOptionNull || option.Tp == ast.ColumnOptionNoOption {
+ continue
+ }
+ retOptions = append(retOptions, option)
+ }
+ sort.Slice(retOptions, func(i, j int) bool {
+ return retOptions[i].Tp < retOptions[j].Tp
+ })
+ return retOptions
+}