Skip to content

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

chore: introduce builtin sql review rules (#13361)

rebelice9c082a4e60f6
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 7ca09012f0..26f8e7ecd7 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1333,9 +1333,11 @@ func (s *SQLService) sqlCheck(
reviewConfig, err := s.store.GetReviewConfigForDatabase(ctx, database)
if err != nil {
if e, ok := err.(*common.Error); ok && e.Code == common.NotFound {
- return storepb.Advice_SUCCESS, nil, nil
+ // Continue to check the builtin rules.
+ reviewConfig = &storepb.ReviewConfigPayload{}
+ } else {
+ return storepb.Advice_ERROR, nil, status.Errorf(codes.Internal, "failed to get SQL review policy with error: %v", err)
}
- return storepb.Advice_ERROR, nil, status.Errorf(codes.Internal, "failed to get SQL review policy with error: %v", err)
}
res, err := advisor.SQLReviewCheck(s.sheetManager, statement, reviewConfig.SqlReviewRules, context)
diff --git a/backend/plugin/advisor/builtin_rules.go b/backend/plugin/advisor/builtin_rules.go
new file mode 100644
index 0000000000..758a8d6ab0
--- /dev/null
+++ b/backend/plugin/advisor/builtin_rules.go
@@ -0,0 +1,24 @@
+package advisor
+
+import storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+
+const (
+ BuiltinRulePriorBackupCheck SQLReviewRuleType = "builtin.prior-backup-check"
+)
+
+func GetBuiltinRules(engine storepb.Engine) []*storepb.SQLReviewRule {
+ switch engine {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_POSTGRES:
+ return []*storepb.SQLReviewRule{
+ {
+ Type: string(BuiltinRulePriorBackupCheck),
+ Level: storepb.SQLReviewRuleLevel_ERROR,
+ Payload: "",
+ Engine: engine,
+ Comment: "",
+ },
+ }
+ default:
+ return nil
+ }
+}
diff --git a/backend/plugin/advisor/code.go b/backend/plugin/advisor/code.go
index 44829287cf..1dc7c5a95b 100644
--- a/backend/plugin/advisor/code.go
+++ b/backend/plugin/advisor/code.go
@@ -52,7 +52,7 @@ const (
StatementWhereMaximumLogicalOperatorCount Code = 225
StatementJoinColumnAttrsNotMatch Code = 226
StatementDisallowMixDDLDML Code = 227
- StatementPriorBackupCheck Code = 228
+ // 228 is deprecated.
StatementAddFKWithValidation Code = 229
StatementNonTransactional Code = 230
StatementAddColumnWithPosition Code = 231
@@ -196,6 +196,9 @@ const (
// 1901 ~ 1999 schema error code.
SchemaNotExists Code = 1901
+
+ // 2001 ~ 2099 builtin error code.
+ BuiltinPriorBackupCheck Code = 2001
)
// Int returns the int type of code.
@@ -434,8 +437,8 @@ const (
// MySQLStatementDisallowMixDDLDML is an advisor type for MySQL disallow mix DDL and DML.
MySQLStatementDisallowMixDDLDML Type = "bb.plugin.advisor.mysql.statement.disallow-mix-ddl-dml"
- // MySQLStatementPriorBackupCheck is an advisor type for MySQL prior backup check.
- MySQLStatementPriorBackupCheck Type = "bb.plugin.advisor.mysql.statement.prior-backup-check"
+ // MySQLBuiltinPriorBackupCheck is an advisor type for MySQL prior backup check.
+ MySQLBuiltinPriorBackupCheck Type = "bb.plugin.advisor.mysql.builtin.prior-backup-check"
// MySQLStatementAddColumnWithoutPosition is an advisor type for MySQL checking no position in ADD COLUMN clause.
MySQLStatementAddColumnWithoutPosition Type = "bb.plugin.advisor.mysql.statement.add-column-without-position"
@@ -601,8 +604,8 @@ const (
// PostgreSQLStatementDisallowMixDDLDML is an advisor type for PostgreSQL disallow mix DDL and DML.
PostgreSQLStatementDisallowMixDDLDML Type = "bb.plugin.advisor.postgresql.statement.disallow-mix-ddl-dml"
- // PostgreSQLStatementPriorBackupCheck is an advisor type for PostgreSQL do prior backup check.
- PostgreSQLStatementPriorBackupCheck Type = "bb.plugin.advisor.postgresql.statement.prior-backup-check"
+ // PostgreSQLBuiltinPriorBackupCheck is an advisor type for PostgreSQL do prior backup check.
+ PostgreSQLBuiltinPriorBackupCheck Type = "bb.plugin.advisor.postgresql.builtin.prior-backup-check"
// PostgreSQLStatementMaximumLimitValue is an advisor type for PostgreSQL statement maximum limit value.
PostgreSQLStatementMaximumLimitValue Type = "bb.plugin.advisor.postgresql.statement.maximum-limit-value"
diff --git a/backend/plugin/advisor/mysql/advisor_statement_prior_backup_check.go b/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go
similarity index 97%
rename from backend/plugin/advisor/mysql/advisor_statement_prior_backup_check.go
rename to backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go
index 5b64412bb1..b72e42069a 100644
--- a/backend/plugin/advisor/mysql/advisor_statement_prior_backup_check.go
+++ b/backend/plugin/advisor/mysql/advisor_builtin_prior_backup_check.go
@@ -20,7 +20,7 @@ var (
)
func init() {
- advisor.Register(storepb.Engine_MYSQL, advisor.MySQLStatementPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
+ advisor.Register(storepb.Engine_MYSQL, advisor.MySQLBuiltinPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
}
type StatementPriorBackupCheckAdvisor struct {
@@ -52,7 +52,7 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
Status: level,
Title: title,
Content: "Prior backup cannot deal with mixed DDL and DML statements",
- Code: advisor.StatementPriorBackupCheck.Int32(),
+ Code: advisor.BuiltinPriorBackupCheck.Int32(),
StartPosition: &storepb.Position{
Line: int32(stmt.BaseLine),
},
@@ -96,7 +96,7 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
content += fmt.Sprintf(" on table `%s`.`%s`, disallow mixing different types of DML statements", table.database, table.table)
adviceList = append(adviceList, &storepb.Advice{
Status: checker.level,
- Code: advisor.StatementPriorBackupCheck.Int32(),
+ Code: advisor.BuiltinPriorBackupCheck.Int32(),
Title: checker.title,
Content: content,
})
diff --git a/backend/plugin/advisor/pg/advisor_statement_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
similarity index 91%
rename from backend/plugin/advisor/pg/advisor_statement_prior_backup_check.go
rename to backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
index 0e2ba4164f..82ec69074f 100644
--- a/backend/plugin/advisor/pg/advisor_statement_prior_backup_check.go
+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go
@@ -18,7 +18,7 @@ var (
)
func init() {
- advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLStatementPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
+ advisor.Register(storepb.Engine_POSTGRES, advisor.PostgreSQLBuiltinPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
}
// StatementPriorBackupCheckAdvisor is the advisor checking for disallow mix DDL and DML.
@@ -52,7 +52,7 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
Status: level,
Title: title,
Content: fmt.Sprintf("Data change can only run DML, \"%s\" is not DML", stmt.Text()),
- Code: advisor.StatementPriorBackupCheck.Int32(),
+ Code: advisor.BuiltinPriorBackupCheck.Int32(),
StartPosition: &storepb.Position{
Line: int32(stmt.LastLine()),
},
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index e0676a340d..c42bc46151 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -494,15 +494,23 @@ func SQLReviewCheck(
checkContext SQLReviewCheckContext,
) ([]*storepb.Advice, error) {
asts, parseResult := sm.GetASTsForChecks(checkContext.DbType, statements)
+
+ builtinOnly := len(ruleList) == 0
+
+ // Append builtin rules to the rule list.
+ ruleList = append(ruleList, GetBuiltinRules(checkContext.DbType)...)
+
if asts == nil || len(ruleList) == 0 {
return parseResult, nil
}
finder := checkContext.Catalog.GetFinder()
- switch checkContext.DbType {
- case storepb.Engine_TIDB, storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_POSTGRES, storepb.Engine_OCEANBASE:
- if err := finder.WalkThrough(asts); err != nil {
- return convertWalkThroughErrorToAdvice(checkContext, err)
+ if !builtinOnly {
+ switch checkContext.DbType {
+ case storepb.Engine_TIDB, storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_POSTGRES, storepb.Engine_OCEANBASE:
+ if err := finder.WalkThrough(asts); err != nil {
+ return convertWalkThroughErrorToAdvice(checkContext, err)
+ }
}
}
@@ -1275,13 +1283,6 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
case storepb.Engine_MSSQL:
return MSSQLStatementDisallowMixDDLDML, nil
}
- case SchemaRuleStatementPriorBackupCheck:
- switch engine {
- case storepb.Engine_MYSQL, storepb.Engine_TIDB:
- return MySQLStatementPriorBackupCheck, nil
- case storepb.Engine_POSTGRES:
- return PostgreSQLStatementPriorBackupCheck, nil
- }
case SchemaRuleStatementAddColumnWithoutPosition:
if engine == storepb.Engine_OCEANBASE {
return MySQLStatementAddColumnWithoutPosition, nil
@@ -1497,6 +1498,14 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine storepb.Engine) (Ty
if engine == storepb.Engine_OCEANBASE {
return MySQLDisallowOfflineDDL, nil
}
+ // ----------------- Builtin Rules -----------------------
+ case BuiltinRulePriorBackupCheck:
+ switch engine {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB:
+ return MySQLBuiltinPriorBackupCheck, nil
+ case storepb.Engine_POSTGRES:
+ return PostgreSQLBuiltinPriorBackupCheck, nil
+ }
}
return "", errors.Errorf("unknown SQL review rule type %v for %v", ruleType, engine)
}
diff --git a/backend/plugin/advisor/tidb/advisor_statement_prior_backup_check.go b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
similarity index 96%
rename from backend/plugin/advisor/tidb/advisor_statement_prior_backup_check.go
rename to backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
index 5076010fc9..4db18d66e4 100644
--- a/backend/plugin/advisor/tidb/advisor_statement_prior_backup_check.go
+++ b/backend/plugin/advisor/tidb/advisor_builtin_prior_backup_check.go
@@ -18,7 +18,7 @@ var (
)
func init() {
- advisor.Register(storepb.Engine_TIDB, advisor.MySQLStatementPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
+ advisor.Register(storepb.Engine_TIDB, advisor.MySQLBuiltinPriorBackupCheck, &StatementPriorBackupCheckAdvisor{})
}
// StatementPriorBackupCheckAdvisor is the advisor checking for no mixed DDL and DML.
@@ -54,7 +54,7 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
Status: level,
Title: title,
Content: "Prior backup cannot deal with mixed DDL and DML statements",
- Code: advisor.StatementPriorBackupCheck.Int32(),
+ Code: advisor.BuiltinPriorBackupCheck.Int32(),
StartPosition: &storepb.Position{
Line: int32(stmtNode.OriginTextPosition()),
},
@@ -108,7 +108,7 @@ func (*StatementPriorBackupCheckAdvisor) Check(ctx advisor.Context, _ string) ([
content += fmt.Sprintf(" on table `%s`.`%s`, disallow mixing different types of DML statements", table.database, table.table)
adviceList = append(adviceList, &storepb.Advice{
Status: checker.level,
- Code: advisor.StatementPriorBackupCheck.Int32(),
+ Code: advisor.BuiltinPriorBackupCheck.Int32(),
Title: checker.title,
Content: content,
})
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 604d74376d..49eeeeb7a6 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -159,6 +159,7 @@ func (e *StatementAdviseExecutor) runReview(
reviewConfig, err := e.store.GetReviewConfigForDatabase(ctx, database)
if err != nil {
if e, ok := err.(*common.Error); ok && e.Code == common.NotFound {
+ // Continue to check the builtin rules.
reviewConfig = &storepb.ReviewConfigPayload{}
} else {
return nil, common.Wrapf(err, common.Internal, "failed to get SQL review config")
diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yaml
index 71559dcda9..8804d0de21 100644
--- a/frontend/src/types/sql-review-schema.yaml
+++ b/frontend/src/types/sql-review-schema.yaml
@@ -600,15 +600,6 @@
- type: statement.disallow-mix-ddl-dml
category: STATEMENT
engine: MSSQL
-- type: statement.prior-backup-check
- category: STATEMENT
- engine: MYSQL
-- type: statement.prior-backup-check
- category: STATEMENT
- engine: POSTGRES
-- type: statement.prior-backup-check
- category: STATEMENT
- engine: TIDB
- type: statement.add-column-without-position
category: STATEMENT
engine: OCEANBASE
diff --git a/frontend/src/types/sql-review.dev.yaml b/frontend/src/types/sql-review.dev.yaml
index 15eede75f4..5741a18858 100644
--- a/frontend/src/types/sql-review.dev.yaml
+++ b/frontend/src/types/sql-review.dev.yaml
@@ -474,15 +474,6 @@ ruleList:
- type: statement.disallow-mix-ddl-dml
level: WARNING
engine: MSSQL
- - type: statement.prior-backup-check
- level: ERROR
- engine: MYSQL
- - type: statement.prior-backup-check
- level: ERROR
- engine: POSTGRES
- - type: statement.prior-backup-check
- level: ERROR
- engine: TIDB
- type: statement.disallow-cross-db-queries
level: WARNING
engine: MSSQL
diff --git a/frontend/src/types/sql-review.prod.yaml b/frontend/src/types/sql-review.prod.yaml
index ed1fa2e1c3..e01975a9d4 100644
--- a/frontend/src/types/sql-review.prod.yaml
+++ b/frontend/src/types/sql-review.prod.yaml
@@ -485,15 +485,6 @@ ruleList:
- type: statement.disallow-mix-ddl-dml
level: WARNING
engine: MSSQL
- - type: statement.prior-backup-check
- level: ERROR
- engine: MYSQL
- - type: statement.prior-backup-check
- level: ERROR
- engine: POSTGRES
- - type: statement.prior-backup-check
- level: ERROR
- engine: TIDB
- type: statement.add-column-without-position
level: WARNING
engine: OCEANBASE