Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(sql-review): implement MySQL column comment requirement rule (#2466)

rebelice04566d0a1b8e
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index f492e1e40a..a9909f0549 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -100,6 +100,9 @@ const (
// MySQLColumnDisallowChangingOrder is an advisor type for MySQL disallow changing column order.
MySQLColumnDisallowChangingOrder Type = "bb.plugin.advisor.mysql.column.disallow-changing-order"
+ // MySQLColumnCommentRequirement is an advisor type for MySQL column comment requirement.
+ MySQLColumnCommentRequirement Type = "bb.plugin.advisor.mysql.column.comment-require"
+
// MySQLNoSelectAll is an advisor type for MySQL no select all.
MySQLNoSelectAll Type = "bb.plugin.advisor.mysql.select.no-select-all"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 443cf6037b..d209de231f 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -50,6 +50,7 @@ const (
ColumnNotExists Code = 405
UseChangeColumnStatement Code = 406
ChangeColumnOrder Code = 407
+ NoColumnComment Code = 408
// 501 engine error code.
NotInnoDBEngine Code = 501
diff --git a/plugin/advisor/mysql/advisor_column_comment_require.go b/plugin/advisor/mysql/advisor_column_comment_require.go
new file mode 100644
index 0000000000..7a06495e11
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_comment_require.go
@@ -0,0 +1,135 @@
+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 = (*ColumnCommentRequireAdvisor)(nil)
+ _ ast.Visitor = (*columnCommentRequireChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLColumnCommentRequirement, &ColumnCommentRequireAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLColumnCommentRequirement, &ColumnCommentRequireAdvisor{})
+}
+
+// ColumnCommentRequireAdvisor is the advisor checking for column comment requirement.
+type ColumnCommentRequireAdvisor struct {
+}
+
+// Check checks for column comment requirement.
+func (*ColumnCommentRequireAdvisor) 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 := &columnCommentRequireChecker{
+ 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 columnCommentRequireChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *columnCommentRequireChecker) Enter(in ast.Node) (ast.Node, bool) {
+ var columnList []columnData
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ for _, column := range node.Cols {
+ if columnNoComment(column) {
+ columnList = append(columnList, columnData{
+ table: node.Table.Name.O,
+ column: column.Name.Name.O,
+ line: column.OriginTextPosition(),
+ })
+ }
+ }
+ case *ast.AlterTableStmt:
+ table := node.Table.Name.O
+ for _, spec := range node.Specs {
+ switch spec.Tp {
+ case ast.AlterTableAddColumns:
+ for _, column := range spec.NewColumns {
+ if columnNoComment(column) {
+ columnList = append(columnList, columnData{
+ table: table,
+ column: column.Name.Name.O,
+ line: checker.line,
+ })
+ }
+ }
+ case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
+ if columnNoComment(spec.NewColumns[0]) {
+ columnList = append(columnList, columnData{
+ table: table,
+ column: spec.NewColumns[0].Name.Name.O,
+ line: checker.line,
+ })
+ }
+ }
+ }
+ }
+
+ for _, column := range columnList {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NoColumnComment,
+ Title: checker.title,
+ Content: fmt.Sprintf("Column `%s`.`%s` requires comments", column.table, column.column),
+ Line: column.line,
+ })
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*columnCommentRequireChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func columnNoComment(column *ast.ColumnDef) bool {
+ for _, option := range column.Options {
+ if option.Tp == ast.ColumnOptionComment {
+ return false
+ }
+ }
+
+ return true
+}
diff --git a/plugin/advisor/mysql/advisor_column_comment_require_test.go b/plugin/advisor/mysql/advisor_column_comment_require_test.go
new file mode 100644
index 0000000000..ca3bc0f31e
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_comment_require_test.go
@@ -0,0 +1,89 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestColumnCommentRequire(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 COMMENT 'some comments',
+ b int,
+ c int)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.NoColumnComment,
+ Title: "column.comment-require",
+ Content: "Column `t`.`b` requires comments",
+ Line: 3,
+ },
+ {
+ Status: advisor.Warn,
+ Code: advisor.NoColumnComment,
+ Title: "column.comment-require",
+ Content: "Column `t`.`c` requires comments",
+ Line: 4,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t ADD COLUMN b int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.NoColumnComment,
+ Title: "column.comment-require",
+ Content: "Column `t`.`b` requires comments",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t CHANGE COLUMN a b int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.NoColumnComment,
+ Title: "column.comment-require",
+ Content: "Column `t`.`b` requires comments",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t MODIFY COLUMN b int`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.NoColumnComment,
+ Title: "column.comment-require",
+ Content: "Column `t`.`b` requires comments",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnCommentRequireAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnCommentRequirement,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/mysql/advisor_column_exists.go b/plugin/advisor/mysql/advisor_column_exists.go
index c0ac865fcd..203e0debc5 100644
--- a/plugin/advisor/mysql/advisor_column_exists.go
+++ b/plugin/advisor/mysql/advisor_column_exists.go
@@ -72,6 +72,7 @@ type columnExistsChecker struct {
type columnData struct {
table string
column string
+ line int
}
// Enter implements the ast.Visitor interface.
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 8d2450475f..2e637a3632 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -78,6 +78,8 @@ const (
SchemaRuleColumnExists SQLReviewRuleType = "column.exists"
// SchemaRuleColumnDisallowChangingOrder disallow changing column order.
SchemaRuleColumnDisallowChangingOrder SQLReviewRuleType = "column.disallow-changing-order"
+ // SchemaRuleColumnCommentRequirement require the column comments.
+ SchemaRuleColumnCommentRequirement SQLReviewRuleType = "column.comment-require"
// SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.
SchemaRuleSchemaBackwardCompatibility SQLReviewRuleType = "schema.backward-compatibility"
@@ -436,6 +438,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLColumnDisallowChangingOrder, nil
}
+ case SchemaRuleColumnCommentRequirement:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLColumnCommentRequirement, nil
+ }
case SchemaRuleTableRequirePK:
switch engine {
case db.MySQL, db.TiDB: