Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Add more incompatible check
天舟d94ab14622ea
diff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.goindex a90b4db042..2f03ef9787 100644--- a/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go@@ -59,11 +59,82 @@ type compatibilityChecker struct {}func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {- if node, ok := in.(*ast.DropTableStmt); ok {+ compatible := true+ // DROP DATABASE+ if _, ok := in.(*ast.DropDatabaseStmt); ok {+ compatible = false+ goto END+ }+ // RENAME TABLE+ if _, ok := in.(*ast.RenameTableStmt); ok {+ compatible = false+ goto END+ }+ // DROP TABLE/VIEW+ if _, ok := in.(*ast.DropTableStmt); ok {+ compatible = false+ goto END+ }+ // ALTER TABLE+ if node, ok := in.(*ast.AlterTableStmt); ok {+ for _, spec := range node.Specs {+ fmt.Printf("spec %d: %+v\n\n", spec.Tp, spec)+ // RENAME COLUMN+ // DROP COLUMN+ if spec.Tp == ast.AlterTableRenameColumn ||+ spec.Tp == ast.AlterTableDropColumn {+ compatible = false+ goto END+ }++ if spec.Tp == ast.AlterTableAddConstraint {+ // ADD PRIMARY KEY+ // ADD UNIQUE/UNIQUE KEY/UNIQUE INDEX+ // ADD FOREIGN KEY+ if spec.Constraint.Tp == ast.ConstraintPrimaryKey ||+ spec.Constraint.Tp == ast.ConstraintUniq ||+ spec.Constraint.Tp == ast.ConstraintUniqKey ||+ spec.Constraint.Tp == ast.ConstraintUniqIndex ||+ spec.Constraint.Tp == ast.ConstraintForeignKey {+ compatible = false+ goto END+ }+ // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html+ // ADD CHECK ENFORCED+ if spec.Constraint.Tp == ast.ConstraintCheck && spec.Constraint.Enforced {+ compatible = false+ goto END+ }+ }++ // Check is only supported after 8.0.16 https://dev.mysql.com/doc/refman/8.0/en/create-table-check-constraints.html+ // ALTER CHECK ENFORCED+ if spec.Tp == ast.AlterTableAlterCheck {+ if spec.Constraint.Enforced {+ compatible = false+ goto END+ }+ }+ }+ }+ // ALTER VIEW TBD: https://github.com/pingcap/parser/pull/1252+ // if node, ok := in.(*ast.AlterViewStmt); ok {+ // }++ // CREATE UNIQUE INDEX+ if node, ok := in.(*ast.CreateIndexStmt); ok {+ if node.KeyType == ast.IndexKeyTypeUnique {+ compatible = false+ goto END+ }+ }++END:+ if !compatible {v.advisorList = append(v.advisorList, advisor.Advice{Status: advisor.Warn,Title: "Incompatible migration",- Content: fmt.Sprintf("%s is backward incompatible", node.Text()),+ Content: fmt.Sprintf("%s is backward incompatible", in.Text()),})}return in, falsediff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.goindex 86e0d32d8b..73f15aef87 100644--- a/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go@@ -8,7 +8,7 @@ import ("go.uber.org/zap")-func TestMigraionCompatibilityAdvisor(t *testing.T) {+func TestBasic(t *testing.T) {type test struct {statement stringwant []advisor.Advice@@ -16,12 +16,12 @@ func TestMigraionCompatibilityAdvisor(t *testing.T) {tests := []test{{- statement: "ALTER TABLE t1 ADD f1 TEXT",+ statement: "DROP DATABASE d1",want: []advisor.Advice{{- Status: advisor.Success,- Title: "OK",- Content: "Migration is backward compatible",+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "DROP DATABASE d1 is backward incompatible",},},},@@ -35,6 +35,36 @@ func TestMigraionCompatibilityAdvisor(t *testing.T) {},},},+ {+ statement: "RENAME TABLE t1 to t2",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "RENAME TABLE t1 to t2 is backward incompatible",+ },+ },+ },+ {+ statement: "DROP VIEW v1",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "DROP VIEW v1 is backward incompatible",+ },+ },+ },+ {+ statement: "CREATE UNIQUE INDEX idx1 ON t1 (f1)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "CREATE UNIQUE INDEX idx1 ON t1 (f1) is backward incompatible",+ },+ },+ },{statement: "DROP TABLE t1;DROP TABLE t2;",want: []advisor.Advice{@@ -70,3 +100,171 @@ func TestMigraionCompatibilityAdvisor(t *testing.T) {}}}++func TestAlterTable(t *testing.T) {+ type test struct {+ statement string+ want []advisor.Advice+ }++ tests := []test{+ {+ statement: "ALTER TABLE t1 ADD f1 TEXT",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Title: "OK",+ Content: "Migration is backward compatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 RENAME COLUMN f1 to f2",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 RENAME COLUMN f1 to f2 is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 DROP COLUMN f1",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 DROP COLUMN f1 is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD PRIMARY KEY (f1)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD PRIMARY KEY (f1) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD UNIQUE (f1)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD UNIQUE (f1) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD UNIQUE KEY (f1)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD UNIQUE KEY (f1) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD UNIQUE INDEX (f1)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD UNIQUE INDEX (f1) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD CHECK (f1 > 0)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD CHECK (f1 > 0) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD CHECK (f1 > 0) NOT ENFORCED",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Title: "OK",+ Content: "Migration is backward compatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ALTER CHECK chk1 ENFORCED",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ALTER CHECK chk1 ENFORCED is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ALTER CHECK chk1 NOT ENFORCED",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Title: "OK",+ Content: "Migration is backward compatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0)",+ want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Title: "Incompatible migration",+ Content: "ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0) is backward incompatible",+ },+ },+ },+ {+ statement: "ALTER TABLE t1 ADD CONSTRAINT CHECK (f1 > 0) NOT ENFORCED",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Title: "OK",+ Content: "Migration is backward compatible",+ },+ },+ },+ }++ adv := CompatibilityAdvisor{}+ logger, _ := zap.NewDevelopmentConfig().Build()+ ctx := advisor.AdvisorContext{+ Logger: logger,+ Charset: "",+ Collation: "",+ }+ for _, tc := range tests {+ adviceList, err := adv.Check(ctx, tc.statement)+ if err != nil {+ t.Errorf("statement=%s: expected no error, got %v", tc.statement, err)+ } else {+ if !reflect.DeepEqual(tc.want, adviceList) {+ t.Errorf("statement=%s: expected %+v, got %+v", tc.statement, tc.want, adviceList)+ }+ }+ }+}