Skip to content

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

feat(sql-review): column comment convention for postgres (#14115)

boojack006e0589dd82
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 590be0d273..d27d3e77fe 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -514,6 +514,9 @@ const (
// PostgreSQLColumnRequirement is an advisor type for PostgreSQL column requirement.
PostgreSQLColumnRequirement Type = "bb.plugin.advisor.postgresql.column.require"
+ // PostgreSQLColumnCommentConvention is an advisor type for PostgreSQL column comment convention.
+ PostgreSQLColumnCommentConvention Type = "bb.plugin.advisor.postgresql.column.comment"
+
// PostgreSQLCommentConvention is an advisor type for PostgreSQL comment convention.
PostgreSQLCommentConvention Type = "bb.plugin.advisor.postgresql.comment"
diff --git a/backend/plugin/advisor/pg/advisor_column_comment_convention.go b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
new file mode 100644
index 0000000000..dd17956403
--- /dev/null
+++ b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
@@ -0,0 +1,154 @@
+package pg
+
+import (
+ "fmt"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnCommentConventionAdvisor)(nil)
+ _ ast.Visitor = (*columnCommentConventionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLColumnCommentConvention, &ColumnCommentConventionAdvisor{})
+}
+
+// ColumnCommentConventionAdvisor is the advisor checking for column comment convention.
+type ColumnCommentConventionAdvisor struct {
+}
+
+func (*ColumnCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {
+ stmtList, ok := ctx.AST.([]ast.Node)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Node")
+ }
+
+ 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),
+ payload: payload,
+ classificationConfig: ctx.ClassificationConfig,
+ }
+
+ for _, stmt := range stmtList {
+ ast.Walk(checker, stmt)
+ }
+
+ for _, columnName := range checker.columnNameList {
+ var commentStmt *ast.CommentStmt
+ for _, stmt := range checker.commentStmtList {
+ columnNameDef, ok := stmt.Object.(*ast.ColumnNameDef)
+ if !ok {
+ continue
+ }
+ if columnNameDef.ColumnName == columnName.ColumnName && columnNameDef.Table.Name == columnName.Table.Name {
+ commentStmt = stmt
+ // continue and find the last comment statement.
+ }
+ }
+ if commentStmt == nil || commentStmt.Comment == "" {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.CommentEmpty.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("Comment is required for column `%s`", stringifyColumnNameDef(columnName)),
+ StartPosition: &storepb.Position{
+ Line: int32(columnName.LastLine()),
+ },
+ })
+ } else {
+ comment := commentStmt.Comment
+ if checker.payload.MaxLength > 0 && len(comment) > checker.payload.MaxLength {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.CommentTooLong.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("Column `%s` comment is too long. The length of comment should be within %d characters", stringifyColumnNameDef(columnName), checker.payload.MaxLength),
+ StartPosition: &storepb.Position{
+ Line: int32(commentStmt.LastLine()),
+ },
+ })
+ }
+ if checker.payload.RequiredClassification {
+ if classification, _ := common.GetClassificationAndUserComment(comment, checker.classificationConfig); classification == "" {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.CommentMissingClassification.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("Column `%s` comment requires classification", stringifyColumnNameDef(columnName)),
+ StartPosition: &storepb.Position{
+ Line: int32(commentStmt.LastLine()),
+ },
+ })
+ }
+ }
+ }
+ }
+
+ return checker.adviceList, nil
+}
+
+type columnCommentConventionChecker struct {
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ payload *advisor.CommentConventionRulePayload
+ classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig
+
+ columnNameList []*ast.ColumnNameDef
+ commentStmtList []*ast.CommentStmt
+}
+
+func (checker *columnCommentConventionChecker) Visit(node ast.Node) ast.Visitor {
+ if createTableStmt, ok := node.(*ast.CreateTableStmt); ok {
+ for _, columnDef := range createTableStmt.ColumnList {
+ columnName := &ast.ColumnNameDef{
+ Table: createTableStmt.Name,
+ ColumnName: columnDef.ColumnName,
+ }
+ columnName.SetLastLine(createTableStmt.LastLine())
+ checker.columnNameList = append(checker.columnNameList, columnName)
+ }
+ } else if alterTableStmt, ok := node.(*ast.AlterTableStmt); ok {
+ for _, alterItem := range alterTableStmt.AlterItemList {
+ if addColumnListStmt, ok := alterItem.(*ast.AddColumnListStmt); ok {
+ for _, columnDef := range addColumnListStmt.ColumnList {
+ columnName := &ast.ColumnNameDef{
+ Table: alterTableStmt.Table,
+ ColumnName: columnDef.ColumnName,
+ }
+ columnName.SetLastLine(alterTableStmt.LastLine())
+ checker.columnNameList = append(checker.columnNameList, columnName)
+ }
+ }
+ }
+ } else if commentStmt, ok := node.(*ast.CommentStmt); ok && commentStmt.Type == ast.ObjectTypeColumn {
+ checker.commentStmtList = append(checker.commentStmtList, commentStmt)
+ }
+ return checker
+}
+
+func stringifyColumnNameDef(columnName *ast.ColumnNameDef) string {
+ if columnName == nil {
+ return ""
+ }
+ if columnName.Table == nil {
+ return columnName.ColumnName
+ }
+ return fmt.Sprintf("%s.%s", columnName.Table.Name, columnName.ColumnName)
+}
diff --git a/backend/plugin/advisor/pg/advisor_table_comment_convention.go b/backend/plugin/advisor/pg/advisor_table_comment_convention.go
index ec2eab90ff..655d75f71c 100644
--- a/backend/plugin/advisor/pg/advisor_table_comment_convention.go
+++ b/backend/plugin/advisor/pg/advisor_table_comment_convention.go
@@ -66,7 +66,7 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*s
Status: checker.level,
Code: advisor.CommentEmpty.Int32(),
Title: checker.title,
- Content: fmt.Sprintf("Comment is required for table `%s`", createTableStmt.Name.Name),
+ Content: fmt.Sprintf("Comment is required for table `%s`", stringifyTableDef(createTableStmt.Name)),
StartPosition: &storepb.Position{
Line: int32(createTableStmt.LastLine()),
},
@@ -78,7 +78,7 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*s
Status: checker.level,
Code: advisor.CommentTooLong.Int32(),
Title: checker.title,
- Content: fmt.Sprintf("Table `%s` comment is too long. The length of comment should be within %d characters", createTableStmt.Name.Name, checker.payload.MaxLength),
+ Content: fmt.Sprintf("Table `%s` comment is too long. The length of comment should be within %d characters", stringifyTableDef(createTableStmt.Name), checker.payload.MaxLength),
StartPosition: &storepb.Position{
Line: int32(commentStmt.LastLine()),
},
@@ -90,9 +90,9 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*s
Status: checker.level,
Code: advisor.CommentMissingClassification.Int32(),
Title: checker.title,
- Content: fmt.Sprintf("Table `%s` comment requires classification", createTableStmt.Name.Name),
+ Content: fmt.Sprintf("Table `%s` comment requires classification", stringifyTableDef(createTableStmt.Name)),
StartPosition: &storepb.Position{
- Line: int32(createTableStmt.LastLine()),
+ Line: int32(commentStmt.LastLine()),
},
})
}
@@ -122,3 +122,16 @@ func (checker *tableCommentConventionChecker) Visit(node ast.Node) ast.Visitor {
}
return checker
}
+
+func stringifyTableDef(tableDef *ast.TableDef) string {
+ if tableDef == nil {
+ return ""
+ }
+ if tableDef.Database == "" && tableDef.Schema == "" {
+ return tableDef.Name
+ }
+ if tableDef.Database == "" {
+ return fmt.Sprintf("%s.%s", tableDef.Schema, tableDef.Name)
+ }
+ return fmt.Sprintf("%s.%s.%s", tableDef.Database, tableDef.Schema, tableDef.Name)
+}
diff --git a/backend/plugin/advisor/pg/pg_rules_test.go b/backend/plugin/advisor/pg/pg_rules_test.go
index 5ea9d5105e..4cd9bfcac2 100644
--- a/backend/plugin/advisor/pg/pg_rules_test.go
+++ b/backend/plugin/advisor/pg/pg_rules_test.go
@@ -59,6 +59,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleStatementCheckSetRoleVariable,
advisor.SchemaRuleStatementMaximumLimitValue,
advisor.SchemaRuleTableCommentConvention,
+ advisor.SchemaRuleColumnCommentConvention,
}
for _, rule := range pgRules {
diff --git a/backend/plugin/advisor/pg/test/column_comment.yaml b/backend/plugin/advisor/pg/test/column_comment.yaml
new file mode 100644
index 0000000000..0e051d3ae9
--- /dev/null
+++ b/backend/plugin/advisor/pg/test/column_comment.yaml
@@ -0,0 +1,43 @@
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON COLUMN t.a IS 'comments';
+- statement: |-
+ CREATE TABLE t(a int);
+ want:
+ - status: 2
+ code: 1032
+ title: column.comment
+ content: Comment is required for column `t.a`
+ startposition:
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON COLUMN t.a IS 'loooooooong comments';
+ want:
+ - status: 2
+ code: 1301
+ title: column.comment
+ content: Column `t.a` comment is too long. The length of comment should be within 10 characters
+ startposition:
+ line: 2
+- statement: |-
+ CREATE TABLE t(a int, d text);
+ COMMENT ON COLUMN t.a IS 'comments';
+ want:
+ - status: 2
+ code: 1032
+ title: column.comment
+ content: Comment is required for column `t.d`
+ startposition:
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON COLUMN t.a IS 'loooooooong comments';
+ COMMENT ON COLUMN t.a IS NULL;
+ want:
+ - status: 2
+ code: 1032
+ title: column.comment
+ content: Comment is required for column `t.a`
+ startposition:
+ line: 1
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 175d382535..88114f0d14 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1095,6 +1095,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
switch engine {
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLColumnCommentConvention, nil
+ case storepb.Engine_POSTGRES:
+ return PostgreSQLColumnCommentConvention, nil
}
case SchemaRuleColumnAutoIncrementMustInteger:
switch engine {
diff --git a/backend/tests/sql.go b/backend/tests/sql.go
index 1aa5df8017..5699b8f4f0 100644
--- a/backend/tests/sql.go
+++ b/backend/tests/sql.go
@@ -184,11 +184,6 @@ func prodTemplateReviewConfigForPostgreSQL() (*v1pb.ReviewConfig, error) {
Level: v1pb.SQLReviewRuleLevel_WARNING,
Engine: v1pb.Engine_POSTGRES,
},
- {
- Type: string(advisor.SchemaRuleColumnCommentConvention),
- Level: v1pb.SQLReviewRuleLevel_WARNING,
- Engine: v1pb.Engine_POSTGRES,
- },
{
Type: string(advisor.SchemaRuleColumnAutoIncrementMustInteger),
Level: v1pb.SQLReviewRuleLevel_ERROR,
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 856d38ebcf..1ec977ae69 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -1519,7 +1519,22 @@
payload:
type: NUMBER
default: 64
- engine: MARIADB
+- type: column.comment
+ category: COLUMN
+ componentList:
+ - key: required
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: requiredClassification
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: maxLength
+ payload:
+ type: NUMBER
+ default: 64
+ engine: POSTGRES
- type: column.auto-increment-must-integer
category: COLUMN
engine: MYSQL
diff --git a/frontend/src/types/sql-review.prod.yaml b/frontend/src/types/sql-review.prod.yaml
index 057b9d2492..f888eed007 100644
--- a/frontend/src/types/sql-review.prod.yaml
+++ b/frontend/src/types/sql-review.prod.yaml
@@ -1138,6 +1138,12 @@ ruleList:
required: true
maxLength: 64
engine: MARIADB
+ - type: column.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
+ engine: POSTGRES
- type: column.maximum-character-length
level: WARNING
payload: