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 no leading wildcard LIKE rule (#1285)
rebelice29b07fd74965
diff --git a/common/error.go b/common/error.goindex 973c2586dd..63062d2216 100644--- a/common/error.go+++ b/common/error.go@@ -49,8 +49,9 @@ const (CompatibilityAlterCheck Code = 10010CompatibilityAlterColumn Code = 10011- StatementNoWhere Code = 10101- StatementSelectAll Code = 10102+ StatementNoWhere Code = 10101+ StatementSelectAll Code = 10102+ StatementLeadingWildcardLike Code = 10103// 10201 table naming advisor error codeNamingTableConventionMismatch Code = 10201diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex f4939adab2..0c71ce75bf 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -70,6 +70,9 @@ const (// MySQLWhereRequirement is an advisor type for MySQL WHERE clause requirement.MySQLWhereRequirement Type = "bb.plugin.advisor.mysql.where.require"+ // MySQLNoLeadingWildcardLike is an advisor type for MySQL no leading wildcard LIKE.+ MySQLNoLeadingWildcardLike Type = "bb.plugin.advisor.mysql.where.no-leading-wildcard-like"+// MySQLNamingTableConvention is an advisor type for MySQL table naming convention.MySQLNamingTableConvention Type = "bb.plugin.advisor.mysql.naming.table"diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.gonew file mode 100644index 0000000000..3eb803944b--- /dev/null+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like.go@@ -0,0 +1,99 @@+package mysql++import (+ "fmt"++ "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 (+ wildcard string = "%"+)++var (+ _ advisor.Advisor = (*NoLeadingWildcardLikeAdvisor)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLNoLeadingWildcardLike, &NoLeadingWildcardLikeAdvisor{})+}++// NoLeadingWildcardLikeAdvisor is the advisor checking for no leading wildcard LIKE.+type NoLeadingWildcardLikeAdvisor struct {+}++// Check checks for no leading wildcard LIKE.+func (adv *NoLeadingWildcardLikeAdvisor) 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 := &noLeadingWildcardLikeChecker{level: level}+ for _, stmtNode := range root {+ checker.text = stmtNode.Text()+ checker.leadingWildcardLike = false+ (stmtNode).Accept(checker)++ if checker.leadingWildcardLike {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: common.StatementLeadingWildcardLike,+ Title: "No leading wildcard LIKE",+ Content: fmt.Sprintf("%q uses leading wildcard LIKE", checker.text),+ })+ }+ }++ 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 noLeadingWildcardLikeChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ text string+ leadingWildcardLike bool+}++// Enter implements the ast.Visitor interface+func (v *noLeadingWildcardLikeChecker) Enter(in ast.Node) (ast.Node, bool) {+ if node, ok := in.(*ast.PatternLikeExpr); !v.leadingWildcardLike && ok {+ pattern, err := restoreNode(node.Pattern, format.RestoreStringWithoutCharset)+ if err != nil {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: common.Internal,+ Title: "Internal error for no leading wildcard LIKE rule",+ Content: fmt.Sprintf("%q meet internal error %q", v.text, err.Error()),+ })+ }+ if len(pattern) > 0 && pattern[:1] == wildcard {+ v.leadingWildcardLike = true+ }+ }+ return in, false+}++// Leave implements the ast.Visitor interface+func (v *noLeadingWildcardLikeChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.gonew file mode 100644index 0000000000..617b4ddabe--- /dev/null+++ b/plugin/advisor/mysql/advisor_stmt_no_leading_wildcard_like_test.go@@ -0,0 +1,64 @@+package mysql++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestNoLeadingWildcardLike(t *testing.T) {+ tests := []test{+ {+ statement: "SELECT * FROM t WHERE a LIKE 'abc%'",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ statement: "SELECT * FROM t WHERE a LIKE '%abc'",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.StatementLeadingWildcardLike,+ Title: "No leading wildcard LIKE",+ Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",+ },+ },+ },+ {+ statement: "SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.StatementLeadingWildcardLike,+ Title: "No leading wildcard LIKE",+ Content: "\"SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'\" uses leading wildcard LIKE",+ },+ },+ },+ {+ statement: "SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.StatementLeadingWildcardLike,+ Title: "No leading wildcard LIKE",+ Content: "\"SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'\" uses leading wildcard LIKE",+ },+ },+ },+ }++ runSchemaReviewRuleTests(t, tests, &NoLeadingWildcardLikeAdvisor{}, &api.SchemaReviewRule{+ Type: api.SchemaRuleStatementNoLeadingWildcardLike,+ Level: api.SchemaRuleLevelError,+ Payload: "",+ }, &MockCatalogService{})+}diff --git a/plugin/advisor/mysql/advisor_use_innodb.go b/plugin/advisor/mysql/advisor_use_innodb.goindex 2760c7bf89..4a420c2132 100644--- a/plugin/advisor/mysql/advisor_use_innodb.go+++ b/plugin/advisor/mysql/advisor_use_innodb.go@@ -93,10 +93,9 @@ func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {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 {+ text, err := restoreNode(variable.Value, format.RestoreNameLowercase)+ if err != nil {v.adviceList = append(v.adviceList, advisor.Advice{Status: v.level,Code: common.Internal,@@ -105,7 +104,7 @@ func (v *useInnoDBChecker) Enter(in ast.Node) (ast.Node, bool) {})continue}- if buffer.String() != innoDB {+ if text != innoDB {code = common.NotInnoDBEnginebreak}diff --git a/plugin/advisor/mysql/utils.go b/plugin/advisor/mysql/utils.goindex c7a75b92cf..f58a7fa0ee 100644--- a/plugin/advisor/mysql/utils.go+++ b/plugin/advisor/mysql/utils.go@@ -2,6 +2,10 @@ package mysqlimport ("sort"+ "strings"++ "github.com/pingcap/tidb/parser/ast"+ "github.com/pingcap/tidb/parser/format")type columnSet map[string]bool@@ -16,3 +20,12 @@ func (t tableState) tableList() []string {sort.Strings(tableList)return tableList}++func restoreNode(node ast.Node, flag format.RestoreFlags) (string, error) {+ var buffer strings.Builder+ ctx := format.NewRestoreCtx(flag, &buffer)+ if err := node.Restore(ctx); err != nil {+ return "", err+ }+ return buffer.String(), nil+}diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.goindex de90c20edb..2515305334 100644--- a/server/task_check_executor_statement_advisor_composite.go+++ b/server/task_check_executor_statement_advisor_composite.go@@ -129,6 +129,11 @@ func getAdvisorTypeByRule(ruleType api.SchemaReviewRuleType, engine db.Type) (adcase db.MySQL, db.TiDB:return advisor.MySQLWhereRequirement, nil}+ case api.SchemaRuleStatementNoLeadingWildcardLike:+ switch engine {+ case db.MySQL, db.TiDB:+ return advisor.MySQLNoLeadingWildcardLike, nil+ }case api.SchemaRuleStatementNoSelectAll:switch engine {case db.MySQL, db.TiDB: