Skip to content

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

chore: migrate column_auto_increment_must_integer (#9288)

zll600ada961586f7a
diff --git a/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_must_integer.go b/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_must_integer.go
index eb27f3284a..f5f7b6c0fa 100644
--- a/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_must_integer.go
+++ b/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_must_integer.go
@@ -5,28 +5,34 @@ package mysqlwip
import (
"fmt"
- "github.com/pingcap/tidb/parser/ast"
- "github.com/pingcap/tidb/parser/mysql"
- "github.com/pingcap/tidb/parser/types"
+ "github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
+ mysql "github.com/bytebase/mysql-parser"
+
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var (
_ advisor.Advisor = (*ColumnAutoIncrementMustIntegerAdvisor)(nil)
- _ ast.Visitor = (*columnAutoIncrementMustIntegerChecker)(nil)
)
+func init() {
+ // only for mysqlwip test.
+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLAutoIncrementColumnMustInteger, &ColumnAutoIncrementMustIntegerAdvisor{})
+}
+
// ColumnAutoIncrementMustIntegerAdvisor is the advisor checking for auto-increment column type.
type ColumnAutoIncrementMustIntegerAdvisor struct {
}
// Check checks for auto-increment column type.
func (*ColumnAutoIncrementMustIntegerAdvisor) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
- stmtList, ok := ctx.AST.([]ast.StmtNode)
+ stmtList, ok := ctx.AST.([]*mysqlparser.ParseResult)
if !ok {
- return nil, errors.Errorf("failed to convert to StmtNode")
+ return nil, errors.Errorf("failed to convert to mysql parse result")
}
level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
@@ -39,9 +45,8 @@ func (*ColumnAutoIncrementMustIntegerAdvisor) Check(ctx advisor.Context, _ strin
}
for _, stmt := range stmtList {
- checker.text = stmt.Text()
- checker.line = stmt.OriginTextPosition()
- (stmt).Accept(checker)
+ checker.baseLine = stmt.BaseLine
+ antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
if len(checker.adviceList) == 0 {
@@ -56,88 +61,117 @@ func (*ColumnAutoIncrementMustIntegerAdvisor) Check(ctx advisor.Context, _ strin
}
type columnAutoIncrementMustIntegerChecker struct {
+ *mysql.BaseMySQLParserListener
+
+ baseLine int
adviceList []advisor.Advice
level advisor.Status
title string
- text string
- line int
}
-type columnData struct {
- table string
- column string
- line int
+func (checker *columnAutoIncrementMustIntegerChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {
+ if ctx.TableElementList() == nil || ctx.TableName() == nil {
+ return
+ }
+
+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
+ for _, tableElement := range ctx.TableElementList().AllTableElement() {
+ if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition().DataType() == nil {
+ continue
+ }
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ checker.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
+ }
}
-// Enter implements the ast.Visitor interface.
-func (checker *columnAutoIncrementMustIntegerChecker) Enter(in ast.Node) (ast.Node, bool) {
- var columnList []columnData
- switch node := in.(type) {
- case *ast.CreateTableStmt:
- for _, column := range node.Cols {
- if !autoIncrementColumnIsInteger(column) {
- columnList = append(columnList, columnData{
- table: node.Table.Name.O,
- column: column.Name.Name.O,
- line: column.OriginTextPosition(),
- })
- }
+func (checker *columnAutoIncrementMustIntegerChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {
+ if ctx.AlterTableActions() == nil {
+ return
+ }
+ if ctx.AlterTableActions().AlterCommandList() == nil {
+ return
+ }
+ if ctx.AlterTableActions().AlterCommandList().AlterList() == nil {
+ return
+ }
+
+ _, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+ if tableName == "" {
+ return
+ }
+ // alter table add column, change column, modify column.
+ for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
+ if item == nil {
+ continue
}
- case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- switch spec.Tp {
- case ast.AlterTableAddColumns:
- for _, column := range spec.NewColumns {
- if !autoIncrementColumnIsInteger(column) {
- columnList = append(columnList, columnData{
- table: node.Table.Name.O,
- column: column.Name.Name.O,
- line: node.OriginTextPosition(),
- })
+
+ var columnName string
+ switch {
+ // add column
+ case item.ADD_SYMBOL() != nil:
+ switch {
+ case item.Identifier() != nil && item.FieldDefinition() != nil:
+ columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ checker.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
+ case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
+ for _, tableElement := range item.TableElementList().AllTableElement() {
+ if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().ColumnName() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
+ continue
}
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ checker.checkFieldDefinition(tableName, columnName, tableElement.ColumnDefinition().FieldDefinition())
}
- case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
- if !autoIncrementColumnIsInteger(spec.NewColumns[0]) {
- columnList = append(columnList, columnData{
- table: node.Table.Name.O,
- column: spec.NewColumns[0].Name.Name.O,
- line: node.OriginTextPosition(),
- })
- }
}
+ // change column.
+ case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
+ // oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ if item.FieldDefinition().DataType() == nil {
+ continue
+ }
+ columnName = mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ checker.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
+ // modify column.
+ case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
+ if item.FieldDefinition().DataType() == nil {
+ continue
+ }
+ columnName = mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ checker.checkFieldDefinition(tableName, columnName, item.FieldDefinition())
}
}
+}
- for _, column := range columnList {
+func (checker *columnAutoIncrementMustIntegerChecker) checkFieldDefinition(tableName, columnName string, ctx mysql.IFieldDefinitionContext) {
+ if !checker.isAutoIncrementColumnIsInteger(ctx) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
Code: advisor.AutoIncrementColumnNotInteger,
Title: checker.title,
- Content: fmt.Sprintf("Auto-increment column `%s`.`%s` requires integer type", column.table, column.column),
- Line: checker.line,
+ Content: fmt.Sprintf("Auto-increment column `%s`.`%s` requires integer type", tableName, columnName),
+ Line: checker.baseLine + ctx.GetStart().GetLine(),
})
}
-
- return in, false
}
-// Leave implements the ast.Visitor interface.
-func (*columnAutoIncrementMustIntegerChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+func (checker *columnAutoIncrementMustIntegerChecker) isAutoIncrementColumnIsInteger(ctx mysql.IFieldDefinitionContext) bool {
+ if checker.isAutoIncrementColumn(ctx) && !checker.isIntegerType(ctx.DataType()) {
+ return false
+ }
+ return true
}
-func autoIncrementColumnIsInteger(column *ast.ColumnDef) bool {
- for _, option := range column.Options {
- if option.Tp == ast.ColumnOptionAutoIncrement && !isInteger(column.Tp) {
- return false
+func (*columnAutoIncrementMustIntegerChecker) isAutoIncrementColumn(ctx mysql.IFieldDefinitionContext) bool {
+ for _, attr := range ctx.AllColumnAttribute() {
+ if attr.AUTO_INCREMENT_SYMBOL() != nil {
+ return true
}
}
- return true
+ return false
}
-func isInteger(tp *types.FieldType) bool {
- switch tp.GetType() {
- case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
+func (*columnAutoIncrementMustIntegerChecker) isIntegerType(ctx mysql.IDataTypeContext) bool {
+ switch ctx.GetType_().GetTokenType() {
+ case mysql.MySQLParserINT_SYMBOL, mysql.MySQLParserTINYINT_SYMBOL, mysql.MySQLParserSMALLINT_SYMBOL, mysql.MySQLParserMEDIUMINT_SYMBOL, mysql.MySQLParserBIGINT_SYMBOL:
return true
default:
return false
diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
index 5a8ced3d37..9a8d91f8b3 100644
--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go
@@ -79,10 +79,12 @@ func TestMySQLWIPRules(t *testing.T) {
advisor.SchemaRuleColumnDisallowDropInIndex,
// advisor.SchemaRuleColumnCommentConvention enforce the column comment convention.
advisor.SchemaRuleColumnCommentConvention,
- // advisor.SchemaRuleColumnDisallowSetCharset disallow set column charset.
- advisor.SchemaRuleColumnDisallowSetCharset,
+ // advisor.SchemaRuleColumnAutoIncrementMustInteger require the auto-increment column to be integer.
+ advisor.SchemaRuleColumnAutoIncrementMustInteger,
// advisor.SchemaRuleColumnTypeDisallowList enforce the column type disallow list.
advisor.SchemaRuleColumnTypeDisallowList,
+ // advisor.SchemaRuleColumnDisallowSetCharset disallow set column charset.
+ advisor.SchemaRuleColumnDisallowSetCharset,
// advisor.SchemaRuleColumnMaximumCharacterLength enforce the maximum character length.
advisor.SchemaRuleColumnMaximumCharacterLength,
// advisor.SchemaRuleColumnAutoIncrementInitialValue enforce the initial auto-increment value.
diff --git a/backend/plugin/advisor/mysqlwip/test/column_auto_increment_must_integer.yaml b/backend/plugin/advisor/mysqlwip/test/column_auto_increment_must_integer.yaml
index d23d0b34d5..a69ec37dc5 100644
--- a/backend/plugin/advisor/mysqlwip/test/column_auto_increment_must_integer.yaml
+++ b/backend/plugin/advisor/mysqlwip/test/column_auto_increment_must_integer.yaml
@@ -1,4 +1,14 @@
-- statement: CREATE TABLE t(a INT AUTO_INCREMENT)
+- statement: |-
+ CREATE TABLE t1(a INT AUTO_INCREMENT);
+ CREATE TABLE t2(a int AUTO_INCREMENT);
+ CREATE TABLE t3(a INTEGER AUTO_INCREMENT);
+ CREATE TABLE t4(a integer AUTO_INCREMENT);
+ CREATE TABLE t5(a TINYINT AUTO_INCREMENT);
+ CREATE TABLE t6(a tinyint AUTO_INCREMENT);
+ CREATE TABLE t7(a mediumint AUTO_INCREMENT);
+ CREATE TABLE t8(a MEDIUMINT AUTO_INCREMENT);
+ CREATE TABLE t9(a bigint AUTO_INCREMENT);
+ CREATE TABLE t10(a BIGINT AUTO_INCREMENT);
want:
- status: SUCCESS
code: 0
@@ -6,7 +16,29 @@
content: ""
line: 0
details: ""
-- statement: CREATE TABLE t(a varchar(255) AUTO_INCREMENT)
+- statement: |-
+ CREATE TABLE t1(a INT AUTO_INCREMENT);
+ CREATE TABLE t2(a varchar(255) AUTO_INCREMENT);
+ want:
+ - status: WARN
+ code: 410
+ title: column.auto-increment-must-integer
+ content: Auto-increment column `t2`.`a` requires integer type
+ line: 2
+ details: ""
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT;
+ want:
+ - status: WARN
+ code: 410
+ title: column.auto-increment-must-integer
+ content: Auto-increment column `t`.`a` requires integer type
+ line: 2
+ details: ""
+- statement: |-
+ CREATE TABLE t(b int);
+ ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT, ADD COLUMN c INT;
want:
- status: WARN
code: 410
@@ -16,31 +48,32 @@
details: ""
- statement: |-
CREATE TABLE t(b int);
- ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT
+ ALTER TABLE t ADD COLUMN (a varchar(255) AUTO_INCREMENT, c INT);
want:
- status: WARN
code: 410
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
- line: 3
+ line: 2
details: ""
- statement: |-
- CREATE TABLE t(a int);
- ALTER TABLE t MODIFY COLUMN a varchar(255) AUTO_INCREMENT
+ CREATE TABLE t(a int, b int);
+ ALTER TABLE t MODIFY COLUMN a varchar(255) AUTO_INCREMENT;
+ ALTER TABLE t MODIFY COLUMN b varchar(255);
want:
- status: WARN
code: 410
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
- line: 3
+ line: 2
details: ""
- statement: |-
CREATE TABLE t(b int);
- ALTER TABLE t CHANGE COLUMN b a varchar(255) AUTO_INCREMENT
+ ALTER TABLE t CHANGE COLUMN b a varchar(255) AUTO_INCREMENT;
want:
- status: WARN
code: 410
title: column.auto-increment-must-integer
content: Auto-increment column `t`.`a` requires integer type
- line: 3
+ line: 2
details: ""
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index b3a615b30e..8d707582a6 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -1256,6 +1256,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
}
case SchemaRuleColumnAutoIncrementMustInteger:
switch engine {
+ // only for mysqlwip test.
+ case storepb.Engine_ENGINE_UNSPECIFIED:
+ return MySQLAutoIncrementColumnMustInteger, nil
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
return MySQLAutoIncrementColumnMustInteger, nil
}