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(sql-review): implement MySQL insert-must-specify-column rule (#2943)
rebelice8e5e63f285b2
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex f6e29a18e0..c98ac1976b 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -163,6 +163,9 @@ const (// MySQLInsertRowLimit is an advisor type for MySQL to limit INSERT rows.MySQLInsertRowLimit Type = "bb.plugin.advisor.mysql.insert.row-limit"+ // MySQLInsertMustSpecifyColumn is an advisor type for MySQL to enforce column specified.+ MySQLInsertMustSpecifyColumn Type = "bb.plugin.advisor.mysql.insert.must-specify-column"+// MySQLDisallowOrderBy is an advisor type for MySQL no ORDER BY clause in DELETE/UPDATE statement.MySQLDisallowOrderBy Type = "bb.plugin.advisor.mysql.statement.disallow-order-by"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex c122abc469..f0b518ca71 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -98,13 +98,14 @@ const (// 1001 ~ 1099 charset error code.DisabledCharset Code = 1001- // 1101 ~ 1199 insert/update error code.- InsertTooManyRows Code = 1101- UpdateUseLimit Code = 1102- InsertUseLimit Code = 1103- UpdateUseOrderBy Code = 1104- DeleteUseOrderBy Code = 1105- DeleteUseLimit Code = 1106+ // 1101 ~ 1199 insert/update/delete error code.+ InsertTooManyRows Code = 1101+ UpdateUseLimit Code = 1102+ InsertUseLimit Code = 1103+ UpdateUseOrderBy Code = 1104+ DeleteUseOrderBy Code = 1105+ DeleteUseLimit Code = 1106+ InsertNotSpecifyColumn Code = 1107)// Int returns the int type of code.diff --git a/plugin/advisor/mysql/advisor_insert_must_specify_column.go b/plugin/advisor/mysql/advisor_insert_must_specify_column.gonew file mode 100644index 0000000000..a7ce7ce928--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_must_specify_column.go@@ -0,0 +1,89 @@+package mysql++// Framework code is generated by the generator.++import (+ "fmt"++ "github.com/pingcap/tidb/parser/ast"++ "github.com/bytebase/bytebase/plugin/advisor"+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++var (+ _ advisor.Advisor = (*InsertMustSpecifyColumnAdvisor)(nil)+ _ ast.Visitor = (*insertMustSpecifyColumnChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLInsertMustSpecifyColumn, &InsertMustSpecifyColumnAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLInsertMustSpecifyColumn, &InsertMustSpecifyColumnAdvisor{})+}++// InsertMustSpecifyColumnAdvisor is the advisor checking for to enforce column specified.+type InsertMustSpecifyColumnAdvisor struct {+}++// Check checks for to enforce column specified.+func (*InsertMustSpecifyColumnAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {+ stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)+ if errAdvice != nil {+ return errAdvice, nil+ }++ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)+ if err != nil {+ return nil, err+ }+ checker := &insertMustSpecifyColumnChecker{+ level: level,+ title: string(ctx.Rule.Type),+ }++ for _, stmt := range stmtList {+ checker.text = stmt.Text()+ checker.line = stmt.OriginTextPosition()+ (stmt).Accept(checker)+ }++ 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 insertMustSpecifyColumnChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (checker *insertMustSpecifyColumnChecker) Enter(in ast.Node) (ast.Node, bool) {+ if node, ok := in.(*ast.InsertStmt); ok {+ if node.Columns == nil {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.InsertNotSpecifyColumn,+ Title: checker.title,+ Content: fmt.Sprintf("The INSERT statement must specify columns but \"%s\" does not", checker.text),+ Line: checker.line,+ })+ }+ }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*insertMustSpecifyColumnChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}diff --git a/plugin/advisor/mysql/advisor_insert_must_specify_column_test.go b/plugin/advisor/mysql/advisor_insert_must_specify_column_test.gonew file mode 100644index 0000000000..415a0a936d--- /dev/null+++ b/plugin/advisor/mysql/advisor_insert_must_specify_column_test.go@@ -0,0 +1,43 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestInsertMustSpecifyColumn(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `INSERT INTO tech_book(id, name) VALUES (1, '1')`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `INSERT INTO tech_book VALUES (1, '1')`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.InsertNotSpecifyColumn,+ Title: "insert.must-specify-column",+ Content: "The INSERT statement must specify columns but \"INSERT INTO tech_book VALUES (1, '1')\" does not",+ Line: 1,+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &InsertMustSpecifyColumnAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleInsertMustSpecifyColumn,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 65cbe13bee..68fc7a7464 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -120,6 +120,8 @@ const (// SchemaRuleInsertRowLimit enforce the insert row limit.SchemaRuleInsertRowLimit SQLReviewRuleType = "insert.row-limit"+ // SchemaRuleInsertMustSpecifyColumn enforce the insert column specified.+ SchemaRuleInsertMustSpecifyColumn SQLReviewRuleType = "insert.must-specify-column"// TableNameTemplateToken is the token for table name.TableNameTemplateToken = "{{table}}"@@ -797,6 +799,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLInsertRowLimit, nil}+ case SchemaRuleInsertMustSpecifyColumn:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLInsertMustSpecifyColumn, nil+ }case SchemaRuleStatementDisallowLimit:switch engine {case db.MySQL, db.TiDB: