Skip to content

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

feat(sql-review): limit PostgreSQL comment length (#2874)

Kikkon288a0c3002e0
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 62b7b6665a..a8ddfadeb3 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -213,6 +213,9 @@ const (
// PostgreSQLColumnRequirement is an advisor type for PostgreSQL column requirement.
PostgreSQLColumnRequirement Type = "bb.plugin.advisor.postgresql.column.require"
+ // PostgreSQLCommentConvention is an advisor type for PostgreSQL comment convention.
+ PostgreSQLCommentConvention Type = "bb.plugin.advisor.postgresql.comment"
+
// PostgreSQLTableRequirePK is an advisor type for PostgreSQL table require primary key.
PostgreSQLTableRequirePK Type = "bb.plugin.advisor.postgresql.table.require-pk"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 050860d394..a970b6a1c0 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -113,6 +113,9 @@ const (
// 1201 ~ 1299 collation error code.
DisabledCollation Code = 1201
+
+ // 1301 ~ 1399 comment error code.
+ CommentTooLong Code = 1301
)
// Int returns the int type of code.
diff --git a/plugin/advisor/pg/advisor_comment_convention.go b/plugin/advisor/pg/advisor_comment_convention.go
new file mode 100644
index 0000000000..085d4a9809
--- /dev/null
+++ b/plugin/advisor/pg/advisor_comment_convention.go
@@ -0,0 +1,95 @@
+package pg
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*CommentConventionAdvisor)(nil)
+ _ ast.Visitor = (*commentConventionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLCommentConvention, &CommentConventionAdvisor{})
+}
+
+// CommentConventionAdvisor is the advisor checking for comment convention.
+type CommentConventionAdvisor struct {
+}
+
+// Check checks for comment convention.
+func (*CommentConventionAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ stmtList, errAdvice := parseStatement(statement)
+ 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 := &commentConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ maxLength: payload.MaxLength,
+ }
+
+ for _, stmt := range stmtList {
+ ast.Walk(checker, stmt)
+ }
+
+ 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 commentConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ maxLength int
+}
+
+// Visit implements the ast.Visitor interface.
+func (checker *commentConventionChecker) Visit(node ast.Node) ast.Visitor {
+ type commentData struct {
+ comment string
+ line int
+ }
+ var comment commentData
+
+ if n, ok := node.(*ast.CommentStmt); ok {
+ comment = commentData{
+ comment: n.Comment,
+ line: n.LastLine(),
+ }
+ }
+
+ if checker.maxLength > 0 && len(comment.comment) > checker.maxLength {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.CommentTooLong,
+ Title: checker.title,
+ Content: fmt.Sprintf("The length of comment should be within %d characters", checker.maxLength),
+ Line: comment.line,
+ })
+ }
+
+ return checker
+}
diff --git a/plugin/advisor/pg/advisor_comment_convention_test.go b/plugin/advisor/pg/advisor_comment_convention_test.go
new file mode 100644
index 0000000000..c163fdfc09
--- /dev/null
+++ b/plugin/advisor/pg/advisor_comment_convention_test.go
@@ -0,0 +1,74 @@
+package pg
+
+// Framework code is generated by the generator.
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestCommentConvention(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `comment on column public.name is 'is a column name'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Line: 0,
+ },
+ },
+ },
+ {
+ Statement: `comment on column public.name is 'is a to long column name'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.CommentTooLong,
+ Title: "comment.length",
+ Content: "The length of comment should be within 20 characters",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `comment on table public is 'is a table name'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Line: 0,
+ },
+ },
+ },
+ {
+ Statement: `comment on table public is 'is a to long table name'`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.CommentTooLong,
+ Title: "comment.length",
+ Content: "The length of 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, &CommentConventionAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleCommentLength,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: string(payload),
+ }, advisor.MockPostgreSQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 2177cd4276..ff6a5a7cde 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -134,6 +134,9 @@ const (
// SchemaRuleInsertDisallowOrderByRand disallow the order by rand in the INSERT statement.
SchemaRuleInsertDisallowOrderByRand SQLReviewRuleType = "insert.disallow-order-by-rand"
+ // SchemaRuleCommentLength limit comment length.
+ SchemaRuleCommentLength SQLReviewRuleType = "comment.length"
+
// TableNameTemplateToken is the token for table name.
TableNameTemplateToken = "{{table}}"
// ColumnListTemplateToken is the token for column name list.