Skip to content

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

feat(sql-review): implement MySQL disallow-table-partition rule (#2831)

rebelice07dd15011169
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 2728dc88db..c840149b86 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -127,6 +127,9 @@ const (
// MySQLTableDisallowCreateTableAs is an advisor type for MySQL disallow CREATE TABLE ... AS ... statement.
MySQLTableDisallowCreateTableAs Type = "bb.plugin.advisor.mysql.table.disallow-create-table-as"
+ // MySQLTableDisallowPartition is an advisor type for MySQL disallow table partition.
+ MySQLTableDisallowPartition Type = "bb.plugin.advisor.mysql.table.disallow-partition"
+
// MySQLDatabaseAllowDropIfEmpty is an advisor type for MySQL only allow drop empty database.
MySQLDatabaseAllowDropIfEmpty Type = "bb.plugin.advisor.mysql.database.drop-empty-database"
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index c336b2bf9b..a9c8259db7 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -72,6 +72,7 @@ const (
NoTableComment Code = 605
TableCommentTooLong Code = 606
TableExists Code = 607
+ CreateTablePartition Code = 608
// 701 ~ 799 database advisor error code.
DatabaseNotEmpty Code = 701
diff --git a/plugin/advisor/mysql/advisor_table_disallow_partition.go b/plugin/advisor/mysql/advisor_table_disallow_partition.go
new file mode 100644
index 0000000000..86b3aada3a
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_disallow_partition.go
@@ -0,0 +1,102 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/pingcap/tidb/parser/ast"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+)
+
+var (
+ _ advisor.Advisor = (*TableDisallowPartitionAdvisor)(nil)
+ _ ast.Visitor = (*tableDisallowPartitionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.MySQL, advisor.MySQLTableDisallowPartition, &TableDisallowPartitionAdvisor{})
+ advisor.Register(db.TiDB, 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, 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 := &tableDisallowPartitionChecker{
+ 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 tableDisallowPartitionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ text string
+ line int
+}
+
+// Enter implements the ast.Visitor interface.
+func (checker *tableDisallowPartitionChecker) Enter(in ast.Node) (ast.Node, bool) {
+ 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 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.line,
+ })
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface.
+func (*tableDisallowPartitionChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_table_disallow_partition_test.go b/plugin/advisor/mysql/advisor_table_disallow_partition_test.go
new file mode 100644
index 0000000000..43a369e2d6
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_disallow_partition_test.go
@@ -0,0 +1,61 @@
+package mysql
+
+// Framework code is generated by the generator.
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+)
+
+func TestTableDisallowPartition(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: `CREATE TABLE t(a int)`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: `CREATE TABLE t(a int) PARTITION BY RANGE (a) (
+ PARTITION p0 VALUES LESS THAN (6),
+ PARTITION p1 VALUES LESS THAN (11)
+ )`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.CreateTablePartition,
+ Title: "table.disallow-partition",
+ Content: "Table partition is forbidden, but \"CREATE TABLE t(a int) PARTITION BY RANGE (a) (\n\t\t\t\tPARTITION p0 VALUES LESS THAN (6),\n\t\t\t\tPARTITION p1 VALUES LESS THAN (11)\n\t\t\t)\" creates",
+ Line: 4,
+ },
+ },
+ },
+ {
+ Statement: `ALTER TABLE tech_book PARTITION BY RANGE (a) (
+ PARTITION p0 VALUES LESS THAN (6),
+ PARTITION p1 VALUES LESS THAN (11)
+ )`,
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Warn,
+ Code: advisor.CreateTablePartition,
+ Title: "table.disallow-partition",
+ Content: "Table partition is forbidden, but \"ALTER TABLE tech_book PARTITION BY RANGE (a) (\n\t\t\t\tPARTITION p0 VALUES LESS THAN (6),\n\t\t\t\tPARTITION p1 VALUES LESS THAN (11)\n\t\t\t)\" creates",
+ Line: 4,
+ },
+ },
+ },
+ }
+
+ advisor.RunSQLReviewRuleTests(t, tests, &TableDisallowPartitionAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleTableDisallowPartition,
+ Level: advisor.SchemaRuleLevelWarning,
+ Payload: "",
+ }, advisor.MockMySQLDatabase)
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 1b55afd041..67380d8c4a 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -68,6 +68,8 @@ const (
SchemaRuleTableCommentConvention SQLReviewRuleType = "table.comment"
// SchemaRuleTableNotExists check the table name conflict.
SchemaRuleTableNotExists SQLReviewRuleType = "table.not-exists"
+ // SchemaRuleTableDisallowPartition disallow the table partition.
+ SchemaRuleTableDisallowPartition SQLReviewRuleType = "table.disallow-partition"
// SchemaRuleRequiredColumn enforce the required columns in each table.
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
@@ -723,6 +725,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
case db.MySQL, db.TiDB:
return MySQLTableCommentConvention, nil
}
+ case SchemaRuleTableDisallowPartition:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLTableDisallowPartition, nil
+ }
case SchemaRuleMySQLEngine:
if engine == db.MySQL {
return MySQLUseInnoDB, nil