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 table comment requirement rule (#2468)
rebelice0ac2c2628744
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex a9909f0549..6118aa6731 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -118,6 +118,9 @@ const (// MySQLTableExists is an advisor type for MySQL table existence.MySQLTableExists Type = "bb.plugin.advisor.mysql.table.exists"+ // MySQLTableCommentRequirement is an advisor for MySQL table comment requirement.+ MySQLTableCommentRequirement Type = "bb.plugin.advisor.mysql.table.comment-require"+// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"diff --git a/plugin/advisor/code.go b/plugin/advisor/code.goindex d209de231f..7bec97752f 100644--- a/plugin/advisor/code.go+++ b/plugin/advisor/code.go@@ -60,6 +60,7 @@ const (TableHasFK Code = 602TableDropNamingConventionMismatch Code = 603TableNotExists Code = 604+ NoTableComment Code = 605// 701 ~ 799 database advisor error code.DatabaseNotEmpty Code = 701diff --git a/plugin/advisor/mysql/advisor_table_comment_require.go b/plugin/advisor/mysql/advisor_table_comment_require.gonew file mode 100644index 0000000000..275422290e--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_comment_require.go@@ -0,0 +1,99 @@+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 = (*TableCommentRequireAdvisor)(nil)+ _ ast.Visitor = (*tableCommentRequireChecker)(nil)+)++func init() {+ advisor.Register(db.MySQL, advisor.MySQLTableCommentRequirement, &TableCommentRequireAdvisor{})+ advisor.Register(db.TiDB, advisor.MySQLTableCommentRequirement, &TableCommentRequireAdvisor{})+}++// TableCommentRequireAdvisor is the advisor checking for table comment requirement.+type TableCommentRequireAdvisor struct {+}++// Check checks for table comment requirement.+func (*TableCommentRequireAdvisor) 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 := &tableCommentRequireChecker{+ 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 tableCommentRequireChecker struct {+ adviceList []advisor.Advice+ level advisor.Status+ title string+ text string+ line int+}++// Enter implements the ast.Visitor interface.+func (checker *tableCommentRequireChecker) Enter(in ast.Node) (ast.Node, bool) {+ if node, ok := in.(*ast.CreateTableStmt); ok {+ if tableNoComment(node.Options) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.NoTableComment,+ Title: checker.title,+ Content: fmt.Sprintf("Table `%s` requires comments", node.Table.Name.O),+ Line: checker.line,+ })+ }+ }++ return in, false+}++// Leave implements the ast.Visitor interface.+func (*tableCommentRequireChecker) Leave(in ast.Node) (ast.Node, bool) {+ return in, true+}++func tableNoComment(options []*ast.TableOption) bool {+ for _, option := range options {+ if option.Tp == ast.TableOptionComment {+ return false+ }+ }++ return true+}diff --git a/plugin/advisor/mysql/advisor_table_comment_require_test.go b/plugin/advisor/mysql/advisor_table_comment_require_test.gonew file mode 100644index 0000000000..b5dda65c0e--- /dev/null+++ b/plugin/advisor/mysql/advisor_table_comment_require_test.go@@ -0,0 +1,43 @@+package mysql++// Framework code is generated by the generator.++import (+ "testing"++ "github.com/bytebase/bytebase/plugin/advisor"+)++func TestTableCommentRequire(t *testing.T) {+ tests := []advisor.TestCase{+ {+ Statement: `CREATE TABLE t(a int) COMMENT 'some comments'`,+ Want: []advisor.Advice{+ {+ Status: advisor.Success,+ Code: advisor.Ok,+ Title: "OK",+ Content: "",+ },+ },+ },+ {+ Statement: `CREATE TABLE t(a int)`,+ Want: []advisor.Advice{+ {+ Status: advisor.Warn,+ Code: advisor.NoTableComment,+ Title: "table.comment-require",+ Content: "Table `t` requires comments",+ Line: 1,+ },+ },+ },+ }++ advisor.RunSQLReviewRuleTests(t, tests, &TableCommentRequireAdvisor{}, &advisor.SQLReviewRule{+ Type: advisor.SchemaRuleTableCommentRequirement,+ Level: advisor.SchemaRuleLevelWarning,+ Payload: "",+ }, advisor.MockMySQLDatabase)+}diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex 2e637a3632..c584de1631 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -63,6 +63,8 @@ const (SchemaRuleTableDropNamingConvention SQLReviewRuleType = "table.drop-naming-convention"// SchemaRuleTableExists require the table existence.SchemaRuleTableExists SQLReviewRuleType = "table.exists"+ // SchemaRuleTableCommentRequirement requaire the table comment.+ SchemaRuleTableCommentRequirement SQLReviewRuleType = "table.comment-require"// SchemaRuleRequiredColumn enforce the required columns in each table.SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"@@ -467,6 +469,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, errcase db.MySQL, db.TiDB:return MySQLTableExists, nil}+ case SchemaRuleTableCommentRequirement:+ switch engine {+ case db.MySQL, db.TiDB:+ return MySQLTableCommentRequirement, nil+ }case SchemaRuleMySQLEngine:if engine == db.MySQL {return MySQLUseInnoDB, nil