Skip to content

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

feat(sql-review): implement MySQL auto-increment column must integer rule (#2493)

rebeliceaa1183cf4062
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index cabe359dff..b442589ba1 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -103,6 +103,9 @@ const (
// MySQLColumnCommentConvention is an advisor type for MySQL column comment convention.
MySQLColumnCommentConvention Type = "bb.plugin.advisor.mysql.column.comment"
+ // MySQLAutoIncrementColumnMustInteger is an advisor type for auto-increment column.
+ MySQLAutoIncrementColumnMustInteger Type = "bb.plugin.advisor.mysql.column.auto-increment-must-integer"
+
// MySQLNoSelectAll is an advisor type for MySQL no select all.
MySQLNoSelectAll Type = "bb.plugin.advisor.mysql.select.no-select-all"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index ed0bcd33aa..d3f92c3465 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -44,15 +44,16 @@ const (
NamingPKConventionMismatch Code = 306
// 401 ~ 499 column error code.
- NoRequiredColumn Code = 401
- ColumnCanNotNull Code = 402
- ChangeColumnType Code = 403
- NotNullColumnWithNullDefault Code = 404
- ColumnNotExists Code = 405
- UseChangeColumnStatement Code = 406
- ChangeColumnOrder Code = 407
- NoColumnComment Code = 408
- ColumnCommentTooLong Code = 409
+ NoRequiredColumn Code = 401
+ ColumnCanNotNull Code = 402
+ ChangeColumnType Code = 403
+ NotNullColumnWithNullDefault Code = 404
+ ColumnNotExists Code = 405
+ UseChangeColumnStatement Code = 406
+ ChangeColumnOrder Code = 407
+ NoColumnComment Code = 408
+ ColumnCommentTooLong Code = 409
+ AutoIncrementColumnNotInteger Code = 410
// 501 engine error code.
NotInnoDBEngine Code = 501
diff --git a/plugin/advisor/mysql/advisor_column_auto_increment_must_integer.go b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer.go
new file mode 100644
index 0000000000..06171a096b
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer.go
@@ -0,0 +1,144 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/pingcap/tidb/parser/ast"
+ "github.com/pingcap/tidb/parser/mysql"
+ "github.com/pingcap/tidb/parser/types"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*ColumnAutoIncrementMustIntegerAdvisor)(nil)
+ _ ast.Visitor = (*columnAutoIncrementMustIntegerChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLAutoIncrementColumnMustInteger, &ColumnAutoIncrementMustIntegerAdvisor{})
+ advisor.Register(db.TiDB, 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, statement string) ([]advisor.Advice, error) {
+ stmtList, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ checker := &columnAutoIncrementMustIntegerChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmtList {
+ checker.text = stmt.Text()
+ checker.line = stmt.OriginTextPosition()
+ (stmt).Accept(checker)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type columnAutoIncrementMustIntegerChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// 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(),
+ })
+ }
+ }
+ 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(),
+ })
+ }
+ }
+ 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(),
+ })
+ }
+ }
+ }
+ }
+
+ for _, column := range columnList {
+ 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,
+ })
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*columnAutoIncrementMustIntegerChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
+
+func autoIncrementColumnIsInteger(column *ast.ColumnDef) bool {
+ for _, option := range column.Options {
+ if option.Tp == ast.ColumnOptionAutoIncrement && !isInteger(column.Tp) {
+ return false
+ }
+ }
+ return true
+}
+
+func isInteger(tp *types.FieldType) bool {
+ switch tp.Tp {
+ case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
+ return true
+ default:
+ return false
+ }
+}
diff --git a/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go
new file mode 100644
index 0000000000..f129bcf410
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_column_auto_increment_must_integer_test.go
@@ -0,0 +1,79 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestColumnAutoIncrementMustInteger(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `CREATE TABLE t(a INT AUTO_INCREMENT)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE t(a varchar(255) AUTO_INCREMENT)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.AutoIncrementColumnNotInteger,
+ Title: "column.auto-increment-must-integer",
+ Content: "Auto-increment column `t`.`a` requires integer type",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t ADD COLUMN a varchar(255) AUTO_INCREMENT`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.AutoIncrementColumnNotInteger,
+ Title: "column.auto-increment-must-integer",
+ Content: "Auto-increment column `t`.`a` requires integer type",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t MODIFY COLUMN a varchar(255) AUTO_INCREMENT`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.AutoIncrementColumnNotInteger,
+ Title: "column.auto-increment-must-integer",
+ Content: "Auto-increment column `t`.`a` requires integer type",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE t CHANGE COLUMN b a varchar(255) AUTO_INCREMENT`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.AutoIncrementColumnNotInteger,
+ Title: "column.auto-increment-must-integer",
+ Content: "Auto-increment column `t`.`a` requires integer type",
+ Line: 1,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &ColumnAutoIncrementMustIntegerAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleColumnAutoIncrementMustInteger,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/mysql/advisor_column_exists.go b/plugin/advisor/mysql/advisor_column_exists.go
index c0ac865fcd..203e0debc5 100644
--- a/plugin/advisor/mysql/advisor_column_exists.go
+++ b/plugin/advisor/mysql/advisor_column_exists.go
@@ -72,6 +72,7 @@ type columnExistsChecker struct {
type columnData struct {
table string
column string
+ line int
}
// Enter implements the ast.Visitor interface.
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index f65d0fa168..cfcd4c09b4 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -84,6 +84,8 @@ const (
SchemaRuleColumnDisallowChangingOrder SQLReviewRuleType = "column.disallow-changing-order"
// SchemaRuleColumnCommentConvention enforce the column comment convention.
SchemaRuleColumnCommentConvention SQLReviewRuleType = "column.comment"
+ // SchemaRuleColumnAutoIncrementMustInteger require the auto-increment column to be integer.
+ SchemaRuleColumnAutoIncrementMustInteger SQLReviewRuleType = "column.auto-increment-must-integer"
// SchemaRuleSchemaBackwardCompatibility enforce the MySQL and TiDB support check whether the schema change is backward compatible.
SchemaRuleSchemaBackwardCompatibility SQLReviewRuleType = "schema.backward-compatibility"