Skip to content

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

chore(sql-review): add check set role variable rule for PostgreSQL (#10914)

boojack479b55265843
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 973117a00d..16974961be 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -367,6 +367,9 @@ const (
// PostgreSQLStatementCreateSpecifySchema is an advisor type for PostgreSQL to specify schema when creating.
PostgreSQLStatementCreateSpecifySchema Type = "bb.plugin.advisor.postgresql.statement.create-specify-schema"
+ // PostgreSQLStatementCheckSetRoleVariable is an advisor type for PostgreSQL to check set role variable.
+ PostgreSQLStatementCheckSetRoleVariable Type = "bb.plugin.advisor.postgresql.statement.check-set-role-variable"
+
// Oracle Advisor.
// OracleSyntax is an advisor type for Oracle syntax.
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index e9455ae2e5..df0bed912f 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -41,6 +41,7 @@ const (
StatementCheckSelectFullTableScanFailed Code = 214
StatementHasTableFullScan Code = 215
StatementCreateWithoutSchemaName Code = 216
+ StatementCheckSetRoleVariable Code = 217
// 301 ~ 399 naming error code
// 301 table naming advisor error code.
diff --git a/backend/plugin/advisor/config/sql-review.dev.yaml b/backend/plugin/advisor/config/sql-review.dev.yaml
index 39977cd9a9..312292d2be 100644
--- a/backend/plugin/advisor/config/sql-review.dev.yaml
+++ b/backend/plugin/advisor/config/sql-review.dev.yaml
@@ -61,6 +61,8 @@ ruleList:
level: WARNING
- type: statement.create-specify-schema
level: WARNING
+ - type: statement.check-set-role-variable
+ level: WARNING
- type: naming.table
level: WARNING
payload:
diff --git a/backend/plugin/advisor/config/sql-review.prod.yaml b/backend/plugin/advisor/config/sql-review.prod.yaml
index 0f20abe3a0..52692fc51e 100644
--- a/backend/plugin/advisor/config/sql-review.prod.yaml
+++ b/backend/plugin/advisor/config/sql-review.prod.yaml
@@ -61,6 +61,8 @@ ruleList:
level: ERROR
- type: statement.create-specify-schema
level: WARNING
+ - type: statement.check-set-role-variable
+ level: WARNING
- type: naming.table
level: WARNING
payload:
diff --git a/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go b/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go
new file mode 100644
index 0000000000..c2ccbc9840
--- /dev/null
+++ b/backend/plugin/advisor/pg/advisor_statement_check_set_role_variable.go
@@ -0,0 +1,64 @@
+package pg
+
+import (
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/parser/sql/ast"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+var (
+ _ advisor.Advisor = (*StatementCheckSetRoleVariable)(nil)
+)
+
+func init() {
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLStatementCheckSetRoleVariable, &StatementCheckSetRoleVariable{})
+}
+
+type StatementCheckSetRoleVariable struct {
+}
+
+func (*StatementCheckSetRoleVariable) Check(ctx advisor.Context, _ string) ([]advisor.Advice, error) {
+ stmts, ok := ctx.AST.([]ast.Node)
+ if !ok {
+ return nil, errors.Errorf("failed to convert to Node")
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+
+ variableSetStmts := []*ast.VariableSetStmt{}
+ for _, stmt := range stmts {
+ if n, ok := stmt.(*ast.VariableSetStmt); ok {
+ variableSetStmts = append(variableSetStmts, n)
+ } else {
+ break
+ }
+ }
+ hasSetRole := false
+ for _, stmt := range variableSetStmts {
+ if stmt.Name == "role" {
+ hasSetRole = true
+ }
+ }
+
+ if !hasSetRole {
+ return []advisor.Advice{{
+ Status: level,
+ Code: advisor.StatementCheckSetRoleVariable,
+ Title: string(ctx.Rule.Type),
+ Content: "No SET ROLE statement found.",
+ Line: 1,
+ }}, nil
+ }
+
+ return []advisor.Advice{{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ }}, nil
+}
diff --git a/backend/plugin/advisor/pg/pg_rules_test.go b/backend/plugin/advisor/pg/pg_rules_test.go
index 8ab7197ec4..07fffb6f97 100644
--- a/backend/plugin/advisor/pg/pg_rules_test.go
+++ b/backend/plugin/advisor/pg/pg_rules_test.go
@@ -49,6 +49,7 @@ func TestPostgreSQLRules(t *testing.T) {
advisor.SchemaRuleStatementAddCheckNotValid,
advisor.SchemaRuleStatementDisallowAddNotNull,
advisor.SchemaRuleStatementCreateSpecifySchema,
+ advisor.SchemaRuleStatementCheckSetRoleVariable,
}
for _, rule := range pgRules {
diff --git a/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml b/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml
new file mode 100644
index 0000000000..9974873611
--- /dev/null
+++ b/backend/plugin/advisor/pg/test/statement_check_set_role_variable.yaml
@@ -0,0 +1,11 @@
+- statement: SET SESSION ROLE joe; SELECT * FROM t
+ want:
+ - status: SUCCESS
+ title: OK
+- statement: SELECT * FROM t
+ want:
+ - status: WARN
+ code: 217
+ title: statement.check-set-role-variable
+ content: No SET ROLE statement found.
+ line: 1
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index cf25cdfd41..678923619e 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -99,6 +99,8 @@ const (
SchemaRuleStatementSelectFullTableScan = "statement.select-full-table-scan"
// SchemaRuleStatementCreateSpecifySchema disallow to create table without specifying schema.
SchemaRuleStatementCreateSpecifySchema = "statement.create-specify-schema"
+ // SchemaRuleStatementCheckSetRoleVariable require add a check for SET ROLE variable.
+ SchemaRuleStatementCheckSetRoleVariable = "statement.check-set-role-variable"
// SchemaRuleTableRequirePK require the table to have a primary key.
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
@@ -1466,6 +1468,10 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
if engine == storepb.Engine_POSTGRES {
return PostgreSQLStatementCreateSpecifySchema, nil
}
+ case SchemaRuleStatementCheckSetRoleVariable:
+ if engine == storepb.Engine_POSTGRES {
+ return PostgreSQLStatementCheckSetRoleVariable, nil
+ }
case SchemaRuleCommentLength:
if engine == storepb.Engine_POSTGRES {
return PostgreSQLCommentConvention, nil
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index cd74fc828c..f275e8bea0 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -359,7 +359,8 @@ func SetDefaultSQLReviewRulePayload(ruleTp SQLReviewRuleType, dbType storepb.Eng
SchemaRuleEventDisallowCreate,
SchemaRuleViewDisallowCreate,
SchemaRuleFunctionDisallowCreate,
- SchemaRuleStatementCreateSpecifySchema:
+ SchemaRuleStatementCreateSpecifySchema,
+ SchemaRuleStatementCheckSetRoleVariable:
case SchemaRuleTableDropNamingConvention:
payload, err = json.Marshal(NamingRulePayload{
Format: "_delete$",