Skip to content

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

fix(advisor): support column comment check for MySQL generated columns (#18473)

h3n4l371fa0e66179
diff --git a/backend/plugin/advisor/mysql/rule_column_comment_convention.go b/backend/plugin/advisor/mysql/rule_column_comment_convention.go
index 6b028e6b01..0693a0166a 100644
--- a/backend/plugin/advisor/mysql/rule_column_comment_convention.go
+++ b/backend/plugin/advisor/mysql/rule_column_comment_convention.go
@@ -178,6 +178,8 @@ func (r *ColumnCommentConventionRule) checkAlterTable(ctx *mysql.AlterTableConte
func (r *ColumnCommentConventionRule) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
comment := ""
+
+ // Check columnAttribute for regular columns.
for _, attribute := range ctx.AllColumnAttribute() {
if attribute == nil || attribute.GetValue() == nil {
continue
@@ -189,20 +191,37 @@ func (r *ColumnCommentConventionRule) checkFieldDefinition(tableName, columnName
continue
}
comment = mysqlparser.NormalizeMySQLTextLiteral(attribute.TextLiteral())
- if r.payload.MaxLength >= 0 && int32(len(comment)) > r.payload.MaxLength {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: code.CommentTooLong.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The length of column `%s`.`%s` comment should be within %d characters", tableName, columnName, r.payload.MaxLength),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+ break
+ }
+
+ // Check gcolAttribute for generated/virtual columns.
+ // Generated columns use gcolAttribute which has a different structure.
+ if comment == "" {
+ for _, gcolAttr := range ctx.AllGcolAttribute() {
+ if gcolAttr == nil || gcolAttr.COMMENT_SYMBOL() == nil {
+ continue
+ }
+ if gcolAttr.TextString() == nil || gcolAttr.TextString().TextStringLiteral() == nil {
+ continue
+ }
+ comment = mysqlparser.NormalizeMySQLTextStringLiteral(gcolAttr.TextString().TextStringLiteral())
+ break
}
+ }
- break
+ // Validate comment length.
+ if comment != "" && r.payload.MaxLength >= 0 && int32(len(comment)) > r.payload.MaxLength {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: code.CommentTooLong.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("The length of column `%s`.`%s` comment should be within %d characters", tableName, columnName, r.payload.MaxLength),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ })
}
- if len(comment) == 0 && r.payload.Required {
+ // Check if comment is required but missing.
+ if comment == "" && r.payload.Required {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.CommentEmpty.Int32(),
diff --git a/backend/plugin/advisor/mysql/test/column_comment.yaml b/backend/plugin/advisor/mysql/test/column_comment.yaml
index 3ef6cd22db..724a347e10 100644
--- a/backend/plugin/advisor/mysql/test/column_comment.yaml
+++ b/backend/plugin/advisor/mysql/test/column_comment.yaml
@@ -181,3 +181,66 @@
line: 2
column: 0
endposition: null
+# Test cases for virtual/generated columns (BYT-8503)
+- statement: |-
+ CREATE TABLE users (
+ id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'ID',
+ first_name VARCHAR(60) NOT NULL COMMENT 'First',
+ last_name VARCHAR(60) NOT NULL COMMENT 'Last',
+ full_name VARCHAR(120) GENERATED ALWAYS AS (CONCAT(first_name, ' ', last_name)) VIRTUAL COMMENT 'Full'
+ );
+ changeType: 1
+# Virtual column with comment - should pass
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL COMMENT 'comment'
+ );
+ changeType: 1
+# Stored generated column with comment - should pass
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) STORED COMMENT 'comment'
+ );
+ changeType: 1
+# Virtual column without comment - should fail
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL
+ );
+ changeType: 1
+ want:
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`b` requires comments
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
+# Mixed regular and generated columns
+- statement: |-
+ CREATE TABLE t(
+ a int,
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL
+ );
+ changeType: 1
+ want:
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`a` requires comments
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`b` requires comments
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/tidb/test/column_comment.yaml b/backend/plugin/advisor/tidb/test/column_comment.yaml
index aeb6eb4c37..c56ff134b1 100644
--- a/backend/plugin/advisor/tidb/test/column_comment.yaml
+++ b/backend/plugin/advisor/tidb/test/column_comment.yaml
@@ -107,3 +107,66 @@
line: 2
column: 0
endposition: null
+# Test cases for virtual/generated columns (BYT-8503)
+- statement: |-
+ CREATE TABLE users (
+ id INT AUTO_INCREMENT PRIMARY KEY COMMENT 'ID',
+ first_name VARCHAR(60) NOT NULL COMMENT 'First',
+ last_name VARCHAR(60) NOT NULL COMMENT 'Last',
+ full_name VARCHAR(120) GENERATED ALWAYS AS (CONCAT(first_name, ' ', last_name)) VIRTUAL COMMENT 'Full'
+ )
+ changeType: 1
+# Virtual column with comment - should pass
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL COMMENT 'comment'
+ )
+ changeType: 1
+# Stored generated column with comment - should pass
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) STORED COMMENT 'comment'
+ )
+ changeType: 1
+# Virtual column without comment - should fail
+- statement: |-
+ CREATE TABLE t(
+ a int COMMENT 'comment',
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL
+ )
+ changeType: 1
+ want:
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`b` requires comments
+ startposition:
+ line: 3
+ column: 0
+ endposition: null
+# Mixed regular and generated columns
+- statement: |-
+ CREATE TABLE t(
+ a int,
+ b int GENERATED ALWAYS AS (a * 2) VIRTUAL
+ )
+ changeType: 1
+ want:
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`a` requires comments
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+ - status: 2
+ code: 1032
+ title: COLUMN_COMMENT
+ content: Column `t`.`b` requires comments
+ startposition:
+ line: 3
+ column: 0
+ endposition: null