Skip to content

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

refactor: unify advisor Context by merging SQLReviewCheckContext (#18175)

Danny Xubcc4531bd053
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index 7db262cb3c..550e354fd0 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -663,7 +663,7 @@ func (s *ReleaseService) runSQLReviewCheckForFile(
connection := driver.GetDB()
classificationConfig := getClassificationByProject(ctx, s.store, database.ProjectID)
- context := advisor.SQLReviewCheckContext{
+ context := advisor.Context{
Charset: dbMetadataProto.CharacterSet,
Collation: dbMetadataProto.Collation,
ChangeType: changeType,
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 903be0efa0..837b9e9763 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -58,6 +58,17 @@ type Context struct {
Statements string
// UsePostgresDatabaseOwner is true if the advisor should use the database owner as default role.
UsePostgresDatabaseOwner bool
+
+ // SQL review level fields.
+ Charset string
+ Collation string
+ DBType storepb.Engine
+
+ // Snowflake specific fields (duplicates CurrentDatabase, kept for compatibility).
+ // CurrentDatabase string
+
+ // Used for test only.
+ NoAppendBuiltin bool
}
// Advisor is the interface for advisor.
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 1caacb8a8e..5d5d9de4d0 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -2,7 +2,6 @@ package advisor
import (
"context"
- "database/sql"
"encoding/json"
"regexp"
@@ -11,9 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/component/sheet"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
// Register walk-through implementations
_ "github.com/bytebase/bytebase/backend/plugin/schema/mysql"
@@ -489,39 +486,13 @@ func UnmarshalNamingCaseRulePayload(payload string) (*NamingCaseRulePayload, err
return &ncr, nil
}
-// SQLReviewCheckContext is the context for SQL review check.
-type SQLReviewCheckContext struct {
- Charset string
- Collation string
- ChangeType storepb.PlanCheckRunConfig_ChangeDatabaseType
- DBSchema *storepb.DatabaseSchemaMetadata
- DBType storepb.Engine
- OriginalMetadata *model.DatabaseMetadata
- FinalMetadata *model.DatabaseMetadata
- Driver *sql.DB
- EnablePriorBackup bool
- ClassificationConfig *storepb.DataClassificationSetting_DataClassificationConfig
- ListDatabaseNamesFunc base.ListDatabaseNamesFunc
- InstanceID string
- IsObjectCaseSensitive bool
-
- // Snowflake specific fields
- CurrentDatabase string
-
- // Used for test only.
- NoAppendBuiltin bool
-
- // UsePostgresDatabaseOwner is true if the advisor should use the database owner as default role.
- UsePostgresDatabaseOwner bool
-}
-
// SQLReviewCheck checks the statements with sql review rules.
func SQLReviewCheck(
ctx context.Context,
sm *sheet.Manager,
statements string,
ruleList []*storepb.SQLReviewRule,
- checkContext SQLReviewCheckContext,
+ checkContext Context,
) ([]*storepb.Advice, error) {
asts, parseResult := sm.GetASTsForChecks(checkContext.DBType, statements)
@@ -555,27 +526,16 @@ func SQLReviewCheck(
ruleType := SQLReviewRuleType(rule.Type)
+ // Set per-rule fields
+ checkContext.AST = asts
+ checkContext.Statements = statements
+ checkContext.Rule = rule
+
adviceList, err := Check(
ctx,
checkContext.DBType,
ruleType,
- Context{
- DBSchema: checkContext.DBSchema,
- ChangeType: checkContext.ChangeType,
- EnablePriorBackup: checkContext.EnablePriorBackup,
- AST: asts,
- Statements: statements,
- Rule: rule,
- OriginalMetadata: checkContext.OriginalMetadata,
- FinalMetadata: checkContext.FinalMetadata,
- Driver: checkContext.Driver,
- CurrentDatabase: checkContext.CurrentDatabase,
- ClassificationConfig: checkContext.ClassificationConfig,
- UsePostgresDatabaseOwner: checkContext.UsePostgresDatabaseOwner,
- ListDatabaseNamesFunc: checkContext.ListDatabaseNamesFunc,
- InstanceID: checkContext.InstanceID,
- IsObjectCaseSensitive: checkContext.IsObjectCaseSensitive,
- },
+ checkContext,
)
if err != nil {
return nil, errors.Wrap(err, "failed to check statement")
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 65b0d01de2..e2d3555ed7 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -235,7 +235,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
},
}
- checkCtx := SQLReviewCheckContext{
+ checkCtx := Context{
Charset: "",
Collation: "",
DBType: dbType,
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 2ea43cdf13..62edbaa94b 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -176,7 +176,7 @@ func (e *StatementAdviseExecutor) runReview(
// Database secrets feature removed - using original statement directly
classificationConfig := getClassificationByProject(ctx, e.store, database.ProjectID)
- adviceList, err := advisor.SQLReviewCheck(ctx, e.sheetManager, statement, reviewConfig.SqlReviewRules, advisor.SQLReviewCheckContext{
+ adviceList, err := advisor.SQLReviewCheck(ctx, e.sheetManager, statement, reviewConfig.SqlReviewRules, advisor.Context{
Charset: dbMetadata.GetProto().CharacterSet,
Collation: dbMetadata.GetProto().Collation,
DBSchema: dbMetadata.GetProto(),