Skip to content

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

feat(advisor): implement PG disallow-commit rule (#4140)

rebelice16d1892e445d
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 481f7520e8..da50a16aea 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -274,6 +274,9 @@ const (
// PostgreSQLColumnMaximumCharacterLength is an advisor type for PostgreSQL maximum character length.
PostgreSQLColumnMaximumCharacterLength Type = "bb.plugin.advisor.postgresql.column.maximum-character-length"
+
+ // PostgreSQLStatementDisallowCommit is an advisor type for PostgreSQL to disallow commit.
+ PostgreSQLStatementDisallowCommit Type = "bb.plugin.advisor.postgresql.statement.disallow-commit"
)
// Advice is the result of an advisor.
diff --git a/plugin/advisor/pg/advisor_statement_disallow_commit.go b/plugin/advisor/pg/advisor_statement_disallow_commit.go
new file mode 100644
index 0000000000..f438e8357c
--- /dev/null
+++ b/plugin/advisor/pg/advisor_statement_disallow_commit.go
@@ -0,0 +1,76 @@
+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 = (*StatementDisallowCommitAdvisor)(nil)
+ _ ast.Visitor = (*statementDisallowCommitChecker)(nil)
+)
+
+func init() {
+ advisor.Register(db.Postgres, advisor.PostgreSQLStatementDisallowCommit, &StatementDisallowCommitAdvisor{})
+}
+
+// StatementDisallowCommitAdvisor is the advisor checking for to disallow commit.
+type StatementDisallowCommitAdvisor struct {
+}
+
+// Check checks for to disallow commit.
+func (*StatementDisallowCommitAdvisor) 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 := &statementDisallowCommitChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ }
+
+ for _, stmt := range stmtList {
+ 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 statementDisallowCommitChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+}
+
+// Visit implements ast.Visitor interface.
+func (checker *statementDisallowCommitChecker) Visit(in ast.Node) ast.Visitor {
+ if _, ok := in.(*ast.CommitStmt); ok {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: checker.level,
+ Code: advisor.StatementDisallowCommit,
+ Title: checker.title,
+ Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", in.Text()),
+ Line: in.LastLine(),
+ })
+ }
+
+ return checker
+}
diff --git a/plugin/advisor/pg/pg_rules_test.go b/plugin/advisor/pg/pg_rules_test.go
index 1b78968a71..1d0953c9c0 100644
--- a/plugin/advisor/pg/pg_rules_test.go
+++ b/plugin/advisor/pg/pg_rules_test.go
@@ -33,6 +33,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleTableDisallowPartition,
advisor.SchemaRuleIndexPrimaryKeyTypeAllowlist,
advisor.SchemaRuleColumnMaximumCharacterLength,
+ advisor.SchemaRuleStatementDisallowCommit,
}
for _, rule := range pgRules {
diff --git a/plugin/advisor/pg/test/statement_disallow_commit.yaml b/plugin/advisor/pg/test/statement_disallow_commit.yaml
new file mode 100644
index 0000000000..0293cbec31
--- /dev/null
+++ b/plugin/advisor/pg/test/statement_disallow_commit.yaml
@@ -0,0 +1,29 @@
+- statement: |-
+ START TRANSACTION;
+ CREATE TABLE techBook(id int, name varchar(255));
+ COMMIT;
+ want:
+ - status: WARN
+ code: 206
+ title: statement.disallow-commit
+ content: 'Commit is not allowed, related statement: "COMMIT;"'
+ line: 3
+- statement: |-
+ START TRANSACTION;
+ CREATE TABLE techBook(id int, name varchar(255));
+ COMMIT;
+
+ START TRANSACTION;
+ INSERT INTO techBook(id, name) VALUES(1, "book");
+ COMMIT;
+ want:
+ - status: WARN
+ code: 206
+ title: statement.disallow-commit
+ content: 'Commit is not allowed, related statement: "COMMIT;"'
+ line: 3
+ - status: WARN
+ code: 206
+ title: statement.disallow-commit
+ content: 'Commit is not allowed, related statement: "COMMIT;"'
+ line: 7
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 369e5f54e5..cfca247966 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -844,6 +844,8 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine db.Type) (Type, err
switch engine {
case db.MySQL, db.TiDB:
return MySQLStatementDisallowCommit, nil
+ case db.Postgres:
+ return PostgreSQLStatementDisallowCommit, nil
}
case SchemaRuleCharsetAllowlist:
switch engine {
diff --git a/plugin/parser/ast/commit_stmt.go b/plugin/parser/ast/commit_stmt.go
new file mode 100644
index 0000000000..5a0d2b0a96
--- /dev/null
+++ b/plugin/parser/ast/commit_stmt.go
@@ -0,0 +1,6 @@
+package ast
+
+// CommitStmt is the struct for commit statement.
+type CommitStmt struct {
+ node
+}
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index e95d74c1ea..ffa79ee317 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -751,6 +751,10 @@ func convert(node *pgquery.Node, statement parser.SingleSQL) (res ast.Node, err
}, nil
}
// TODO(rebelice): support RENAME ENUM VALUE statements
+ case *pgquery.Node_TransactionStmt:
+ if in.TransactionStmt.Kind == pgquery.TransactionStmtKind_TRANS_STMT_COMMIT {
+ return &ast.CommitStmt{}, nil
+ }
default:
return &ast.UnconvertedStmt{}, nil
}
diff --git a/plugin/parser/engine/pg/convert_test.go b/plugin/parser/engine/pg/convert_test.go
index 747758b38d..f53df5773b 100644
--- a/plugin/parser/engine/pg/convert_test.go
+++ b/plugin/parser/engine/pg/convert_test.go
@@ -3098,3 +3098,22 @@ func TestAttachPartition(t *testing.T) {
runTests(t, tests)
}
+
+func TestCommit(t *testing.T) {
+ tests := []testData{
+ {
+ stmt: `COMMIT;`,
+ want: []ast.Node{
+ &ast.CommitStmt{},
+ },
+ statementList: []parser.SingleSQL{
+ {
+ Text: `COMMIT;`,
+ LastLine: 1,
+ },
+ },
+ },
+ }
+
+ runTests(t, tests)
+}