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 disallow_partition (#9071)
zll6001ff504850a73
diff --git a/backend/plugin/advisor/mysqlwip/advisor_table_disallow_partition.go b/backend/plugin/advisor/mysqlwip/advisor_table_disallow_partition.goindex 7924a0c6c9..3cd407b9ec 100644--- a/backend/plugin/advisor/mysqlwip/advisor_table_disallow_partition.go+++ b/backend/plugin/advisor/mysqlwip/advisor_table_disallow_partition.go@@ -1,30 +1,36 @@package mysqlwip-// Framework code is generated by the generator.-import ("fmt"- "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 = (*TableDisallowPartitionAdvisor)(nil)- _ ast.Visitor = (*tableDisallowPartitionChecker)(nil))+func init() {+ // only for mysqlwip test.+ advisor.Register(storepb.Engine_ENGINE_UNSPECIFIED, advisor.MySQLTableDisallowPartition, &TableDisallowPartitionAdvisor{})+}+// TableDisallowPartitionAdvisor is the advisor checking for disallow table partition.type TableDisallowPartitionAdvisor struct {}// Check checks for disallow table partition.func (*TableDisallowPartitionAdvisor) 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)@@ -37,9 +43,8 @@ func (*TableDisallowPartitionAdvisor) Check(ctx advisor.Context, _ string) ([]ad}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 {@@ -54,28 +59,24 @@ func (*TableDisallowPartitionAdvisor) Check(ctx advisor.Context, _ string) ([]ad}type tableDisallowPartitionChecker struct {+ *mysql.BaseMySQLParserListener++ baseLine intadviceList []advisor.Advicelevel advisor.Statustitle stringtext string- line int}-// Enter implements the ast.Visitor interface.-func (checker *tableDisallowPartitionChecker) Enter(in ast.Node) (ast.Node, bool) {+func (checker *tableDisallowPartitionChecker) EnterQuery(ctx *mysql.QueryContext) {+ checker.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)+}++// EnterCreateDatabase is called when production createDatabase is entered.+func (checker *tableDisallowPartitionChecker) EnterCreateTable(ctx *mysql.CreateTableContext) {code := advisor.Ok- switch node := in.(type) {- case *ast.CreateTableStmt:- if node.Partition != nil {- code = advisor.CreateTablePartition- }- case *ast.AlterTableStmt:- for _, spec := range node.Specs {- if spec.Tp == ast.AlterTablePartition {- code = advisor.CreateTablePartition- break- }- }+ if ctx.PartitionClause() != nil && ctx.PartitionClause().PartitionTypeDef() != nil {+ code = advisor.CreateTablePartition}if code != advisor.Ok {@@ -84,14 +85,24 @@ func (checker *tableDisallowPartitionChecker) Enter(in ast.Node) (ast.Node, boolCode: code,Title: checker.title,Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", checker.text),- Line: checker.line,+ Line: checker.baseLine + ctx.GetStart().GetLine(),})}-- return in, false}-// Leave implements the ast.Visitor interface.-func (*tableDisallowPartitionChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+// EnterAlterTable is called when production alterTable is entered.+func (checker *tableDisallowPartitionChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {+ code := advisor.Ok+ if ctx.AlterTableActions().PartitionClause() != nil && ctx.AlterTableActions().PartitionClause().PartitionTypeDef() != nil {+ code = advisor.CreateTablePartition+ }+ if code != advisor.Ok {+ checker.adviceList = append(checker.adviceList, advisor.Advice{+ Status: checker.level,+ Code: code,+ Title: checker.title,+ Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", checker.text),+ 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 90099694fd..42a4e92851 100644--- a/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go+++ b/backend/plugin/advisor/mysqlwip/mysqlwip_rules_test.go@@ -54,6 +54,8 @@ func TestMySQLWIPRules(t *testing.T) {advisor.SchemaRuleTableDropNamingConvention,// advisor.SchemaRuleTableCommentConvention enforce the table comment convention.advisor.SchemaRuleTableCommentConvention,+ // advisor.SchemaRuleTableDisallowPartition disallow the table partition.+ advisor.SchemaRuleTableDisallowPartition,// 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/table_disallow_partition.yaml b/backend/plugin/advisor/mysqlwip/test/table_disallow_partition.yamlindex 0d5307a0ae..aec1fbeaa4 100644--- a/backend/plugin/advisor/mysqlwip/test/table_disallow_partition.yaml+++ b/backend/plugin/advisor/mysqlwip/test/table_disallow_partition.yaml@@ -1,4 +1,4 @@-- statement: CREATE TABLE t(a int)+- statement: CREATE TABLE t(a int);want:- status: SUCCESScode: 0@@ -7,10 +7,11 @@line: 0details: ""- statement: |-+ CREATE TABLE b(a INT);CREATE TABLE t(a int) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11)- )+ );want:- status: WARNcode: 608@@ -19,15 +20,14 @@Table partition is forbidden, but "CREATE TABLE t(a int) PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11)- )- ;" creates- line: 5+ );" creates+ line: 2details: ""- statement: |-ALTER TABLE tech_book PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11)- )+ );want:- status: WARNcode: 608@@ -36,7 +36,36 @@Table partition is forbidden, but "ALTER TABLE tech_book PARTITION BY RANGE (a) (PARTITION p0 VALUES LESS THAN (6),PARTITION p1 VALUES LESS THAN (11)- )- ;" creates+ );" creates+ line: 1+ details: ""+- statement: |-+ CREATE TABLE t(a int) PARTITION BY RANGE (a) (+ PARTITION p0 VALUES LESS THAN (6),+ PARTITION p1 VALUES LESS THAN (11)+ );+ ALTER TABLE tech_book PARTITION BY RANGE (a) (+ PARTITION p0 VALUES LESS THAN (6),+ PARTITION p1 VALUES LESS THAN (11)+ );+ want:+ - status: WARN+ code: 608+ title: table.disallow-partition+ content: |-+ Table partition is forbidden, but "CREATE TABLE t(a int) PARTITION BY RANGE (a) (+ PARTITION p0 VALUES LESS THAN (6),+ PARTITION p1 VALUES LESS THAN (11)+ );" creates+ line: 1+ details: ""+ - status: WARN+ code: 608+ title: table.disallow-partition+ content: |-+ Table partition is forbidden, but "ALTER TABLE tech_book PARTITION BY RANGE (a) (+ PARTITION p0 VALUES LESS THAN (6),+ PARTITION p1 VALUES LESS THAN (11)+ );" createsline: 5details: ""diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 11137e93e6..1414e79366 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -1343,6 +1343,9 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty}case SchemaRuleTableDisallowPartition:switch engine {+ // only for mysqlwip test.+ case storepb.Engine_ENGINE_UNSPECIFIED:+ return MySQLTableDisallowPartition, nilcase storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return MySQLTableDisallowPartition, nilcase storepb.Engine_POSTGRES: