Skip to content

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

refactor(schema-system): add SchemaReviewCheck to check statements with policy (#1537)

rebeliceb61c7db6b44b
diff --git a/plugin/advisor/schema_review.go b/plugin/advisor/schema_review.go
index 4b901efc08..a8ea2e8cfb 100644
--- a/plugin/advisor/schema_review.go
+++ b/plugin/advisor/schema_review.go
@@ -1,13 +1,23 @@
package advisor
import (
+ "context"
"encoding/json"
"fmt"
"regexp"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/common/log"
+ "github.com/bytebase/bytebase/plugin/catalog"
+ "github.com/bytebase/bytebase/plugin/db"
+ "go.uber.org/zap"
)
+// How to add a schema review rule:
+// 1. Implement an advisor.(plugin/advisor/mysql or plugin/advisor/pg)
+// 2. Register this advisor in map[db.Type][AdvisorType].(plugin/advisor.go)
+// 3. Map SchemaReviewRuleType to advisor.Type in getAdvisorTypeByRule(current file).
+
// SchemaReviewRuleLevel is the error level for schema review rule.
type SchemaReviewRuleLevel string
@@ -191,3 +201,128 @@ func UnmarshalRequiredColumnRulePayload(payload string) (*RequiredColumnRulePayl
}
return &rcr, nil
}
+
+// SchemaReviewCheckContext is the context for schema review check.
+type SchemaReviewCheckContext struct {
+ Charset string
+ Collation string
+ DbType db.Type
+ Catalog catalog.Catalog
+}
+
+// SchemaReviewCheck checks the statments with schema review policy.
+func SchemaReviewCheck(ctx context.Context, statements string, policy *SchemaReviewPolicy, context SchemaReviewCheckContext) ([]Advice, error) {
+ var result []Advice
+ for _, rule := range policy.RuleList {
+ if rule.Level == SchemaRuleLevelDisabled {
+ continue
+ }
+
+ advisorType, err := getAdvisorTypeByRule(rule.Type, context.DbType)
+ if err != nil {
+ log.Debug("not supported rule", zap.Error(err))
+ continue
+ }
+
+ adviceList, err := Check(
+ context.DbType,
+ advisorType,
+ Context{
+ Charset: context.Charset,
+ Collation: context.Collation,
+ Rule: rule,
+ Catalog: context.Catalog,
+ },
+ statements,
+ )
+ if err != nil {
+ return nil, common.Errorf(common.Internal, fmt.Errorf("failed to check statement: %w", err))
+ }
+
+ result = append(result, adviceList...)
+ }
+
+ // There may be multiple syntax errors, return one only.
+ if len(result) > 0 && result[0].Title == SyntaxErrorTitle {
+ return result[:1], nil
+ }
+ if len(result) == 0 {
+ result = append(result, Advice{
+ Status: Success,
+ Code: common.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return result, nil
+}
+
+func getAdvisorTypeByRule(ruleType SchemaReviewRuleType, engine db.Type) (Type, error) {
+ switch ruleType {
+ case SchemaRuleStatementRequireWhere:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLWhereRequirement, nil
+ }
+ case SchemaRuleStatementNoLeadingWildcardLike:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNoLeadingWildcardLike, nil
+ }
+ case SchemaRuleStatementNoSelectAll:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNoSelectAll, nil
+ }
+ case SchemaRuleSchemaBackwardCompatibility:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLMigrationCompatibility, nil
+ }
+ case SchemaRuleTableNaming:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNamingTableConvention, nil
+ }
+ case SchemaRuleIDXNaming:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNamingIndexConvention, nil
+ }
+ case SchemaRuleUKNaming:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNamingUKConvention, nil
+ }
+ case SchemaRuleFKNaming:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNamingFKConvention, nil
+ }
+ case SchemaRuleColumnNaming:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLNamingColumnConvention, nil
+ }
+ case SchemaRuleRequiredColumn:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLColumnRequirement, nil
+ }
+ case SchemaRuleColumnNotNull:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLColumnNoNull, nil
+ }
+ case SchemaRuleTableRequirePK:
+ switch engine {
+ case db.MySQL, db.TiDB:
+ return MySQLTableRequirePK, nil
+ }
+ case SchemaRuleMySQLEngine:
+ if engine == db.MySQL {
+ return MySQLUseInnoDB, nil
+ }
+ }
+ return Fake, fmt.Errorf("unknown schema review rule type %v for %v", ruleType, engine)
+}
diff --git a/server/task_check_executor_statement_advisor_composite.go b/server/task_check_executor_statement_advisor_composite.go
index 7a28728cc9..aefb15f170 100644
--- a/server/task_check_executor_statement_advisor_composite.go
+++ b/server/task_check_executor_statement_advisor_composite.go
@@ -7,11 +7,8 @@ import (
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
- "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/advisor"
- "github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/store"
- "go.uber.org/zap"
)
// Schema review policy consists of a list of schema review rules.
@@ -19,11 +16,6 @@ import (
// 1. One schema review policy maps a TaskCheckRun.
// 2. Each schema review rule type maps an advisor.Type.
// 3. Each [db.Type][AdvisorType] maps an advisor.
-//
-// How to add a schema review rule:
-// 1. Implement an advisor.(plugin/xxx)
-// 2. Register this advisor in map[db.Type][AdvisorType].(plugin/advisor.go)
-// 3. Map SchemaReviewRuleType to advisor.Type in getAdvisorTypeByRule(current file).
// NewTaskCheckStatementAdvisorCompositeExecutor creates a task check statement advisor composite executor.
func NewTaskCheckStatementAdvisorCompositeExecutor() TaskCheckExecutor {
@@ -68,55 +60,38 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
return nil, common.Errorf(common.Internal, fmt.Errorf("failed to get task by id: %w", err))
}
+ catalog := store.NewCatalog(task.DatabaseID, server.store)
+
+ adviceList, err := advisor.SchemaReviewCheck(ctx, payload.Statement, policy, advisor.SchemaReviewCheckContext{
+ Charset: payload.Charset,
+ Collation: payload.Collation,
+ DbType: payload.DbType,
+ Catalog: catalog,
+ })
+ if err != nil {
+ return nil, err
+ }
+
result = []api.TaskCheckResult{}
- for _, rule := range policy.RuleList {
- if rule.Level == advisor.SchemaRuleLevelDisabled {
+ for _, advice := range adviceList {
+ status := api.TaskCheckStatusSuccess
+ switch advice.Status {
+ case advisor.Success:
continue
+ case advisor.Warn:
+ status = api.TaskCheckStatusWarn
+ case advisor.Error:
+ status = api.TaskCheckStatusError
}
- advisorType, err := getAdvisorTypeByRule(rule.Type, payload.DbType)
- if err != nil {
- log.Debug("not supported rule", zap.Error(err))
- continue
- }
- adviceList, err := advisor.Check(
- payload.DbType,
- advisorType,
- advisor.Context{
- Charset: payload.Charset,
- Collation: payload.Collation,
- Rule: rule,
- Catalog: store.NewCatalog(task.DatabaseID, server.store),
- },
- payload.Statement,
- )
- if err != nil {
- return nil, common.Errorf(common.Internal, fmt.Errorf("failed to check statement: %w", err))
- }
-
- for _, advice := range adviceList {
- status := api.TaskCheckStatusSuccess
- switch advice.Status {
- case advisor.Success:
- continue
- case advisor.Warn:
- status = api.TaskCheckStatusWarn
- case advisor.Error:
- status = api.TaskCheckStatusError
- }
- result = append(result, api.TaskCheckResult{
- Status: status,
- Code: advice.Code,
- Title: advice.Title,
- Content: advice.Content,
- })
-
- }
- }
- // There may be multiple syntax errors, return one only.
- if len(result) > 0 && result[0].Title == advisor.SyntaxErrorTitle {
- return result[:1], nil
+ result = append(result, api.TaskCheckResult{
+ Status: status,
+ Code: advice.Code,
+ Title: advice.Title,
+ Content: advice.Content,
+ })
}
+
if len(result) == 0 {
result = append(result, api.TaskCheckResult{
Status: api.TaskCheckStatusSuccess,
@@ -125,76 +100,6 @@ func (exec *TaskCheckStatementAdvisorCompositeExecutor) Run(ctx context.Context,
Content: "",
})
}
- return result, nil
-
-}
-func getAdvisorTypeByRule(ruleType advisor.SchemaReviewRuleType, engine db.Type) (advisor.Type, error) {
- switch ruleType {
- case advisor.SchemaRuleStatementRequireWhere:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLWhereRequirement, nil
- }
- case advisor.SchemaRuleStatementNoLeadingWildcardLike:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNoLeadingWildcardLike, nil
- }
- case advisor.SchemaRuleStatementNoSelectAll:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNoSelectAll, nil
- }
- case advisor.SchemaRuleSchemaBackwardCompatibility:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLMigrationCompatibility, nil
- }
- case advisor.SchemaRuleTableNaming:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNamingTableConvention, nil
- }
- case advisor.SchemaRuleIDXNaming:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNamingIndexConvention, nil
- }
- case advisor.SchemaRuleUKNaming:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNamingUKConvention, nil
- }
- case advisor.SchemaRuleFKNaming:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNamingFKConvention, nil
- }
- case advisor.SchemaRuleColumnNaming:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLNamingColumnConvention, nil
- }
- case advisor.SchemaRuleRequiredColumn:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLColumnRequirement, nil
- }
- case advisor.SchemaRuleColumnNotNull:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLColumnNoNull, nil
- }
- case advisor.SchemaRuleTableRequirePK:
- switch engine {
- case db.MySQL, db.TiDB:
- return advisor.MySQLTableRequirePK, nil
- }
- case advisor.SchemaRuleMySQLEngine:
- if engine == db.MySQL {
- return advisor.MySQLUseInnoDB, nil
- }
- }
- return advisor.Fake, fmt.Errorf("unknown schema review rule type %v for %v", ruleType, engine)
+ return result, nil
}