Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(schema system): support MySQL InnoDB rule (#1268)
rebelice032bc8fc09a3
diff --git a/common/error.go b/common/error.goindex 11405b897d..c5ccec6d17 100644--- a/common/error.go+++ b/common/error.go@@ -66,6 +66,8 @@ const (// 10301 column rule advisor error codeNoRequiredColumn Code = 10301++ NotInnoDBEngine Code = 10401)// Error represents an application-specific error. Application errors can bediff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex ef58274668..2a47a53cc3 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -61,6 +61,9 @@ const (// MySQLSyntax is an advisor type for MySQL syntax.MySQLSyntax Type = "bb.plugin.advisor.mysql.syntax"+ // MySQLUseInnoDB is an advisor type for MySQL InnoDB Engine.+ MySQLUseInnoDB Type = "bb.plugin.advisor.mysql.use-innodb"+// MySQLMigrationCompatibility is an advisor type for MySQL migration compatibility.MySQLMigrationCompatibility Type = "bb.plugin.advisor.mysql.migration-compatibility"diff --git a/plugin/advisor/mysql/advisor_use_innodb.go b/plugin/advisor/mysql/advisor_use_innodb.gonew file mode 100644index 0000000000..2760c7bf89--- /dev/null+++ b/plugin/advisor/mysql/advisor_use_innodb.go@@ -0,0 +1,130 @@+package mysql++import (+ "fmt"+ "strings"++ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/db"+ "github.com/pingcap/tidb/parser/ast"+ "github.com/pingcap/tidb/parser/format"+)++const (+ innoDB string = "innodb"+ defaultStorageEngin string = "default_storage_engine"+)++var (+ _ advisor.Advisor = (*UseInnoDBAdvisor)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLUseInnoDB, &UseInnoDBAdvisor{})+}++// UseInnoDBAdvisor is the advisor checking for using InnoDB engine.+type UseInnoDBAdvisor struct {+}++// Check checks for using InnoDB engine.+func (adv *UseInnoDBAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ root, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySchemaReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ checker := &useInnoDBChecker{+ level: level,+ }++ for _, stmtNode := range root {+ (stmtNode).Accept(checker)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type useInnoDBChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+}++// Enter implements the ast.Visitor interface+func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {+ code := common.Ok+ switch node := in.(type) {+ // CREATE TABLE+ case *ast.CreateTableStmt:+ for _, option := range node.Options {+ if option.Tp == ast.TableOptionEngine && strings.ToLower(option.StrValue) != innoDB {+ code = common.NotInnoDBEngine+ break+ }+ }+ // ALTER TABLE+ case *ast.AlterTableStmt:+ for _, spec := range node.Specs {+ // TABLE OPTION+ if spec.Tp == ast.AlterTableOption {+ for _, option := range spec.Options {+ if option.Tp == ast.TableOptionEngine && strings.ToLower(option.StrValue) != innoDB {+ code = common.NotInnoDBEngine+ break+ }+ }+ }+ }+ // SET+ case *ast.SetStmt:+ for _, variable := range node.Variables {+ if strings.ToLower(variable.Name) == defaultStorageEngin {+ var buffer strings.Builder+ // Return lowercase+ ctx := format.NewRestoreCtx(format.RestoreNameLowercase, &buffer)+ if err := variable.Value.Restore(ctx); err != nil {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: common.Internal,+ Title: "Internal error for use InnoDB rule",+ Content: fmt.Sprintf("%q meet internal error %q", in.Text(), err.Error()),+ })+ continue+ }+ if buffer.String() != innoDB {+ code = common.NotInnoDBEngine+ break+ }+ }+ }+ }++ if code != common.Ok {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: code,+ Title: "InnoDB engine is not used",+ Content: fmt.Sprintf("%q doesn't use InnoDB engine", in.Text()),+ })+ }+ return in, false+}++// Leave implements the ast.Visitor interface+func (v *useInnoDBChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_use_innodb_test.go b/plugin/advisor/mysql/advisor_use_innodb_test.gonew file mode 100644index 0000000000..962bf91894--- /dev/null+++ b/plugin/advisor/mysql/advisor_use_innodb_test.go@@ -0,0 +1,96 @@+package mysql++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestUseInnoDB(t *testing.T) {+ tests := []test{+ {+ statement: "CREATE TABLE book(id int) ENGINE = INNODB",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "CREATE TABLE book(id int)",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "CREATE TABLE book(id int) ENGINE = CSV",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.NotInnoDBEngine,+ Title: "InnoDB engine is not used",+ Content: "\"CREATE TABLE book(id int) ENGINE = CSV\" doesn't use InnoDB engine",+ },+ },+ },+ {+ statement: "ALTER TABLE book ENGINE = INNODB",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "ALTER TABLE book ENGINE = CSV",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.NotInnoDBEngine,+ Title: "InnoDB engine is not used",+ Content: "\"ALTER TABLE book ENGINE = CSV\" doesn't use InnoDB engine",+ },+ },+ },+ {+ statement: "SET default_storage_engine=INNODB",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "SET default_storage_engine=CSV",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.NotInnoDBEngine,+ Title: "InnoDB engine is not used",+ Content: "\"SET default_storage_engine=CSV\" doesn't use InnoDB engine",+ },+ },+ },+ }+ runSchemaReviewRuleTests(t, tests, &UseInnoDBAdvisor{}, &api.SchemaReviewRule{+ Type: api.SchemaRuleMySQLEngine,+ Level: api.SchemaRuleLevelError,+ Payload: "",+ }, &MockCatalogService{})+}diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.goindex b61e6ce3fe..1a53daa6b0 100644--- a/server/task_check_executor_statement_advisor_composite.go+++ b/server/task_check_executor_statement_advisor_composite.go@@ -169,6 +169,11 @@ func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (adcase db.MySQL, db.TiDB:return advisor.MySQLColumnRequirement, nil}+ case api.SchemaRuleMySQLEngine:+ switch engine {+ case db.MySQL:+ return advisor.MySQLUseInnoDB, nil+ }}return advisor.Fake, fmt.Errorf("unknown schema review rule type %v for %v", ruleType, engine)}