Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: migrate auto_increment_initial_value (#9132)
zll6006e045c33a481
diff --git a/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_initial_value.go b/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_initial_value.goindex 70e900e75a..b325e60be7 100644--- a/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_initial_value.go+++ b/backend/plugin/advisor/mysqlwip/advisor_column_auto_increment_initial_value.go@@ -4,27 +4,36 @@ package mysqlwipimport ("fmt"+ "strconv"- "github.com/pingcap/tidb/parser/ast"+ "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 = (*ColumnAutoIncrementInitialValueAdvisor)(nil)- _ ast.Visitor = (*columnAutoIncrementInitialValueChecker)(nil))+func init() {+ // only for mysqlwip test.+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLAutoIncrementColumnInitialValue, &ColumnAutoIncrementInitialValueAdvisor{})+}+// ColumnAutoIncrementInitialValueAdvisor is the advisor checking for auto-increment column initial value.type ColumnAutoIncrementInitialValueAdvisor struct {}// Check checks for auto-increment column initial value.func (*ColumnAutoIncrementInitialValueAdvisor) 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 parser result")}level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)@@ -42,9 +51,8 @@ func (*ColumnAutoIncrementInitialValueAdvisor) Check(ctx advisor.Context, _ stri}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 {@@ -59,36 +67,85 @@ func (*ColumnAutoIncrementInitialValueAdvisor) Check(ctx advisor.Context, _ stri}type columnAutoIncrementInitialValueChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine intadviceList []advisor.Advicelevel advisor.Statustitle string- text string- line intvalue int}-// Enter implements the ast.Visitor interface.-func (checker *columnAutoIncrementInitialValueChecker) Enter(in ast.Node) (ast.Node, bool) {- if createTable, ok := in.(*ast.CreateTableStmt); ok {- for _, option := range createTable.Options {- if option.Tp == ast.TableOptionAutoIncrement {- if option.UintValue != uint64(checker.value) {- checker.adviceList = append(checker.adviceList, advisor.Advice{- Status: checker.level,- Code: advisor.AutoIncrementColumnInitialValueNotMatch,- Title: checker.title,- Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", createTable.Table.Name.O, option.UintValue, checker.value),- Line: checker.line,- })- }- }- }+func (checker *columnAutoIncrementInitialValueChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {+ if ctx.CreateTableOptions() == nil || ctx.TableName() == nil {+ return}- return in, false+ _, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())+ for _, option := range ctx.CreateTableOptions().AllCreateTableOption() {+ if option.AUTO_INCREMENT_SYMBOL() == nil || option.Ulonglong_number() == nil {+ continue+ }++ base := 10+ bitSize := 0+ value, err := strconv.ParseUint(option.Ulonglong_number().GetText(), base, bitSize)+ if err != nil {+ continue+ }+ if value != uint64(checker.value) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.AutoIncrementColumnInitialValueNotMatch,+ Title: checker.title,+ Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", tableName, value, checker.value),+ Line: checker.baseLine + ctx.GetStart().GetLine(),+ })+ }+ }}-// Leave implements the ast.Visitor interface.-func (*columnAutoIncrementInitialValueChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+// EnterAlterTable is called when production alterTable is entered.+func (checker *columnAutoIncrementInitialValueChecker) 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 option.+ for _, option := range ctx.AlterTableActions().AlterCommandList().AlterList().AllCreateTableOptionsSpaceSeparated() {+ if option == nil {+ continue+ }+ for _, tableOption := range option.AllCreateTableOption() {+ if tableOption == nil || tableOption.AUTO_INCREMENT_SYMBOL() == nil || tableOption.Ulonglong_number() == nil {+ continue+ }++ base := 10+ bitSize := 0+ value, err := strconv.ParseUint(tableOption.Ulonglong_number().GetText(), base, bitSize)+ if err != nil {+ continue+ }+ if value != uint64(checker.value) {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: advisor.AutoIncrementColumnInitialValueNotMatch,+ Title: checker.title,+ Content: fmt.Sprintf("The initial auto-increment value in table `%s` is %v, which doesn't equal %v", tableName, value, checker.value),+ Line: checker.baseLine + ctx.GetStart().GetLine(),+ })+ }+ }+ }}diff --git a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.goindex 55bbf23099..26c49fcaab 100644--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go@@ -59,13 +59,14 @@ func TestMySQLWIPRules(t *testing.T) {// advisor.SchemaRuleRequiredColumn enforce the required columns in each table.advisor.SchemaRuleRequiredColumn,-// advisor.SchemaRuleColumnNotNull enforce the columns cannot have NULL value.advisor.SchemaRuleColumnNotNull,// 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.+ advisor.SchemaRuleColumnAutoIncrementInitialValue,// advisor.SchemaRuleDropEmptyDatabase enforce the MySQL support check if the database is empty before users drop it.advisor.SchemaRuleDropEmptyDatabase,diff --git a/backend/plugin/advisor/mysqlwip/test/column_auto_increment_initial_value.yaml b/backend/plugin/advisor/mysqlwip/test/column_auto_increment_initial_value.yamlindex c84eb6f231..670c9c38f6 100644--- a/backend/plugin/advisor/mysqlwip/test/column_auto_increment_initial_value.yaml+++ b/backend/plugin/advisor/mysqlwip/test/column_auto_increment_initial_value.yaml@@ -1,4 +1,4 @@-- statement: CREATE TABLE t(a int)+- statement: CREATE TABLE t(a int);want:- status: SUCCESScode: 0@@ -6,7 +6,7 @@content: ""line: 0details: ""-- statement: CREATE TABLE t(a int auto_increment) auto_increment = 20+- statement: CREATE TABLE t(a int auto_increment) auto_increment = 20;want:- status: SUCCESScode: 0@@ -14,11 +14,25 @@content: ""line: 0details: ""-- statement: CREATE TABLE t(a int auto_increment) auto_increment = 2+- statement: |-+ CREATE TABLE t1(a int auto_increment) auto_increment = 20;+ CREATE TABLE t2(a int auto_increment) auto_increment = 2;want:- status: WARNcode: 416title: column.auto-increment-initial-value- content: The initial auto-increment value in table `t` is 2, which doesn't equal 20+ content: The initial auto-increment value in table `t2` is 2, which doesn't equal 20line: 2details: ""+- statement: |-+ CREATE TABLE t(a int auto_increment) auto_increment = 20;+ ALTER TABLE t COMMENT 'table t';+ ALTER TABLE t AUTO_INCREMENT = 20;+ ALTER TABLE t AUTO_INCREMENT = 100;+ want:+ - status: WARN+ code: 416+ title: column.auto-increment-initial-value+ content: The initial auto-increment value in table `t` is 100, which doesn't equal 20+ line: 4+ details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 99c85eb591..d710ef00b6 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1278,6 +1278,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty}case SchemaRuleColumnAutoIncrementInitialValue:switch engine {+ // only for mysqlwip test.+ case storepb.Engine_ENGINE_UNSPECIFIED:+ return MySQLAutoIncrementColumnInitialValue, nilcase storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return MySQLAutoIncrementColumnInitialValue, nil}