Skip to content

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

feat(sql-review): table comment convention for oracle (#14121)

boojack4555daa22c19
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index d27d3e77fe..f24541e941 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -719,6 +719,9 @@ const (
OracleStatementDisallowMixInDDL Type = "bb.plugin.advisor.oracle.statement.disallow-mix-in-ddl"
OracleStatementDisallowMixInDML Type = "bb.plugin.advisor.oracle.statement.disallow-mix-in-dml"
+ // OracleTableCommentConvention is an advisor type for Oracle table comment convention.
+ OracleTableCommentConvention Type = "bb.plugin.advisor.oracle.table.comment"
+
// Snowflake Advisor.
// SnowflakeSyntax is an advisor type for Snowflake syntax.
diff --git a/backend/plugin/advisor/oracle/advisor_table_comment_convention.go b/backend/plugin/advisor/oracle/advisor_table_comment_convention.go
new file mode 100644
index 0000000000..94c1a1c225
--- /dev/null
+++ b/backend/plugin/advisor/oracle/advisor_table_comment_convention.go
@@ -0,0 +1,142 @@
+// Package oracle is the advisor for oracle database.
+package oracle
+
+import (
+ "fmt"
+
+ "github.com/antlr4-go/antlr/v4"
+ parser "github.com/bytebase/plsql-parser"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ plsqlparser "github.com/bytebase/bytebase/backend/plugin/parser/plsql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*TableCommentConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_ORACLE, advisor.OracleTableCommentConvention, &TableCommentConventionAdvisor{})
+ advisor.Register(storepb.Engine_DM, advisor.OracleTableCommentConvention, &TableCommentConventionAdvisor{})
+ advisor.Register(storepb.Engine_OCEANBASE_ORACLE, advisor.OracleTableCommentConvention, &TableCommentConventionAdvisor{})
+}
+
+// TableCommentConventionAdvisor is the advisor checking for table comment convention.
+type TableCommentConventionAdvisor struct {
+}
+
+func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*storepb.Advice, error) {
+ tree, ok := ctx.AST.(antlr.Tree)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Tree")
+ }
+
+ 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
+ }
+
+ listener := &tableCommentConventionListener{
+ level: level,
+ title: string(ctx.Rule.Type),
+ currentDatabase: ctx.CurrentDatabase,
+ payload: payload,
+ classificationConfig: ctx.ClassificationConfig,
+ tableNames: []string{},
+ tableComment: make(map[string]string),
+ tableLine: make(map[string]int),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvices()
+}
+
+// tableCommentConventionListener is the listener for table comment convention.
+type tableCommentConventionListener struct {
+ *parser.BasePlSqlParserListener
+
+ level storepb.Advice_Status
+ title string
+ currentDatabase string
+ payload *advisor.CommentConventionRulePayload
+ classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig
+
+ tableNames []string
+ tableComment map[string]string
+ tableLine map[string]int
+}
+
+func (l *tableCommentConventionListener) EnterCreate_table(ctx *parser.Create_tableContext) {
+ schemaName := l.currentDatabase
+ if ctx.Schema_name() != nil {
+ schemaName = normalizeIdentifier(ctx.Schema_name(), l.currentDatabase)
+ }
+
+ tableName := fmt.Sprintf("%s.%s", schemaName, normalizeIdentifier(ctx.Table_name(), l.currentDatabase))
+ l.tableNames = append(l.tableNames, tableName)
+ l.tableLine[tableName] = ctx.GetStart().GetLine()
+}
+
+func (l *tableCommentConventionListener) EnterComment_on_table(ctx *parser.Comment_on_tableContext) {
+ if ctx.Tableview_name() == nil {
+ return
+ }
+
+ tableName := normalizeIdentifier(ctx.Tableview_name(), l.currentDatabase)
+ l.tableComment[tableName] = plsqlparser.NormalizeQuotedString(ctx.Quoted_string())
+}
+
+func (l *tableCommentConventionListener) generateAdvices() ([]*storepb.Advice, error) {
+ advices := []*storepb.Advice{}
+ for _, tableName := range l.tableNames {
+ comment, ok := l.tableComment[tableName]
+ if !ok || comment == "" {
+ if l.payload.Required {
+ advices = append(advices, &storepb.Advice{
+ Status: l.level,
+ Code: advisor.CommentEmpty.Int32(),
+ Title: l.title,
+ Content: fmt.Sprintf("Comment is required for table %s", normalizeTableName(tableName)),
+ StartPosition: &storepb.Position{
+ Line: int32(l.tableLine[tableName]),
+ },
+ })
+ }
+ } else {
+ if l.payload.MaxLength > 0 && len(comment) > l.payload.MaxLength {
+ advices = append(advices, &storepb.Advice{
+ Status: l.level,
+ Code: advisor.CommentTooLong.Int32(),
+ Title: l.title,
+ Content: fmt.Sprintf("Table %s comment is too long. The length of comment should be within %d characters", normalizeTableName(tableName), l.payload.MaxLength),
+ StartPosition: &storepb.Position{
+ Line: int32(l.tableLine[tableName]),
+ },
+ })
+ }
+ if l.payload.RequiredClassification {
+ if classification, _ := common.GetClassificationAndUserComment(comment, l.classificationConfig); classification == "" {
+ advices = append(advices, &storepb.Advice{
+ Status: l.level,
+ Code: advisor.CommentMissingClassification.Int32(),
+ Title: l.title,
+ Content: fmt.Sprintf("Table %s comment requires classification", normalizeTableName(tableName)),
+ StartPosition: &storepb.Position{
+ Line: int32(l.tableLine[tableName]),
+ },
+ })
+ }
+ }
+ }
+ }
+
+ return advices, nil
+}
diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.go
index 20ee42323c..bfd6d781ab 100644
--- a/backend/plugin/advisor/oracle/oracle_rules_test.go
+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go
@@ -30,6 +30,7 @@ func TestOracleRules(t *testing.T) {
advisor.SchemaRuleIdentifierNoKeyword,
advisor.SchemaRuleIdentifierCase,
advisor.SchemaRuleStatementDisallowMixDDLDML,
+ advisor.SchemaRuleTableCommentConvention,
advisor.SchemaRuleStatementDisallowMixInDDL,
advisor.SchemaRuleStatementDisallowMixInDML,
}
diff --git a/backend/plugin/advisor/oracle/test/table_comment.yaml b/backend/plugin/advisor/oracle/test/table_comment.yaml
new file mode 100644
index 0000000000..ba2801a790
--- /dev/null
+++ b/backend/plugin/advisor/oracle/test/table_comment.yaml
@@ -0,0 +1,47 @@
+- statement: |-
+ CREATE TABLE book (
+ id int,
+ name varchar(255)
+ );
+ COMMENT ON TABLE book IS 'comments';
+- statement: |-
+ CREATE TABLE t(a int);
+ want:
+ - status: 2
+ code: 1032
+ title: table.comment
+ content: Comment is required for table "TEST_DB"."T"
+ startposition:
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ CREATE TABLE d(a int);
+ COMMENT ON TABLE t IS 'comments';
+ want:
+ - status: 2
+ code: 1032
+ title: table.comment
+ content: Comment is required for table "TEST_DB"."D"
+ startposition:
+ line: 2
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON TABLE t IS 'comments';
+ COMMENT ON TABLE t IS '';
+ want:
+ - status: 2
+ code: 1032
+ title: table.comment
+ content: Comment is required for table "TEST_DB"."T"
+ startposition:
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON TABLE t IS 'looooooong comments';
+ want:
+ - status: 2
+ code: 1301
+ title: table.comment
+ content: Table "TEST_DB"."T" comment is too long. The length of comment should be within 10 characters
+ startposition:
+ line: 1
diff --git a/backend/plugin/advisor/pg/advisor_column_comment_convention.go b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
index dd17956403..e68aebdada 100644
--- a/backend/plugin/advisor/pg/advisor_column_comment_convention.go
+++ b/backend/plugin/advisor/pg/advisor_column_comment_convention.go
@@ -62,15 +62,17 @@ func (*ColumnCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*
}
}
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()),
- },
- })
+ if checker.payload.Required {
+ 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 {
diff --git a/backend/plugin/advisor/pg/advisor_table_comment_convention.go b/backend/plugin/advisor/pg/advisor_table_comment_convention.go
index 655d75f71c..02c467724e 100644
--- a/backend/plugin/advisor/pg/advisor_table_comment_convention.go
+++ b/backend/plugin/advisor/pg/advisor_table_comment_convention.go
@@ -62,15 +62,17 @@ func (*TableCommentConventionAdvisor) Check(ctx advisor.Context, _ string) ([]*s
}
}
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 table `%s`", stringifyTableDef(createTableStmt.Name)),
- StartPosition: &storepb.Position{
- Line: int32(createTableStmt.LastLine()),
- },
- })
+ if checker.payload.Required {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.CommentEmpty.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("Comment is required for table `%s`", stringifyTableDef(createTableStmt.Name)),
+ StartPosition: &storepb.Position{
+ Line: int32(createTableStmt.LastLine()),
+ },
+ })
+ }
} else {
comment := commentStmt.Comment
if checker.payload.MaxLength > 0 && len(comment) > checker.payload.MaxLength {
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 433cc29578..f76e1d17f1 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1219,6 +1219,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
return MySQLTableCommentConvention, nil
case storepb.Engine_POSTGRES:
return PostgreSQLTableCommentConvention, nil
+ case storepb.Engine_ORACLE, storepb.Engine_DM, storepb.Engine_OCEANBASE_ORACLE:
+ return OracleTableCommentConvention, nil
}
case SchemaRuleTableDisallowPartition:
switch engine {
diff --git a/backend/plugin/parser/plsql/util.go b/backend/plugin/parser/plsql/util.go
index 4769b8bddb..84a6eba445 100644
--- a/backend/plugin/parser/plsql/util.go
+++ b/backend/plugin/parser/plsql/util.go
@@ -55,6 +55,16 @@ func NormalizeTableAlias(ctx plsql.ITable_aliasContext) string {
return ""
}
+// NormalizeQuotedString returns the string without the quotes.
+func NormalizeQuotedString(ctx plsql.IQuoted_stringContext) string {
+ if ctx == nil {
+ return ""
+ }
+
+ raw := ctx.GetText()
+ return raw[1 : len(raw)-1]
+}
+
func normalizeClusterName(ctx plsql.ICluster_nameContext) (string, string) {
var list []string
for _, idExpression := range ctx.AllId_expression() {
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 1ec977ae69..9f4ec7ef13 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -193,6 +193,22 @@
type: NUMBER
default: 64
engine: POSTGRES
+- type: table.comment
+ category: TABLE
+ componentList:
+ - key: required
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: requiredClassification
+ payload:
+ type: BOOLEAN
+ default: false
+ - key: maxLength
+ payload:
+ type: NUMBER
+ default: 64
+ engine: ORACLE
- type: table.disallow-partition
category: TABLE
engine: MYSQL
diff --git a/frontend/src/types/sql-review.prod.yaml b/frontend/src/types/sql-review.prod.yaml
index f888eed007..58d466f788 100644
--- a/frontend/src/types/sql-review.prod.yaml
+++ b/frontend/src/types/sql-review.prod.yaml
@@ -143,6 +143,12 @@ ruleList:
required: true
maxLength: 64
engine: POSTGRES
+ - type: table.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
+ engine: ORACLE
- type: table.no-duplicate-index
level: WARNING
engine: MYSQL