Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: migrate mysql_use_innodb rule (#8907)
zll600d7fd5e8043e6
diff --git a/backend/plugin/advisor/mysqlwip/advisor_use_innodb.go b/backend/plugin/advisor/mysqlwip/advisor_use_innodb.gonew file mode 100644index 0000000000..9aab5a900f--- /dev/null+++ b/backend/plugin/advisor/mysqlwip/advisor_use_innodb.go@@ -0,0 +1,154 @@+package mysqlwip++import (+ "fmt"+ "strings"++ "github.com/antlr4-go/antlr/v4"+ "github.com/pkg/errors"++ mysql "github.com/bytebase/mysql-parser"++ "github.com/bytebase/bytebase/backend/plugin/advisor"+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+)++const (+ innoDB string = "innodb"+ defaultStorageEngin string = "default_storage_engine"+)++var _ advisor.Advisor = (*UseInnoDBAdvisor)(nil)++func init() {+ // only for mysqlwip test.+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLUseInnoDB, &UseInnoDBAdvisor{})+}++// UseInnoDBAdvisor is the advisor checking for using InnoDB engine.+type UseInnoDBAdvisor struct {+}++// Check checks for using InnoDB engine.+func (*UseInnoDBAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {+ list, ok := ctx.AST.([]*mysqlparser.ParseResult)+ if !ok {+ return nil, errors.Errorf("failed to convert to Tree")+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }++ checker := &useInnoDBChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range list {+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)+ }++ if len(checker.adviceList) == 0 {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ })+ }+ return checker.adviceList, nil+}++type useInnoDBChecker struct {+ *mysql.BaseMySQLParserListener++ adviceList []advisor.Advice+ level advisor.Status+ title string+}++// EnterCreateTable is called when production createTable is entered.+func (c *useInnoDBChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {+ if ctx.CreateTableOptions() == nil {+ return+ }+ code := advisor.Ok+ for _, tableOption := range ctx.CreateTableOptions().AllCreateTableOption() {+ if tableOption.ENGINE_SYMBOL() != nil && tableOption.EngineRef() != nil {+ engine := mysqlparser.NormalizeMySQLTextOrIdentifier(tableOption.EngineRef().TextOrIdentifier())+ if strings.ToLower(engine) != innoDB {+ code = advisor.NotInnoDBEngine+ break+ }+ }+ }++ if code != advisor.Ok {+ c.adviceList = append(c.adviceList, advisor.Advice{+ Status: c.level,+ Code: code,+ Title: c.title,+ Content: fmt.Sprintf("\"CREATE %s;\" doesn't use InnoDB engine", ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)),+ Line: ctx.GetStart().GetLine(),+ })+ }+}++func (c *useInnoDBChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {+ code := advisor.Ok+ for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {+ for _, op := range option.AllCreateTableOption() {+ switch {+ case op.ENGINE_SYMBOL() != nil:+ engine := op.EngineRef().GetText()+ if strings.ToLower(engine) != innoDB {+ code = advisor.NotInnoDBEngine+ break+ }+ default:+ }+ }+ }++ if code != advisor.Ok {+ c.adviceList = append(c.adviceList, advisor.Advice{+ Status: c.level,+ Code: code,+ Title: c.title,+ Content: fmt.Sprintf("\"ALTER %s;\" doesn't use InnoDB engine", ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)),+ Line: ctx.GetStart().GetLine(),+ })+ }+}++func (c *useInnoDBChecker) EnterSetStatement(ctx *mysql.SetStatementContext) {+ code := advisor.Ok+ if ctx.StartOptionValueList() == nil {+ return+ }++ startOptionValueList := ctx.StartOptionValueList()+ if startOptionValueList.OptionValueNoOptionType() == nil {+ return+ }+ optionValueNoOptionType := startOptionValueList.OptionValueNoOptionType()+ if optionValueNoOptionType.SetExprOrDefault() != nil {+ engine := optionValueNoOptionType.SetExprOrDefault().GetText()+ if strings.ToLower(engine) != innoDB {+ code = advisor.NotInnoDBEngine+ }+ }++ if code != advisor.Ok {+ c.adviceList = append(c.adviceList, advisor.Advice{+ Status: c.level,+ Code: code,+ Title: c.title,+ Content: fmt.Sprintf("\"%s;\" doesn't use InnoDB engine", ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)),+ Line: ctx.GetStart().GetLine(),+ })+ }+}diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.goindex 3b2ba5547d..900dc5682e 100644--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go@@ -9,6 +9,8 @@ import (func TestMySQLWIPRules(t *testing.T) {mysqlwipRules := []advisor.SQLReviewRuleType{+ // advisor.SchemaRuleMySQLEngine enforce the innodb engine.+ advisor.SchemaRuleMySQLEngine,// advisor.SchemaRuleTableNaming enforce the table name format.advisor.SchemaRuleTableNaming,}diff --git a/backend/plugin/advisor/mysqlwip/test/engine_mysql_use_innodb.yaml b/backend/plugin/advisor/mysqlwip/test/engine_mysql_use_innodb.yamlindex 70190f0dda..614261d613 100644--- a/backend/plugin/advisor/mysqlwip/test/engine_mysql_use_innodb.yaml+++ b/backend/plugin/advisor/mysqlwip/test/engine_mysql_use_innodb.yaml@@ -1,4 +1,4 @@-- statement: CREATE TABLE book(id int) ENGINE = INNODB+- statement: CREATE TABLE book(id int) ENGINE = INNODB;want:- status: SUCCESScode: 0@@ -6,7 +6,7 @@content: ""line: 0details: ""-- statement: CREATE TABLE book(id int)+- statement: CREATE TABLE book(id int);want:- status: SUCCESScode: 0@@ -14,17 +14,16 @@content: ""line: 0details: ""-- statement: CREATE TABLE book(id int) ENGINE = CSV+- statement: CREATE TABLE book(id int) ENGINE = CSV;want:- status: WARNcode: 501title: engine.mysql.use-innodbcontent: |-- "CREATE TABLE book(id int) ENGINE = CSV- ;" doesn't use InnoDB engine- line: 2+ "CREATE TABLE book(id int) ENGINE = CSV;" doesn't use InnoDB engine+ line: 1details: ""-- statement: ALTER TABLE tech_book ENGINE = INNODB+- statement: ALTER TABLE tech_book ENGINE = INNODB;want:- status: SUCCESScode: 0@@ -32,17 +31,16 @@content: ""line: 0details: ""-- statement: ALTER TABLE tech_book ENGINE = CSV+- statement: ALTER TABLE tech_book ENGINE = CSV;want:- status: WARNcode: 501title: engine.mysql.use-innodbcontent: |-- "ALTER TABLE tech_book ENGINE = CSV- ;" doesn't use InnoDB engine- line: 2+ "ALTER TABLE tech_book ENGINE = CSV;" doesn't use InnoDB engine+ line: 1details: ""-- statement: SET default_storage_engine=INNODB+- statement: SET default_storage_engine=INNODB;want:- status: SUCCESScode: 0@@ -50,13 +48,12 @@content: ""line: 0details: ""-- statement: SET default_storage_engine=CSV+- statement: SET default_storage_engine=CSV;want:- status: WARNcode: 501title: engine.mysql.use-innodbcontent: |-- "SET default_storage_engine=CSV- ;" doesn't use InnoDB engine- line: 2+ "SET default_storage_engine=CSV;" doesn't use InnoDB engine+ line: 1details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex b6368bd6db..1965126f3a 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1314,6 +1314,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty}case SchemaRuleMySQLEngine:switch engine {+ // only for mysqlwip test.+ case storepb.Engine_ENGINE_UNSPECIFIED:+ return MySQLUseInnoDB, nilcase storepb.Engine_MYSQL, storepb.Engine_MARIADB:return MySQLUseInnoDB, nil}diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.goindex aac98763e7..ba2d06c270 100644--- a/backend/plugin/parser/mysql/normalize.go+++ b/backend/plugin/parser/mysql/normalize.go@@ -77,6 +77,16 @@ func NormalizeMySQLIdentifier(identifier parser.IIdentifierContext) string {return identifier.GetText()}+// NormalizeMySQLTextOrIdentifier normalizes the given TextOrIdentifier.+func NormalizeMySQLTextOrIdentifier(ctx parser.ITextOrIdentifierContext) string {+ if ctx.Identifier() != nil {+ return NormalizeMySQLIdentifier(ctx.Identifier())+ }+ textString := ctx.TextStringLiteral().GetText()+ // remove the quotations.+ return textString[1 : len(textString)-1]+}+// NormalizeMySQLSelectAlias normalizes the given select alias.func NormalizeMySQLSelectAlias(selectAlias parser.ISelectAliasContext) string {if selectAlias.Identifier() != nil {