Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(schema-synchronize): compare column default value (#3124)
h3n4l751ac3e17dcb
diff --git a/plugin/parser/differ/mysql/column_test.go b/plugin/parser/differ/mysql/column_test.goindex 960e2dd114..139d56a2e2 100644--- a/plugin/parser/differ/mysql/column_test.go+++ b/plugin/parser/differ/mysql/column_test.go@@ -188,6 +188,14 @@ func TestColumnDefaultValue(t *testing.T) {new: `CREATE TABLE book(id INT DEFAULT 0 NOT NULL);`,want: "",},+ // Function Call+ {+ old: "CREATE TABLE action(action_id smallint(5) unsigned NOT NULL AUTO_INCREMENT," ++ "`last_update` timestamp not null default current_timestamp);",+ new: "CREATE TABLE action(action_id smallint(5) unsigned NOT NULL AUTO_INCREMENT," ++ "`last_update` timestamp not null default current_timestamp(1));",+ want: "ALTER TABLE `action` MODIFY COLUMN `last_update` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(1);\n",+ },}testDiffWithoutDisableForeignKeyCheck(t, tests)}diff --git a/plugin/parser/differ/mysql/differ.go b/plugin/parser/differ/mysql/differ.goindex a5848eb166..91ea204dbc 100644--- a/plugin/parser/differ/mysql/differ.go+++ b/plugin/parser/differ/mysql/differ.go@@ -5,10 +5,13 @@ import ("bytes""fmt""io"- "regexp""sort""strings"+ "go.uber.org/zap"++ "github.com/bytebase/bytebase/common/log"+"github.com/pingcap/tidb/parser""github.com/pingcap/tidb/parser/ast""github.com/pingcap/tidb/parser/format"@@ -33,14 +36,6 @@ func init() {differ.Register(bbparser.TiDB, &SchemaDiffer{})}-type objectType string--const (- event objectType = "EVENT"- function objectType = "FUNCTION"- procedure objectType = "PROCEDURE"- trigger objectType = "TRIGGER"-)const (disableFKCheckStmt string = "SET FOREIGN_KEY_CHECKS=0;\n"enableFKCheckStmt string = "SET FOREIGN_KEY_CHECKS=1;\n"@@ -710,21 +705,19 @@ func isColumnOptionsEqual(old, new []*ast.ColumnOption) bool {return false}for idx, oldOption := range oldNormalizeOptions {+ oldOptionStr, err := toString(oldOption)+ if err != nil {+ log.Error("failed to convert old column option to string", zap.Error(err))+ return false+ }newOption := newNormalizeOptions[idx]- if oldOption.Tp != newOption.Tp {+ newOptionStr, err := toString(newOption)+ if err != nil {+ log.Error("failed to convert new column option to string", zap.Error(err))return false}- // TODO(zp): it's not enough to compare the type for some options.- switch oldOption.Tp {- case ast.ColumnOptionComment, ast.ColumnOptionDefaultValue:- if oldOption.Expr.(ast.ValueExpr).GetValue() != newOption.Expr.(ast.ValueExpr).GetValue() {- return false- }- case ast.ColumnOptionCollate:- if oldOption.StrValue != newOption.StrValue {- return false- }- default:+ if oldOptionStr != newOptionStr {+ return false}}return true@@ -794,18 +787,17 @@ func isKeyPartEqual(old, new []*ast.IndexPartSpecification) bool {return false}if oldKeyPart.Expr != nil && newKeyPart.Expr != nil {- var oldExpr bytes.Buffer- var newExpr bytes.Buffer- if err := oldKeyPart.Expr.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &oldExpr)); err != nil {- // Return error will cause the logic to be more complicated, so we just return false here.+ oldKeyPartStr, err := toString(oldKeyPart.Expr)+ if err != nil {+ log.Error("failed to convert old key part to string", zap.Error(err))return false}- if err := newKeyPart.Expr.Restore(format.NewRestoreCtx(format.DefaultRestoreFlags, &newExpr)); err != nil {- return false- }- if oldExpr.String() != newExpr.String() {+ newKeyPartStr, err := toString(newKeyPart.Expr)+ if err != nil {+ log.Error("failed to convert new key part to string", zap.Error(err))return false}+ return oldKeyPartStr == newKeyPartStr}// TODO(zp): TiDB MySQL parser doesn't record the index order field in go struct, but it can parse correctly.// https://sourcegraph.com/github.com/pingcap/tidb/-/blob/parser/parser.y?L3688@@ -914,24 +906,17 @@ func isCheckConstraintEqual(old, new *ast.Constraint) bool {return false}- var oldBuf bytes.Buffer- var newBuf bytes.Buffer- oldRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &oldBuf)- newRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &newBuf)- if err := old.Expr.Restore(oldRestoreCtx); err != nil {- return false- }- if err := new.Expr.Restore(newRestoreCtx); err != nil {- return false- }- if oldBuf.String() != newBuf.String() {+ oldStr, err := toString(old)+ if err != nil {+ log.Error("failed to convert old check constraint to string", zap.Error(err))return false}-- if old.Enforced != new.Enforced {+ newStr, err := toString(new)+ if err != nil {+ log.Error("failed to convert new check constraint to string", zap.Error(err))return false}- return true+ return oldStr == newStr}// diffTableOptions returns the diff of two table options, returns nil if they are the same.@@ -1231,16 +1216,17 @@ func isViewEqual(old, new *ast.CreateViewStmt) bool {// [WITH [CASCADED | LOCAL] CHECK OPTION]// We can easily replace view statement by using `CREATE OR REPLACE VIEW` statement to replace the old one.// So we don't need to compare each part, just compare the restore string.- var oldBuf, newBuf bytes.Buffer- oldRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &oldBuf)- newRestoreCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &newBuf)- if err := old.Restore(oldRestoreCtx); err != nil {+ oldViewStr, err := toString(old)+ if err != nil {+ log.Error("fail to convert old view to string", zap.Error(err))return false}- if err := new.Restore(newRestoreCtx); err != nil {+ newViewStr, err := toString(new)+ if err != nil {+ log.Error("fail to convert new view to string", zap.Error(err))return false}- return oldBuf.String() == newBuf.String()+ return oldViewStr == newViewStr}// buildTableOptionMap builds a map of table options.@@ -1251,39 +1237,3 @@ func buildTableOptionMap(options []*ast.TableOption) map[ast.TableOptionType]*as}return m}--// extractUnsupportObjNameAndType extract the object name from the CREATE TRIGGER/EVENT/FUNCTION/PROCEDURE statement and returns the object name and type.-func extractUnsupportObjNameAndType(stmt string) (string, objectType, error) {- fs := []objectType{- function,- procedure,- }- regexFmt := "(?i)^CREATE\\s+(DEFINER=`(.)+`@`(.)+`(\\s)+)?%s\\s+(?P<OBJECT_NAME>%s)(\\s)*\\("- namingRegex := "`[^\\\\/?%*:|\\\"`<>]+`"- for _, obj := range fs {- regex := fmt.Sprintf(regexFmt, string(obj), namingRegex)- re := regexp.MustCompile(regex)- matchList := re.FindStringSubmatch(stmt)- index := re.SubexpIndex("OBJECT_NAME")- if index >= 0 && index < len(matchList) {- objectName := strings.Trim(matchList[index], "`")- return objectName, obj, nil- }- }- objects := []objectType{- trigger,- event,- }- regexFmt = "(?i)^CREATE\\s+(DEFINER=`(.)+`@`(.)+`(\\s)+)?%s\\s+(?P<OBJECT_NAME>%s)(\\s)+"- for _, obj := range objects {- regex := fmt.Sprintf(regexFmt, string(obj), namingRegex)- re := regexp.MustCompile(regex)- matchList := re.FindStringSubmatch(stmt)- index := re.SubexpIndex("OBJECT_NAME")- if index >= 0 && index < len(matchList) {- objectName := strings.Trim(matchList[index], "`")- return objectName, obj, nil- }- }- return "", "", errors.Errorf("cannot extract object name and type from %q", stmt)-}diff --git a/plugin/parser/differ/mysql/utils.go b/plugin/parser/differ/mysql/utils.gonew file mode 100644index 0000000000..f82448cc25--- /dev/null+++ b/plugin/parser/differ/mysql/utils.go@@ -0,0 +1,67 @@+package mysql++import (+ "bytes"+ "fmt"+ "regexp"+ "strings"++ "github.com/pingcap/tidb/parser/ast"+ "github.com/pingcap/tidb/parser/format"++ "github.com/pkg/errors"+)++type objectType string++const (+ event objectType = "EVENT"+ function objectType = "FUNCTION"+ procedure objectType = "PROCEDURE"+ trigger objectType = "TRIGGER"+)++// extractUnsupportObjNameAndType extract the object name from the CREATE TRIGGER/EVENT/FUNCTION/PROCEDURE statement and returns the object name and type.+func extractUnsupportObjNameAndType(stmt string) (string, objectType, error) {+ fs := []objectType{+ function,+ procedure,+ }+ regexFmt := "(?i)^CREATE\\s+(DEFINER=`(.)+`@`(.)+`(\\s)+)?%s\\s+(?P<OBJECT_NAME>%s)(\\s)*\\("+ namingRegex := "`[^\\\\/?%*:|\\\"`<>]+`"+ for _, obj := range fs {+ regex := fmt.Sprintf(regexFmt, string(obj), namingRegex)+ re := regexp.MustCompile(regex)+ matchList := re.FindStringSubmatch(stmt)+ index := re.SubexpIndex("OBJECT_NAME")+ if index >= 0 && index < len(matchList) {+ objectName := strings.Trim(matchList[index], "`")+ return objectName, obj, nil+ }+ }+ objects := []objectType{+ trigger,+ event,+ }+ regexFmt = "(?i)^CREATE\\s+(DEFINER=`(.)+`@`(.)+`(\\s)+)?%s\\s+(?P<OBJECT_NAME>%s)(\\s)+"+ for _, obj := range objects {+ regex := fmt.Sprintf(regexFmt, string(obj), namingRegex)+ re := regexp.MustCompile(regex)+ matchList := re.FindStringSubmatch(stmt)+ index := re.SubexpIndex("OBJECT_NAME")+ if index >= 0 && index < len(matchList) {+ objectName := strings.Trim(matchList[index], "`")+ return objectName, obj, nil+ }+ }+ return "", "", errors.Errorf("cannot extract object name and type from %q", stmt)+}++func toString(node ast.Node) (string, error) {+ var buf bytes.Buffer+ restoreFlag := format.DefaultRestoreFlags | format.RestoreStringWithoutCharset+ if err := node.Restore(format.NewRestoreCtx(restoreFlag, &buf)); err != nil {+ return "", errors.Wrapf(err, "cannot restore node %v", node)+ }+ return buf.String(), nil+}diff --git a/plugin/parser/differ/mysql/unsupport_object_test.go b/plugin/parser/differ/mysql/utils_test.gosimilarity index 100%rename from plugin/parser/differ/mysql/unsupport_object_test.gorename to plugin/parser/differ/mysql/utils_test.go