Skip to content

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

chore: migrate table_comment_convention (#9065)

zll60072f382889365
diff --git a/backend/plugin/advisor/mysqlwip/advisor_table_comment_convention.go b/backend/plugin/advisor/mysqlwip/advisor_table_comment_convention.go
index 048f9a6a4b..42cb62e4f3 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_table_comment_convention.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_table_comment_convention.go
@@ -1,30 +1,35 @@
package mysqlwip
-// Framework code is generated by the generator.
-
import (
"fmt"
- "github.com/pingcap/tidb/parser/ast"
+ "github.com/antlr4-go/antlr/v4"
+ mysql "github.com/bytebase/mysql-parser"
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
_ advisor.Advisor = (*TableCommentConventionAdvisor)(nil)
- _ ast.Visitor = (*tableCommentConventionChecker)(nil)
)
+func init() {
+ // only for mysqlwip test.
+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, 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, _ string) ([]advisor.Advice, error) {
- stmtList, ok := ctx.AST.([]ast.StmtNode)
+ list, ok := ctx.AST.([]*mysqlparser.ParseResult)
if !ok {
- return nil, errors.Errorf("failed to convert to StmtNode")
+ return nil, errors.Errorf("failed to convert to mysql parser result")
}
level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
@@ -35,6 +40,7 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]ad
if err != nil {
return nil, err
}
+
checker := &tableCommentConventionChecker{
level: level,
title: string(ctx.Rule.Type),
@@ -42,10 +48,9 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]ad
maxLength: payload.MaxLength,
}
- for _, stmt := range stmtList {
- checker.text = stmt.Text()
- checker.line = stmt.OriginTextPosition()
- (stmt).Accept(checker)
+ for _, stmt := range list {
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
if len(checker.adviceList) == 0 {
@@ -60,53 +65,57 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]ad
}
type tableCommentConventionChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
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,
- })
- }
+// EnterCreateTable is called when production createTable is entered.
+func (checker *tableCommentConventionChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {
+ if ctx.TableName() == nil {
+ return
}
- return in, false
-}
+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+
+ comment, exists := checker.handleCreateTableOptions(ctx.CreateTableOptions())
-// Leave implements the ast.Visitor interface.
-func (*tableCommentConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+ if checker.required && !exists {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.NoTableComment,
+ Title: checker.title,
+ Content: fmt.Sprintf("Table `%s` requires comments", tableName),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
+ 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", tableName, checker.maxLength),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
+ })
+ }
}
-func tableComment(options []*ast.TableOption) (bool, string) {
- for _, option := range options {
- if option.Tp == ast.TableOptionComment {
- return true, option.StrValue
- }
+func (*tableCommentConventionChecker) handleCreateTableOptions(ctx mysql.ICreateTableOptionsContext) (string, bool) {
+ if ctx == nil {
+ return "", false
}
+ for _, option := range ctx.AllCreateTableOption() {
+ if option.COMMENT_SYMBOL() == nil || option.TextStringLiteral() == nil {
+ continue
+ }
- return false, ""
+ comment := mysqlparser.NormalizeMySQLTextStringLiteral(option.TextStringLiteral())
+ return comment, true
+ }
+ return "", false
}
diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
index cfbdae18d3..90099694fd 100644
--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
@@ -52,6 +52,8 @@ func TestMySQLWIPRules(t *testing.T) {
advisor.SchemaRuleTableNoFK,
// advisor.SchemaRuleTableDropNamingConvention require only the table following the naming convention can be deleted.
advisor.SchemaRuleTableDropNamingConvention,
+ // advisor.SchemaRuleTableCommentConvention enforce the table comment convention.
+ advisor.SchemaRuleTableCommentConvention,
// advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.
advisor.SchemaRuleDropEmptyDatabase,
diff --git a/backend/plugin/advisor/mysqlwip/test/table_comment.yaml b/backend/plugin/advisor/mysqlwip/test/table_comment.yaml
index 6a2833b108..edd577b38a 100644
--- a/backend/plugin/advisor/mysqlwip/test/table_comment.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/table_comment.yaml
@@ -1,4 +1,6 @@
-- statement: CREATE TABLE t(a int) COMMENT 'some comments'
+- statement: |-
+ CREATE TABLE a(b INT) COMMENT 'table';
+ CREATE TABLE t(a int) COMMENT 'some comments';
want:
- status: WARN
code: 606
@@ -6,7 +8,9 @@
content: The length of table `t` comment should be within 10 characters
line: 2
details: ""
-- statement: CREATE TABLE t(a int)
+- statement: |-
+ CREATE TABLE a(b INT) COMMENT 'table';
+ CREATE TABLE t(a int);
want:
- status: WARN
code: 605
@@ -14,7 +18,9 @@
content: Table `t` requires comments
line: 2
details: ""
-- statement: CREATE TABLE t(a int) COMMENT 'sdlfkjalkseblkjduafelbnlsdfkljayue'
+- statement: |-
+ CREATE TABLE a(b INT) COMMENT 'table';
+ CREATE TABLE t(a int) COMMENT 'sdlfkjalkseblkjduafelbnlsdfkljayue';
want:
- status: WARN
code: 606
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 344bac67fe..11137e93e6 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1335,6 +1335,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
}
case SchemaRuleTableCommentConvention:
switch engine {
+ // only for mysqlwip test.
+ case storepb.Engine_ENGINE_UNSPECIFIED:
+ return MySQLTableCommentConvention, nil
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLTableCommentConvention, nil
}
diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.go
index 4babaaf65c..17f1c50231 100644
--- a/backend/plugin/parser/mysql/normalize.go
+++ b/backend/plugin/parser/mysql/normalize.go
@@ -87,6 +87,13 @@ func NormalizeMySQLTextOrIdentifier(ctx parser.ITextOrIdentifierContext) string
return textString[1 : len(textString)-1]
}
+// NormalizeMySQLTextStringLiteral normalize the given TextStringLiteral.
+func NormalizeMySQLTextStringLiteral(ctx parser.ITextStringLiteralContext) string {
+ textString := ctx.GetText()
+ // remove the quotations.
+ return textString[1 : len(textString)-1]
+}
+
// NormalizeMySQLSelectAlias normalizes the given select alias.
func NormalizeMySQLSelectAlias(selectAlias parser.ISelectAliasContext) string {
if selectAlias.Identifier() != nil {