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 oracle (#14130)

boojack6e3a45c70fa3
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index f24541e941..d3c6f684e6 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -721,6 +721,8 @@ const (
// OracleTableCommentConvention is an advisor type for Oracle table comment convention.
OracleTableCommentConvention Type = "bb.plugin.advisor.oracle.table.comment"
+ // OracleColumnCommentConvention is an advisor type for Oracle column comment convention.
+ OracleColumnCommentConvention Type = "bb.plugin.advisor.oracle.column.comment"
// Snowflake Advisor.
diff --git a/backend/plugin/advisor/oracle/advisor_column_comment_convention.go b/backend/plugin/advisor/oracle/advisor_column_comment_convention.go
new file mode 100644
index 0000000000..cedee853ea
--- /dev/null
+++ b/backend/plugin/advisor/oracle/advisor_column_comment_convention.go
@@ -0,0 +1,160 @@
+// 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 = (*ColumnCommentConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_ORACLE, advisor.OracleColumnCommentConvention, &ColumnCommentConventionAdvisor{})
+ advisor.Register(storepb.Engine_OCEANBASE_ORACLE, advisor.OracleColumnCommentConvention, &ColumnCommentConventionAdvisor{})
+}
+
+// ColumnCommentConventionAdvisor is the advisor checking for column comment convention.
+type ColumnCommentConventionAdvisor struct {
+}
+
+func (*ColumnCommentConventionAdvisor) 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 := &columnCommentConventionListener{
+ level: level,
+ title: string(ctx.Rule.Type),
+ currentDatabase: ctx.CurrentDatabase,
+ payload: payload,
+ classificationConfig: ctx.ClassificationConfig,
+ columnNames: []string{},
+ columnComment: make(map[string]string),
+ columnLine: make(map[string]int),
+ }
+
+ antlr.ParseTreeWalkerDefault.Walk(listener, tree)
+
+ return listener.generateAdvices()
+}
+
+// columnCommentConventionListener is the listener for column comment convention.
+type columnCommentConventionListener struct {
+ *parser.BasePlSqlParserListener
+
+ level storepb.Advice_Status
+ title string
+ currentDatabase string
+ payload *advisor.CommentConventionRulePayload
+ classificationConfig *storepb.DataClassificationSetting_DataClassificationConfig
+
+ tableName string
+ columnNames []string
+ columnComment map[string]string
+ columnLine map[string]int
+}
+
+func (l *columnCommentConventionListener) EnterCreate_table(ctx *parser.Create_tableContext) {
+ schemaName := l.currentDatabase
+ if ctx.Schema_name() != nil {
+ schemaName = normalizeIdentifier(ctx.Schema_name(), l.currentDatabase)
+ }
+ l.tableName = fmt.Sprintf("%s.%s", schemaName, normalizeIdentifier(ctx.Table_name(), schemaName))
+}
+
+func (l *columnCommentConventionListener) ExitCreate_table(_ *parser.Create_tableContext) {
+ l.tableName = ""
+}
+
+func (l *columnCommentConventionListener) EnterColumn_definition(ctx *parser.Column_definitionContext) {
+ if l.tableName == "" {
+ return
+ }
+ columnName := fmt.Sprintf(`%s.%s`, l.tableName, normalizeIdentifier(ctx.Column_name(), l.currentDatabase))
+ l.columnNames = append(l.columnNames, columnName)
+ l.columnLine[columnName] = ctx.GetStart().GetLine()
+}
+
+func (l *columnCommentConventionListener) EnterAlter_table(ctx *parser.Alter_tableContext) {
+ l.tableName = normalizeIdentifier(ctx.Tableview_name(), l.currentDatabase)
+}
+
+func (l *columnCommentConventionListener) ExitAdd_column_clause(_ *parser.Add_column_clauseContext) {
+ l.tableName = ""
+}
+
+func (l *columnCommentConventionListener) EnterComment_on_column(ctx *parser.Comment_on_columnContext) {
+ if ctx.Column_name() == nil {
+ return
+ }
+
+ columnName := fmt.Sprintf(`%s.%s`, l.currentDatabase, normalizeIdentifier(ctx.Column_name(), ""))
+ l.columnComment[columnName] = plsqlparser.NormalizeQuotedString(ctx.Quoted_string())
+}
+
+func (l *columnCommentConventionListener) generateAdvices() ([]*storepb.Advice, error) {
+ advices := []*storepb.Advice{}
+ for _, columnName := range l.columnNames {
+ comment, ok := l.columnComment[columnName]
+ 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 column %s", normalizeIdentifierName(columnName)),
+ StartPosition: &storepb.Position{
+ Line: int32(l.columnLine[columnName]),
+ },
+ })
+ }
+ } 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("Column %s comment is too long. The length of comment should be within %d characters", normalizeIdentifierName(columnName), l.payload.MaxLength),
+ StartPosition: &storepb.Position{
+ Line: int32(l.columnLine[columnName]),
+ },
+ })
+ }
+ 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("Column %s comment requires classification", normalizeIdentifierName(columnName)),
+ StartPosition: &storepb.Position{
+ Line: int32(l.columnLine[columnName]),
+ },
+ })
+ }
+ }
+ }
+ }
+
+ return advices, nil
+}
diff --git a/backend/plugin/advisor/oracle/advisor_table_comment_convention.go b/backend/plugin/advisor/oracle/advisor_table_comment_convention.go
index 94c1a1c225..3281d62e65 100644
--- a/backend/plugin/advisor/oracle/advisor_table_comment_convention.go
+++ b/backend/plugin/advisor/oracle/advisor_table_comment_convention.go
@@ -20,7 +20,6 @@ var (
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{})
}
@@ -104,7 +103,7 @@ func (l *tableCommentConventionListener) generateAdvices() ([]*storepb.Advice, e
Status: l.level,
Code: advisor.CommentEmpty.Int32(),
Title: l.title,
- Content: fmt.Sprintf("Comment is required for table %s", normalizeTableName(tableName)),
+ Content: fmt.Sprintf("Comment is required for table %s", normalizeIdentifierName(tableName)),
StartPosition: &storepb.Position{
Line: int32(l.tableLine[tableName]),
},
@@ -116,7 +115,7 @@ func (l *tableCommentConventionListener) generateAdvices() ([]*storepb.Advice, e
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),
+ Content: fmt.Sprintf("Table %s comment is too long. The length of comment should be within %d characters", normalizeIdentifierName(tableName), l.payload.MaxLength),
StartPosition: &storepb.Position{
Line: int32(l.tableLine[tableName]),
},
@@ -128,7 +127,7 @@ func (l *tableCommentConventionListener) generateAdvices() ([]*storepb.Advice, e
Status: l.level,
Code: advisor.CommentMissingClassification.Int32(),
Title: l.title,
- Content: fmt.Sprintf("Table %s comment requires classification", normalizeTableName(tableName)),
+ Content: fmt.Sprintf("Table %s comment requires classification", normalizeIdentifierName(tableName)),
StartPosition: &storepb.Position{
Line: int32(l.tableLine[tableName]),
},
diff --git a/backend/plugin/advisor/oracle/advisor_table_no_foreign_key.go b/backend/plugin/advisor/oracle/advisor_table_no_foreign_key.go
index 12451f4b57..7f24ea4a29 100644
--- a/backend/plugin/advisor/oracle/advisor_table_no_foreign_key.go
+++ b/backend/plugin/advisor/oracle/advisor_table_no_foreign_key.go
@@ -71,7 +71,7 @@ func (l *tableNoForeignKeyListener) generateAdvice() ([]*storepb.Advice, error)
Status: l.level,
Code: advisor.TableHasFK.Int32(),
Title: l.title,
- Content: fmt.Sprintf("Foreign key is not allowed in the table %s.", normalizeTableName(tableName)),
+ Content: fmt.Sprintf("Foreign key is not allowed in the table %s.", normalizeIdentifierName(tableName)),
StartPosition: &storepb.Position{
Line: int32(l.tableLine[tableName]),
},
diff --git a/backend/plugin/advisor/oracle/advisor_table_require_pk.go b/backend/plugin/advisor/oracle/advisor_table_require_pk.go
index e0f5c3fd7f..8e99a8c835 100644
--- a/backend/plugin/advisor/oracle/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/oracle/advisor_table_require_pk.go
@@ -71,7 +71,7 @@ func (l *TableRequirePKListener) generateAdvice() ([]*storepb.Advice, error) {
Status: l.level,
Code: advisor.TableNoPK.Int32(),
Title: l.title,
- Content: fmt.Sprintf("Table %s requires PRIMARY KEY.", normalizeTableName(tableName)),
+ Content: fmt.Sprintf("Table %s requires PRIMARY KEY.", normalizeIdentifierName(tableName)),
StartPosition: &storepb.Position{
Line: int32(l.tableLine[tableName]),
},
diff --git a/backend/plugin/advisor/oracle/oracle_rules_test.go b/backend/plugin/advisor/oracle/oracle_rules_test.go
index bfd6d781ab..9d23a0fbc9 100644
--- a/backend/plugin/advisor/oracle/oracle_rules_test.go
+++ b/backend/plugin/advisor/oracle/oracle_rules_test.go
@@ -30,9 +30,10 @@ func TestOracleRules(t *testing.T) {
advisor.SchemaRuleIdentifierNoKeyword,
advisor.SchemaRuleIdentifierCase,
advisor.SchemaRuleStatementDisallowMixDDLDML,
- advisor.SchemaRuleTableCommentConvention,
advisor.SchemaRuleStatementDisallowMixInDDL,
advisor.SchemaRuleStatementDisallowMixInDML,
+ advisor.SchemaRuleTableCommentConvention,
+ advisor.SchemaRuleColumnCommentConvention,
}
for _, rule := range oracleRules {
diff --git a/backend/plugin/advisor/oracle/parser.go b/backend/plugin/advisor/oracle/parser.go
index 4230caa24d..6c26d8d244 100644
--- a/backend/plugin/advisor/oracle/parser.go
+++ b/backend/plugin/advisor/oracle/parser.go
@@ -66,7 +66,9 @@ func normalizeIDExpression(idExpression plsql.IId_expressionContext) string {
return ""
}
-func normalizeTableName(name string) string {
+// normalizeIdentifierName normalizes the identifier name to the format of "schema"."table"."column".
+// Including table name and column name.
+func normalizeIdentifierName(name string) string {
list := strings.Split(name, ".")
var result []string
for _, item := range list {
diff --git a/backend/plugin/advisor/oracle/test/column_comment.yaml b/backend/plugin/advisor/oracle/test/column_comment.yaml
new file mode 100644
index 0000000000..931af3e302
--- /dev/null
+++ b/backend/plugin/advisor/oracle/test/column_comment.yaml
@@ -0,0 +1,39 @@
+- statement: |-
+ CREATE TABLE book (
+ id int
+ );
+ COMMENT ON COLUMN book.id IS 'comments';
+- statement: |-
+ ALTER TABLE t ADD (d int, a int);
+ COMMENT ON COLUMN t.d IS 'comments';
+ 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 "TEST_DB"."T"."A"
+ startposition:
+ line: 1
+- statement: |-
+ ALTER TABLE t ADD (d int, a int);
+ COMMENT ON COLUMN t.a IS 'comments';
+ want:
+ - status: 2
+ code: 1032
+ title: column.comment
+ content: Comment is required for column "TEST_DB"."T"."D"
+ startposition:
+ line: 1
+- statement: |-
+ CREATE TABLE t(a int);
+ COMMENT ON COLUMN t.a IS 'comments';
+ COMMENT ON COLUMN t.a IS '';
+ want:
+ - status: 2
+ code: 1032
+ title: column.comment
+ content: Comment is required for column "TEST_DB"."T"."A"
+ startposition:
+ line: 1
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index f76e1d17f1..bf3f3bb1ee 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1102,6 +1102,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
return MySQLColumnCommentConvention, nil
case storepb.Engine_POSTGRES:
return PostgreSQLColumnCommentConvention, nil
+ case storepb.Engine_ORACLE, storepb.Engine_OCEANBASE_ORACLE:
+ return OracleColumnCommentConvention, nil
}
case SchemaRuleColumnAutoIncrementMustInteger:
switch engine {
@@ -1219,7 +1221,7 @@ 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:
+ case storepb.Engine_ORACLE, storepb.Engine_OCEANBASE_ORACLE:
return OracleTableCommentConvention, nil
}
case SchemaRuleTableDisallowPartition:
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 876f393265..aa466e9040 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -1537,6 +1537,38 @@
type: NUMBER
default: 64
engine: POSTGRES
+- 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: ORACLE
+- 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: OCEANBASE_ORACLE
- 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 efe59cba9e..1480d3acce 100644
--- a/frontend/src/types/sql-review.prod.yaml
+++ b/frontend/src/types/sql-review.prod.yaml
@@ -1156,6 +1156,18 @@ ruleList:
required: true
maxLength: 64
engine: POSTGRES
+ - type: column.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
+ engine: ORACLE
+ - type: column.comment
+ level: ERROR
+ payload:
+ required: true
+ maxLength: 64
+ engine: OCEANBASE_ORACLE
- type: column.maximum-character-length
level: WARNING
payload: