Skip to content

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

feat(advisor): implement PG disallow-partition rule (#4135)

rebeliced37e64017773
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index db922c5e91..159286a75a 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -248,6 +248,9 @@ const (
// PostgreSQLTableNoFK is an advisor type for PostgreSQL table disallow foreign key.
PostgreSQLTableNoFK Type = "bb.plugin.advisor.postgresql.table.no-foreign-key"
+ // PostgreSQLTableDisallowPartition is an advisor type for PostgreSQL disallow table partition.
+ PostgreSQLTableDisallowPartition Type = "bb.plugin.advisor.postgresql.table.disallow-partition"
+
// PostgreSQLInsertRowLimit is an advisor type for PostgreSQL to limit INSERT rows.
PostgreSQLInsertRowLimit Type = "bb.plugin.advisor.postgresql.insert.row-limit"
diff --git a/plugin/advisor/pg/advisor_table_disallow_partition.go b/plugin/advisor/pg/advisor_table_disallow_partition.go
new file mode 100644
index 0000000000..d85875af32
--- /dev/null
+++ b/plugin/advisor/pg/advisor_table_disallow_partition.go
@@ -0,0 +1,91 @@
+package pg
+
+// Framework code is generated by the generator.
+
+import (
+ "fmt"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/bytebase/bytebase/plugin/advisor/db"
+ "github.com/bytebase/bytebase/plugin/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*TableDisallowPartitionAdvisor)(nil)
+ _ ast.Visitor = (*tableDisallowPartitionChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLTableDisallowPartition, &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)
+ 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.LastLine()
+ ast.Walk(checker, stmt)
+ }
+
+ 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
+}
+
+// Visit implements ast.Visitor interface.
+func (checker *tableDisallowPartitionChecker) Visit(in ast.Node) ast.Visitor {
+ code := advisor.Ok
+
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ if node.PartitionDef != nil {
+ code = advisor.CreateTablePartition
+ }
+ case *ast.AttachPartitionStmt:
+ 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.line,
+ })
+ }
+
+ return checker
+}
diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.go
index c40d675795..3ce5862a1b 100644
--- a/plugin/advisor/pg/pg_rules_test.go
+++ b/plugin/advisor/pg/pg_rules_test.go
@@ -30,6 +30,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleTableNoFK,
advisor.SchemaRuleTableRequirePK,
advisor.SchemaRuleColumnDisallowChangeType,
+ advisor.SchemaRuleTableDisallowPartition,
}
for _, rule := range pgRules {
diff --git a/plugin/advisor/pg/test/table_disallow_partition.yaml b/plugin/advisor/pg/test/table_disallow_partition.yaml
new file mode 100644
index 0000000000..6ffb12f043
--- /dev/null
+++ b/plugin/advisor/pg/test/table_disallow_partition.yaml
@@ -0,0 +1,21 @@
+- statement: CREATE TABLE t(a int)
+ want:
+ - status: SUCCESS
+ code: 0
+ title: OK
+ content: ""
+ line: 0
+- statement: CREATE TABLE t(a int) PARTITION BY RANGE (a);
+ want:
+ - status: WARN
+ code: 608
+ title: table.disallow-partition
+ content: Table partition is forbidden, but "CREATE TABLE t(a int) PARTITION BY RANGE (a);" creates
+ line: 1
+- statement: ALTER TABLE tech_book ATTACH PARTITION p1 DEFAULT;
+ want:
+ - status: WARN
+ code: 608
+ title: table.disallow-partition
+ content: Table partition is forbidden, but "ALTER TABLE tech_book ATTACH PARTITION p1 DEFAULT;" creates
+ line: 1
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 6babdb49f1..ac8e82adf8 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -805,6 +805,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
switch engine {
case db.MySQL, db.TiDB:
return MySQLTableDisallowPartition, nil
+ case db.Postgres:
+ return PostgreSQLTableDisallowPartition, nil
}
case SchemaRuleMySQLEngine:
if engine == db.MySQL {
diff --git a/plugin/parser/ast/attach_partition_stmt.go b/plugin/parser/ast/attach_partition_stmt.go
new file mode 100644
index 0000000000..aa92ca261e
--- /dev/null
+++ b/plugin/parser/ast/attach_partition_stmt.go
@@ -0,0 +1,9 @@
+package ast
+
+// AttachPartitionStmt is the attach partition statement.
+type AttachPartitionStmt struct {
+ node
+
+ Table *TableDef
+ // TODO(rebelice): convert more attach partition statement fields.
+}
diff --git a/plugin/parser/ast/create_table_stmt.go b/plugin/parser/ast/create_table_stmt.go
index c9f32ea5d9..4a8839cca4 100644
--- a/plugin/parser/ast/create_table_stmt.go
+++ b/plugin/parser/ast/create_table_stmt.go
@@ -1,6 +1,6 @@
package ast
-// CreateTableStmt is the strcut for create table stmt.
+// CreateTableStmt is the struct for create table stmt.
type CreateTableStmt struct {
ddl
@@ -8,4 +8,7 @@ type CreateTableStmt struct {
Name *TableDef
ColumnList []*ColumnDef
ConstraintList []*ConstraintDef
+
+ // TODO(rebelice): convert the partition definition.
+ PartitionDef Node
}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 01d23ecf75..e95d74c1ea 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -140,6 +140,10 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err
alterTable.AlterItemList = append(alterTable.AlterItemList, setDefault)
}
+ case pgquery.AlterTableType_AT_AttachPartition:
+ alterTable.AlterItemList = append(alterTable.AlterItemList, &ast.AttachPartitionStmt{
+ Table: alterTable.Table,
+ })
}
}
}
@@ -1123,6 +1127,11 @@ func convertCreateStmt(in *pgquery.CreateStmt) (*ast.CreateTableStmt, error) {
table.ConstraintList = append(table.ConstraintList, cons)
}
}
+
+ if in.Partspec != nil {
+ // TODO(rebelice): convert the partition definition.
+ table.PartitionDef = &ast.UnconvertedStmt{}
+ }
return table, nil
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 029edc2c80..747758b38d 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -56,6 +56,48 @@ func newUnconvertedDataType(name []string, text string) *ast.UnconvertedDataType
func TestPGConvertCreateTableStmt(t *testing.T) {
tests := []testData{
+ {
+ stmt: `
+ CREATE TABLE tech_book(
+ a int,
+ b int
+ )
+ PARTITION BY RANGE (a)
+ `,
+ want: []ast.Node{
+ &ast.CreateTableStmt{
+ IfNotExists: false,
+ Name: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ ColumnList: []*ast.ColumnDef{
+ {
+ ColumnName: "a",
+ Type: &ast.Integer{Size: 4},
+ },
+ {
+ ColumnName: "b",
+ Type: &ast.Integer{Size: 4},
+ },
+ },
+ PartitionDef: &ast.UnconvertedStmt{},
+ },
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `CREATE TABLE tech_book(
+ a int,
+ b int
+ )
+ PARTITION BY RANGE (a)`,
+ LastLine: 6,
+ },
+ },
+ columnLine: [][]int{
+ {3, 4},
+ },
+ },
{
stmt: `
CREATE TABLE tech_book(
@@ -3024,3 +3066,35 @@ func TestAlterType(t *testing.T) {
runTests(t, tests)
}
+
+func TestAttachPartition(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `ALTER TABLE tech_book ATTACH PARTITION p1 DEFAULT`,
+ want: []ast.Node{
+ &ast.AlterTableStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ AlterItemList: []ast.Node{
+ &ast.AttachPartitionStmt{
+ Table: &ast.TableDef{
+ Type: ast.TableTypeBaseTable,
+ Name: "tech_book",
+ },
+ },
+ },
+ },
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `ALTER TABLE tech_book ATTACH PARTITION p1 DEFAULT`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}