Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add migration_compatibility_advisor

天舟83672b2b6f44
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 1a57c634c9..b925785325 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -33,8 +33,9 @@ func (e Status) String() string {
type AdvisorType string
const (
- Fake AdvisorType = "bb.plugin.advisor.fake"
- MySQLSyntax AdvisorType = "bb.plugin.advisor.mysql.syntax"
+ Fake AdvisorType = "bb.plugin.advisor.fake"
+ MySQLSyntax AdvisorType = "bb.plugin.advisor.mysql.syntax"
+ MySQLMigrationCompatibility AdvisorType = "bb.plugin.advisor.mysql.migration-compatibility"
)
type Advice struct {
diff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go
new file mode 100644
index 0000000000..a90b4db042
--- /dev/null
+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor.go
@@ -0,0 +1,74 @@
+package mysql
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/db"
+
+ "github.com/pingcap/parser"
+ "github.com/pingcap/parser/ast"
+ _ "github.com/pingcap/tidb/types/parser_driver"
+)
+
+var (
+ _ advisor.Advisor = (*CompatibilityAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLMigrationCompatibility, &CompatibilityAdvisor{})
+}
+
+type CompatibilityAdvisor struct {
+}
+
+// A fake advisor to report 1 advice for each severity.
+func (adv *CompatibilityAdvisor) Check(ctx advisor.AdvisorContext, statement string) ([]advisor.Advice, error) {
+ p := parser.New()
+
+ root, _, err := p.Parse(statement, ctx.Charset, ctx.Collation)
+ if err != nil {
+ return []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Title: "Syntax error",
+ Content: err.Error(),
+ },
+ }, nil
+ }
+
+ c := &compatibilityChecker{}
+ for _, stmtNode := range root {
+ fmt.Printf("%+v\n", stmtNode)
+ (stmtNode).Accept(c)
+ fmt.Printf("%+v\n", c)
+ }
+
+ if len(c.advisorList) == 0 {
+ c.advisorList = append(c.advisorList, advisor.Advice{
+ Status: advisor.Success,
+ Title: "OK",
+ Content: "Migration is backward compatible"})
+ }
+ return c.advisorList, nil
+}
+
+type compatibilityChecker struct {
+ advisorList []advisor.Advice
+}
+
+func (v *compatibilityChecker) Enter(in ast.Node) (ast.Node, bool) {
+ if node, ok := in.(*ast.DropTableStmt); ok {
+ v.advisorList = append(v.advisorList, advisor.Advice{
+ Status: advisor.Warn,
+ Title: "Incompatible migration",
+ Content: fmt.Sprintf("%s is backward incompatible", node.Text()),
+ })
+ }
+ return in, false
+}
+
+func (v *compatibilityChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go
new file mode 100644
index 0000000000..86e0d32d8b
--- /dev/null
+++ b/plugin/advisor/mysql/mysql_migration_compatibility_advisor_test.go
@@ -0,0 +1,72 @@
+package mysql
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "go.uber.org/zap"
+)
+
+func TestMigraionCompatibilityAdvisor(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: "DROP TABLE t1",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Title: "Incompatible migration",
+ Content: "DROP TABLE t1 is backward incompatible",
+ },
+ },
+ },
+ {
+ statement: "DROP TABLE t1;DROP TABLE t2;",
+ want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Title: "Incompatible migration",
+ Content: "DROP TABLE t1; is backward incompatible",
+ },
+ {
+ Status: advisor.Warn,
+ Title: "Incompatible migration",
+ Content: "DROP TABLE t2; is backward incompatible",
+ },
+ },
+ },
+ }
+
+ 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)
+ }
+ }
+ }
+}
diff --git a/plugin/advisor/mysql/mysql_syntax_advisor.go b/plugin/advisor/mysql/mysql_syntax_advisor.go
index b3c0eb306c..b1a103f150 100644
--- a/plugin/advisor/mysql/mysql_syntax_advisor.go
+++ b/plugin/advisor/mysql/mysql_syntax_advisor.go
@@ -10,19 +10,19 @@ import (
)
var (
- _ advisor.Advisor = (*Advisor)(nil)
+ _ advisor.Advisor = (*SyntaxAdvisor)(nil)
)
func init() {
- advisor.Register(db.MySQL, advisor.MySQLSyntax, &Advisor{})
- advisor.Register(db.TiDB, advisor.MySQLSyntax, &Advisor{})
+ advisor.Register(db.MySQL, advisor.MySQLSyntax, &SyntaxAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLSyntax, &SyntaxAdvisor{})
}
-type Advisor struct {
+type SyntaxAdvisor struct {
}
// A fake advisor to report 1 advice for each severity.
-func (adv *Advisor) Check(ctx advisor.AdvisorContext, statement string) ([]advisor.Advice, error) {
+func (adv *SyntaxAdvisor) Check(ctx advisor.AdvisorContext, statement string) ([]advisor.Advice, error) {
p := parser.New()
_, warns, err := p.Parse(statement, ctx.Charset, ctx.Collation)