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 SELECT all rule (#1278)
rebelice6d8237296d5e
diff --git a/common/error.go b/common/error.goindex c5ccec6d17..534f98d064 100644--- a/common/error.go+++ b/common/error.go@@ -49,7 +49,8 @@ const (CompatibilityAlterCheck Code = 10010CompatibilityAlterColumn Code = 10011- StatementNoWhere Code = 10101+ StatementNoWhere Code = 10101+ StatementSelectAll Code = 10102// 10201 table naming advisor error codeNamingTableConventionMismatch Code = 10201diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex 2a47a53cc3..304d8464c7 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -90,6 +90,9 @@ const (// MySQLColumnRequirement is an advisor type for MySQL column requirement.MySQLColumnRequirement Type = "bb.plugin.advisor.mysql.column.require"++ // MySQLNoSelectAll is an advisor type for MySQL no select all.+ MySQLNoSelectAll Type = "bb.plugin.advisor.mysql.select.no-select-all")// Advice is the result of an advisor.diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all.go b/plugin/advisor/mysql/advisor_stmt_no_select_all.gonew file mode 100644index 0000000000..7d2a27cb5d--- /dev/null+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all.go@@ -0,0 +1,79 @@+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"+)++var (+ _ advisor.Advisor = (*NoSelectAllAdvisor)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLNoSelectAll, &NoSelectAllAdvisor{})+}++// NoSelectAllAdvisor is the advisor checking for no "select *".+type NoSelectAllAdvisor struct {+}++// Check checks for no "select *".+func (adv *NoSelectAllAdvisor) 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 := &noSelectAllChecker{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 noSelectAllChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+}++// Enter implements the ast.Visitor interface+func (v *noSelectAllChecker) Enter(in ast.Node) (ast.Node, bool) {+ if node, ok := in.(*ast.SelectStmt); ok {+ for _, field := range node.Fields.Fields {+ if field.WildCard != nil {+ v.adviceList = append(v.adviceList, advisor.Advice{+ Status: v.level,+ Code: common.StatementSelectAll,+ Title: "No SELECT all",+ Content: fmt.Sprintf("%q uses SELECT all", in.Text()),+ })+ break+ }+ }++ }+ return in, false+}++// Leave implements the ast.Visitor interface+func (v *noSelectAllChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.gonew file mode 100644index 0000000000..f93d42e3e8--- /dev/null+++ b/plugin/advisor/mysql/advisor_stmt_no_select_all_test.go@@ -0,0 +1,42 @@+package mysql++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestNoSelectAll(t *testing.T) {+ tests := []test{+ {+ statement: "SELECT * FROM t",+ want: []advisor.Advice{+ {+ Status: advisor.Error,+ Code: common.StatementSelectAll,+ Title: "No SELECT all",+ Content: "\"SELECT * FROM t\" uses SELECT all",+ },+ },+ },+ {+ statement: "SELECT a, b FROM t",+ want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: common.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ }++ runSchemaReviewRuleTests(t, tests, &NoSelectAllAdvisor{}, &api.SchemaReviewRule{+ Type: api.SchemaRuleStatementNoSelectAll,+ 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 1a53daa6b0..cb31a2d890 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.SchemaRuleStatementNoSelectAll:+ switch engine {+ case db.MySQL, db.TiDB:+ return advisor.MySQLNoSelectAll, nil+ }case api.SchemaRuleSchemaBackwardCompatibility:switch engine {case db.MySQL, db.TiDB: