Skip to content

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

feat(sql-review): support check the length of MySQL table/column comments (#2479)

rebelicee00fe0611c82
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 22329da989..740109e878 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -100,8 +100,8 @@ 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"
+ // MySQLColumnCommentConvention is an advisor type for MySQL column comment convention.
+ MySQLColumnCommentConvention Type = "bb.plugin.advisor.mysql.column.comment"
// MySQLNoSelectAll is an advisor type for MySQL no select all.
MySQLNoSelectAll Type = "bb.plugin.advisor.mysql.select.no-select-all"
@@ -118,12 +118,12 @@ const (
// MySQLTableExists is an advisor type for MySQL table existence.
MySQLTableExists Type = "bb.plugin.advisor.mysql.table.exists"
+ // MySQLTableCommentConvention is an advisor for MySQL table comment convention.
+ MySQLTableCommentConvention Type = "bb.plugin.advisor.mysql.table.comment"
+
// MySQLTableDisallowCreateTableAs is an advisor type for MySQL disallow CREATE TABLE ... AS ... statement.
MySQLTableDisallowCreateTableAs Type = "bb.plugin.advisor.mysql.table.disallow-create-table-as"
- // 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.go
index 1eb8b9835a..612a1815ef 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -52,6 +52,7 @@ const (
UseChangeColumnStatement Code = 406
ChangeColumnOrder Code = 407
NoColumnComment Code = 408
+ ColumnCommentTooLong Code = 409
// 501 engine error code.
NotInnoDBEngine Code = 501
@@ -62,6 +63,7 @@ const (
TableDropNamingConventionMismatch Code = 603
TableNotExists Code = 604
NoTableComment Code = 605
+ TableCommentTooLong Code = 606
// 701 ~ 799 database advisor error code.
DatabaseNotEmpty Code = 701
diff --git a/plugin/advisor/mysql/advisor_column_comment_convention.go b/plugin/advisor/mysql/advisor_column_comment_convention.go
new file mode 100644
index 0000000000..fa65e5bd3d
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_comment_convention.go
@@ -0,0 +1,176 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/pingcap/tidb/parser/ast"
+ "github.com/pingcap/tidb/parser/format"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnCommentConventionAdvisor)(nil)
+ _ ast.Visitor = (*columnCommentConventionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLColumnCommentConvention, &ColumnCommentConventionAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLColumnCommentConvention, &ColumnCommentConventionAdvisor{})
+}
+
+// ColumnCommentConventionAdvisor is the advisor checking for column comment convention.
+type ColumnCommentConventionAdvisor struct {
+}
+
+// Check checks for column comment convention.
+func (*ColumnCommentConventionAdvisor) 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
+ }
+ payload, err := advisor.UnmarshalCommentConventionRulePayload(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ checker := &columnCommentConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ required: payload.Required,
+ maxLength: payload.MaxLength,
+ }
+
+ 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 columnCommentConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+ required bool
+ maxLength int
+}
+
+type columnCommentData struct {
+ exist bool
+ comment string
+ table string
+ column string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *columnCommentConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
+ var columnList []columnCommentData
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ for _, column := range node.Cols {
+ exist, comment := checker.columnComment(column)
+ columnList = append(columnList, columnCommentData{
+ exist: exist,
+ comment: comment,
+ 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 {
+ exist, comment := checker.columnComment(column)
+ columnList = append(columnList, columnCommentData{
+ exist: exist,
+ comment: comment,
+ table: table,
+ column: column.Name.Name.O,
+ line: checker.line,
+ })
+ }
+ case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
+ exist, comment := checker.columnComment(spec.NewColumns[0])
+ columnList = append(columnList, columnCommentData{
+ exist: exist,
+ comment: comment,
+ table: table,
+ column: spec.NewColumns[0].Name.Name.O,
+ line: checker.line,
+ })
+ }
+ }
+ }
+
+ for _, column := range columnList {
+ if checker.required && !column.exist {
+ 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,
+ })
+ }
+ if checker.maxLength >= 0 && len(column.comment) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.ColumnCommentTooLong,
+ Title: checker.title,
+ Content: fmt.Sprintf("The length of column `%s`.`%s` comment should be within %d characters", column.table, column.column, checker.maxLength),
+ Line: column.line,
+ })
+ }
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*columnCommentConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func (checker *columnCommentConventionChecker) columnComment(column *ast.ColumnDef) (bool, string) {
+ for _, option := range column.Options {
+ if option.Tp == ast.ColumnOptionComment {
+ comment, err := restoreNode(option.Expr, format.RestoreStringWithoutCharset)
+ if err != nil {
+ comment = ""
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.Internal,
+ Title: "Internal error for parsing column comment",
+ Content: fmt.Sprintf("\"%q\" meet internal error %s", checker.text, err),
+ })
+ }
+ return true, comment
+ }
+ }
+
+ return false, ""
+}
diff --git a/plugin/advisor/mysql/advisor_column_comment_require_test.go b/plugin/advisor/mysql/advisor_column_comment_convention_test.go
similarity index 62%
rename from plugin/advisor/mysql/advisor_column_comment_require_test.go
rename to plugin/advisor/mysql/advisor_column_comment_convention_test.go
index ca3bc0f31e..3f71810a3c 100644
--- a/plugin/advisor/mysql/advisor_column_comment_require_test.go
+++ b/plugin/advisor/mysql/advisor_column_comment_convention_test.go
@@ -3,12 +3,15 @@ package mysql
// Framework code is generated by the generator.
import (
+ "encoding/json"
"testing"
+ "github.com/stretchr/testify/require"
+
"github.com/bytebase/bytebase/plugin/advisor"
)
-func TestColumnCommentRequire(t *testing.T) {
+func TestColumnCommentConvention(t *testing.T) {
tests := []advisor.TestCase{
{
Statement: `CREATE TABLE t(a int COMMENT 'some comments')`,
@@ -30,14 +33,14 @@ func TestColumnCommentRequire(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
- Title: "column.comment-require",
+ Title: "column.comment",
Content: "Column `t`.`b` requires comments",
Line: 3,
},
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
- Title: "column.comment-require",
+ Title: "column.comment",
Content: "Column `t`.`c` requires comments",
Line: 4,
},
@@ -49,7 +52,7 @@ func TestColumnCommentRequire(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
- Title: "column.comment-require",
+ Title: "column.comment",
Content: "Column `t`.`b` requires comments",
Line: 1,
},
@@ -61,7 +64,7 @@ func TestColumnCommentRequire(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
- Title: "column.comment-require",
+ Title: "column.comment",
Content: "Column `t`.`b` requires comments",
Line: 1,
},
@@ -73,17 +76,34 @@ func TestColumnCommentRequire(t *testing.T) {
{
Status: advisor.Warn,
Code: advisor.NoColumnComment,
- Title: "column.comment-require",
+ Title: "column.comment",
Content: "Column `t`.`b` requires comments",
Line: 1,
},
},
},
+ {
+ Statement: `ALTER TABLE t MODIFY COLUMN b int COMMENT 'abcdefghiakljhakljdsfalugelkhnabsdguelkadf'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.ColumnCommentTooLong,
+ Title: "column.comment",
+ Content: "The length of column `t`.`b` comment should be within 20 characters",
+ Line: 1,
+ },
+ },
+ },
}
- advisor.RunSQLReviewRuleTests(t, tests, &ColumnCommentRequireAdvisor{}, &advisor.SQLReviewRule{
- Type: advisor.SchemaRuleColumnCommentRequirement,
+ payload, err := json.Marshal(advisor.CommentConventionRulePayload{
+ Required: true,
+ MaxLength: 20,
+ })
+ require.NoError(t, err)
+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnCommentConventionAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnCommentConvention,
Level: advisor.SchemaRuleLevelWarning,
- Payload: "",
+ Payload: string(payload),
}, advisor.MockMySQLDatabase)
}
diff --git a/plugin/advisor/mysql/advisor_column_comment_require.go b/plugin/advisor/mysql/advisor_column_comment_require.go
deleted file mode 100644
index 7a06495e11..0000000000
--- a/plugin/advisor/mysql/advisor_column_comment_require.go
+++ /dev/null
@@ -1,135 +0,0 @@
-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_exists.go b/plugin/advisor/mysql/advisor_column_exists.go
index 203e0debc5..c0ac865fcd 100644
--- a/plugin/advisor/mysql/advisor_column_exists.go
+++ b/plugin/advisor/mysql/advisor_column_exists.go
@@ -72,7 +72,6 @@ type columnExistsChecker struct {
type columnData struct {
table string
column string
- line int
}
// Enter implements the ast.Visitor interface.
diff --git a/plugin/advisor/mysql/advisor_table_comment_convention.go b/plugin/advisor/mysql/advisor_table_comment_convention.go
new file mode 100644
index 0000000000..b8f2653b11
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_comment_convention.go
@@ -0,0 +1,117 @@
+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 = (*TableCommentConventionAdvisor)(nil)
+ _ ast.Visitor = (*tableCommentConventionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLTableCommentConvention, &TableCommentConventionAdvisor{})
+ advisor.Register(db.TiDB, advisor.MySQLTableCommentConvention, &TableCommentConventionAdvisor{})
+}
+
+// TableCommentConventionAdvisor is the advisor checking for table comment convention.
+type TableCommentConventionAdvisor struct {
+}
+
+// Check checks for table comment convention.
+func (*TableCommentConventionAdvisor) 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
+ }
+ payload, err := advisor.UnmarshalCommentConventionRulePayload(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+ checker := &tableCommentConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ required: payload.Required,
+ maxLength: payload.MaxLength,
+ }
+
+ 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 tableCommentConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+ required bool
+ maxLength int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *tableCommentConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
+ if node, ok := in.(*ast.CreateTableStmt); ok {
+ exist, comment := tableComment(node.Options)
+ if checker.required && !exist {
+ 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,
+ })
+ }
+ if checker.maxLength >= 0 && len(comment) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.TableCommentTooLong,
+ Title: checker.title,
+ Content: fmt.Sprintf("The length of table `%s` comment should be within %d characters", node.Table.Name.O, checker.maxLength),
+ Line: checker.line,
+ })
+ }
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*tableCommentConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func tableComment(options []*ast.TableOption) (bool, string) {
+ for _, option := range options {
+ if option.Tp == ast.TableOptionComment {
+ return true, option.StrValue
+ }
+ }
+
+ return false, ""
+}
diff --git a/plugin/advisor/mysql/advisor_table_comment_convention_test.go b/plugin/advisor/mysql/advisor_table_comment_convention_test.go
new file mode 100644
index 0000000000..6943becc7d
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_comment_convention_test.go
@@ -0,0 +1,63 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestTableCommentConvention(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",
+ Content: "Table `t` requires comments",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE t(a int) COMMENT 'sdlfkjalkseblkjduafelbnlsdfkljayue'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.TableCommentTooLong,
+ Title: "table.comment",
+ Content: "The length of table `t` comment should be within 20 characters",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ payload, err := json.Marshal(advisor.CommentConventionRulePayload{
+ Required: true,
+ MaxLength: 20,
+ })
+ require.NoError(t, err)
+ advisor.RunSQLReviewRuleTests(t, tests, &TableCommentConventionAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleTableCommentConvention,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: string(payload),
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/mysql/advisor_table_comment_require.go b/plugin/advisor/mysql/advisor_table_comment_require.go
deleted file mode 100644
index 275422290e..0000000000
--- a/plugin/advisor/mysql/advisor_table_comment_require.go
+++ /dev/null
@@ -1,99 +0,0 @@
-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.go
deleted file mode 100644
index b5dda65c0e..0000000000
--- a/plugin/advisor/mysql/advisor_table_comment_require_test.go
+++ /dev/null
@@ -1,43 +0,0 @@
-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.go
index 4b8e55d753..a20123edfc 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -65,8 +65,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"
+ // SchemaRuleTableCommentConvention enforce the table comment convention.
+ SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"
// SchemaRuleRequiredColumn enforce the required columns in each table.
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
@@ -82,8 +82,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"
+ // SchemaRuleColumnCommentConvention enforce the column comment convention.
+ SchemaRuleColumnCommentConvention SQLReviewRuleType = "column.comment"
// SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.
SchemaRuleSchemaBackwardCompatibility SQLReviewRuleType = "schema.backward-compatibility"
@@ -182,6 +182,10 @@ func (rule *SQLReviewRule) Validate() error {
if _, err := UnmarshalRequiredColumnRulePayload(rule.Payload); err != nil {
return err
}
+ case SchemaRuleColumnCommentConvention, SchemaRuleTableCommentConvention:
+ if _, err := UnmarshalCommentConventionRulePayload(rule.Payload); err != nil {
+ return err
+ }
}
return nil
}
@@ -197,6 +201,12 @@ type RequiredColumnRulePayload struct {
ColumnList []string `json:"columnList"`
}
+// CommentConventionRulePayload is the payload for comment convention rule.
+type CommentConventionRulePayload struct {
+ Required bool `json:"required"`
+ MaxLength int `json:"maxLength"`
+}
+
// UnamrshalNamingRulePayloadAsRegexp will unmarshal payload to NamingRulePayload and compile it as regular expression.
func UnamrshalNamingRulePayloadAsRegexp(payload string) (*regexp.Regexp, int, error) {
var nr NamingRulePayload
@@ -278,6 +288,15 @@ func UnmarshalRequiredColumnRulePayload(payload string) (*RequiredColumnRulePayl
return &rcr, nil
}
+// UnmarshalCommentConventionRulePayload will unmarshal payload to CommentConventionRulePayload.
+func UnmarshalCommentConventionRulePayload(payload string) (*CommentConventionRulePayload, error) {
+ var ccr CommentConventionRulePayload
+ if err := json.Unmarshal([]byte(payload), &ccr); err != nil {
+ return nil, errors.Wrapf(err, "failed to unmarshal comment convention rule payload %q", payload)
+ }
+ return &ccr, nil
+}
+
// SQLReviewCheckContext is the context for SQL review check.
type SQLReviewCheckContext struct {
Charset string
@@ -445,10 +464,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLColumnDisallowChangingOrder, nil
}
- case SchemaRuleColumnCommentRequirement:
+ case SchemaRuleColumnCommentConvention:
switch engine {
case db.MySQL, db.TiDB:
- return MySQLColumnCommentRequirement, nil
+ return MySQLColumnCommentConvention, nil
}
case SchemaRuleTableRequirePK:
switch engine {
@@ -474,10 +493,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLTableExists, nil
}
- case SchemaRuleTableCommentRequirement:
+ case SchemaRuleTableCommentConvention:
switch engine {
case db.MySQL, db.TiDB:
- return MySQLTableCommentRequirement, nil
+ return MySQLTableCommentConvention, nil
}
case SchemaRuleMySQLEngine:
if engine == db.MySQL {