Skip to content

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

refactor: plan check run executor pass PlanCheckRun.Config only (#8204)

p0ny7f2edc67891f
diff --git a/backend/runner/plancheck/database_connect_executor.go b/backend/runner/plancheck/database_connect_executor.go
index 2f96e090d3..51027a1457 100644
--- a/backend/runner/plancheck/database_connect_executor.go
+++ b/backend/runner/plancheck/database_connect_executor.go
@@ -29,8 +29,7 @@ type DatabaseConnectExecutor struct {
}
// Run runs the executor.
-func (e *DatabaseConnectExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- config := planCheckRun.Config
+func (e *DatabaseConnectExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
if err != nil {
diff --git a/backend/runner/plancheck/executor.go b/backend/runner/plancheck/executor.go
index 07625cb84f..cdbccb902f 100644
--- a/backend/runner/plancheck/executor.go
+++ b/backend/runner/plancheck/executor.go
@@ -8,11 +8,10 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/db"
- "github.com/bytebase/bytebase/backend/store"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func runExecutorOnce(ctx context.Context, exec Executor, planCheckRun *store.PlanCheckRunMessage) (results []*storepb.PlanCheckRunResult_Result, err error) {
+func runExecutorOnce(ctx context.Context, exec Executor, config *storepb.PlanCheckRunConfig) (results []*storepb.PlanCheckRunResult_Result, err error) {
defer func() {
if r := recover(); r != nil {
panicErr, ok := r.(error)
@@ -24,13 +23,13 @@ func runExecutorOnce(ctx context.Context, exec Executor, planCheckRun *store.Pla
}
}()
- return exec.Run(ctx, planCheckRun)
+ return exec.Run(ctx, config)
}
// Executor is the plan check executor.
type Executor interface {
// Run will be called periodically by the plan check scheduler
- Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) (results []*storepb.PlanCheckRunResult_Result, err error)
+ Run(ctx context.Context, config *storepb.PlanCheckRunConfig) (results []*storepb.PlanCheckRunResult_Result, err error)
}
func isStatementTypeCheckSupported(dbType db.Type) bool {
diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.go
index ffc567526b..5b4c56732e 100644
--- a/backend/runner/plancheck/ghost_sync_executor.go
+++ b/backend/runner/plancheck/ghost_sync_executor.go
@@ -2,6 +2,7 @@ package plancheck
import (
"context"
+ "math/rand"
"github.com/github/gh-ost/go/logic"
"github.com/pkg/errors"
@@ -28,7 +29,7 @@ type GhostSyncExecutor struct {
}
// Run runs the gh-ost sync check executor.
-func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) (results []*storepb.PlanCheckRunResult_Result, err error) {
+func (e *GhostSyncExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) (results []*storepb.PlanCheckRunResult_Result, err error) {
// gh-ost dry run could panic.
// It may be bytebase who panicked, but that's rare. So
// capture the error and send it into the result list.
@@ -52,11 +53,11 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanChe
}
}()
- if planCheckRun.Config.DatabaseGroupUid != nil {
+ if config.DatabaseGroupUid != nil {
return nil, errors.Errorf("database group is not supported")
}
- instanceUID := int(planCheckRun.Config.InstanceUid)
+ instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get instance UID %v", instanceUID)
@@ -65,12 +66,12 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanChe
return nil, errors.Errorf("instance not found UID %v", instanceUID)
}
- database, err := e.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{InstanceID: &instance.ResourceID, DatabaseName: &planCheckRun.Config.DatabaseName})
+ database, err := e.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{InstanceID: &instance.ResourceID, DatabaseName: &config.DatabaseName})
if err != nil {
- return nil, errors.Wrapf(err, "failed to get database %q", planCheckRun.Config.DatabaseName)
+ return nil, errors.Wrapf(err, "failed to get database %q", config.DatabaseName)
}
if database == nil {
- return nil, errors.Errorf("database not found %q", planCheckRun.Config.DatabaseName)
+ return nil, errors.Errorf("database not found %q", config.DatabaseName)
}
adminDataSource := utils.DataSourceFromInstanceWithType(instance, api.Admin)
@@ -83,7 +84,7 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanChe
return nil, common.Errorf(common.Internal, "failed to find instance user by instanceID %d", instance.UID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
@@ -105,12 +106,12 @@ func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanChe
return nil, common.Wrapf(err, common.Internal, "failed to parse table name from statement, statement: %v", statement)
}
- config, err := utils.GetGhostConfig(planCheckRun.UID, database, adminDataSource, e.secret, instanceUsers, tableName, renderedStatement, true, 20000000)
+ ghostConfig, err := utils.GetGhostConfig(rand.Intn(10000000), database, adminDataSource, e.secret, instanceUsers, tableName, renderedStatement, true, 20000000)
if err != nil {
return nil, err
}
- migrationContext, err := utils.NewMigrationContext(config)
+ migrationContext, err := utils.NewMigrationContext(ghostConfig)
if err != nil {
return nil, common.Wrapf(err, common.Internal, "failed to create migration context")
}
diff --git a/backend/runner/plancheck/pitr_mysql_executor.go b/backend/runner/plancheck/pitr_mysql_executor.go
index f2450fff4a..dfbe183079 100644
--- a/backend/runner/plancheck/pitr_mysql_executor.go
+++ b/backend/runner/plancheck/pitr_mysql_executor.go
@@ -27,12 +27,12 @@ type PITRMySQLExecutor struct {
}
// Run runs the PITR MySQL executor.
-func (e *PITRMySQLExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- if planCheckRun.Config.DatabaseGroupUid != nil {
+func (e *PITRMySQLExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ if config.DatabaseGroupUid != nil {
return nil, errors.Errorf("database group is not supported")
}
- instanceUID := int(planCheckRun.Config.InstanceUid)
+ instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
if err != nil {
return nil, errors.Wrapf(err, "failed to get instance UID %v", instanceUID)
@@ -40,7 +40,7 @@ func (e *PITRMySQLExecutor) Run(ctx context.Context, planCheckRun *store.PlanChe
if instance == nil {
return nil, errors.Errorf("instance not found UID %v", instanceUID)
}
- databaseName := planCheckRun.Config.DatabaseName
+ databaseName := config.DatabaseName
driver, err := e.dbFactory.GetAdminDatabaseDriver(ctx, instance, nil /* database */)
if err != nil {
diff --git a/backend/runner/plancheck/scheduler.go b/backend/runner/plancheck/scheduler.go
index 2dd9d6aa96..8afed85295 100644
--- a/backend/runner/plancheck/scheduler.go
+++ b/backend/runner/plancheck/scheduler.go
@@ -123,7 +123,7 @@ func (s *Scheduler) runPlanCheckRun(ctx context.Context, planCheckRun *store.Pla
s.stateCfg.InstanceOutstandingConnections[instanceUID]--
s.stateCfg.Unlock()
}()
- results, err := runExecutorOnce(ctx, executor, planCheckRun)
+ results, err := runExecutorOnce(ctx, executor, planCheckRun.Config)
if err != nil {
s.markPlanCheckRunFailed(ctx, planCheckRun, err.Error())
return
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index 6f5f53ce46..dfddc9abf0 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -38,25 +38,19 @@ type StatementAdviseExecutor struct {
}
// Run will run the plan check statement advise executor once, and run its sub-advisors one-by-one.
-func (e *StatementAdviseExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- if planCheckRun.Type != store.PlanCheckDatabaseStatementAdvise {
- return nil, common.Errorf(common.Invalid, "unexpected plan check type in statement advise executor: %v", planCheckRun.Type)
- }
-
- changeType := planCheckRun.Config.ChangeDatabaseType
- if changeType == storepb.PlanCheckRunConfig_CHANGE_DATABASE_TYPE_UNSPECIFIED {
+func (e *StatementAdviseExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ if config.ChangeDatabaseType == storepb.PlanCheckRunConfig_CHANGE_DATABASE_TYPE_UNSPECIFIED {
return nil, errors.Errorf("change database type is unspecified")
}
- if planCheckRun.Config.DatabaseGroupUid != nil {
- return e.runForDatabaseGroupTarget(ctx, planCheckRun, *planCheckRun.Config.DatabaseGroupUid)
+ if config.DatabaseGroupUid != nil {
+ return e.runForDatabaseGroupTarget(ctx, config, *config.DatabaseGroupUid)
}
- return e.runForDatabaseTarget(ctx, planCheckRun)
+ return e.runForDatabaseTarget(ctx, config)
}
-func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- changeType := planCheckRun.Config.ChangeDatabaseType
- config := planCheckRun.Config
+func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ changeType := config.ChangeDatabaseType
instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
@@ -118,7 +112,7 @@ func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, plan
return nil, err
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
@@ -228,9 +222,8 @@ func (e *StatementAdviseExecutor) runForDatabaseTarget(ctx context.Context, plan
return results, nil
}
-func (e *StatementAdviseExecutor) runForDatabaseGroupTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
- changeType := planCheckRun.Config.ChangeDatabaseType
- config := planCheckRun.Config
+func (e *StatementAdviseExecutor) runForDatabaseGroupTarget(ctx context.Context, config *storepb.PlanCheckRunConfig, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
+ changeType := config.ChangeDatabaseType
databaseGroup, err := e.store.GetDatabaseGroup(ctx, &store.FindDatabaseGroupMessage{
UID: &databaseGroupUID,
})
@@ -267,7 +260,7 @@ func (e *StatementAdviseExecutor) runForDatabaseGroupTarget(ctx context.Context,
return nil, errors.Errorf("no matched databases found in database group %q", databaseGroup.ResourceID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
diff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.go
index c49f0193e6..3200007667 100644
--- a/backend/runner/plancheck/statement_report_executor.go
+++ b/backend/runner/plancheck/statement_report_executor.go
@@ -4,9 +4,10 @@ import (
"context"
"database/sql"
"fmt"
- "log/slog"
"strings"
+ "log/slog"
+
pgquery "github.com/pganalyze/pg_query_go/v4"
tidbparser "github.com/pingcap/tidb/parser"
tidbast "github.com/pingcap/tidb/parser/ast"
@@ -40,16 +41,14 @@ type StatementReportExecutor struct {
}
// Run runs the statement report executor.
-func (e *StatementReportExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- if planCheckRun.Config.DatabaseGroupUid != nil {
- return e.runForDatabaseGroupTarget(ctx, planCheckRun, *planCheckRun.Config.DatabaseGroupUid)
+func (e *StatementReportExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ if config.DatabaseGroupUid != nil {
+ return e.runForDatabaseGroupTarget(ctx, config, *config.DatabaseGroupUid)
}
- return e.runForDatabaseTarget(ctx, planCheckRun)
+ return e.runForDatabaseTarget(ctx, config)
}
-func (e *StatementReportExecutor) runForDatabaseTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- config := planCheckRun.Config
-
+func (e *StatementReportExecutor) runForDatabaseTarget(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
if err != nil {
@@ -89,7 +88,7 @@ func (e *StatementReportExecutor) runForDatabaseTarget(ctx context.Context, plan
return nil, errors.Errorf("database schema metadata not found: %d", database.UID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
@@ -157,9 +156,7 @@ func (e *StatementReportExecutor) runForDatabaseTarget(ctx context.Context, plan
}
}
-func (e *StatementReportExecutor) runForDatabaseGroupTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
- config := planCheckRun.Config
-
+func (e *StatementReportExecutor) runForDatabaseGroupTarget(ctx context.Context, config *storepb.PlanCheckRunConfig, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
databaseGroup, err := e.store.GetDatabaseGroup(ctx, &store.FindDatabaseGroupMessage{
UID: &databaseGroupUID,
})
@@ -196,7 +193,7 @@ func (e *StatementReportExecutor) runForDatabaseGroupTarget(ctx context.Context,
return nil, errors.Errorf("no matched databases found in database group %q", databaseGroup.ResourceID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
diff --git a/backend/runner/plancheck/statement_type_executor.go b/backend/runner/plancheck/statement_type_executor.go
index 58e4d27f90..2809b2ef79 100644
--- a/backend/runner/plancheck/statement_type_executor.go
+++ b/backend/runner/plancheck/statement_type_executor.go
@@ -38,21 +38,19 @@ type StatementTypeExecutor struct {
}
// Run runs the statement type executor.
-func (e *StatementTypeExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- changeType := planCheckRun.Config.ChangeDatabaseType
- if changeType == storepb.PlanCheckRunConfig_CHANGE_DATABASE_TYPE_UNSPECIFIED {
+func (e *StatementTypeExecutor) Run(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ if config.ChangeDatabaseType == storepb.PlanCheckRunConfig_CHANGE_DATABASE_TYPE_UNSPECIFIED {
return nil, errors.Errorf("change database type is unspecified")
}
- if planCheckRun.Config.DatabaseGroupUid != nil {
- return e.runForDatabaseGroupTarget(ctx, planCheckRun, *planCheckRun.Config.DatabaseGroupUid)
+ if config.DatabaseGroupUid != nil {
+ return e.runForDatabaseGroupTarget(ctx, config, *config.DatabaseGroupUid)
}
- return e.runForDatabaseTarget(ctx, planCheckRun)
+ return e.runForDatabaseTarget(ctx, config)
}
-func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) ([]*storepb.PlanCheckRunResult_Result, error) {
- changeType := planCheckRun.Config.ChangeDatabaseType
- config := planCheckRun.Config
+func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, config *storepb.PlanCheckRunConfig) ([]*storepb.PlanCheckRunResult_Result, error) {
+ changeType := config.ChangeDatabaseType
instanceUID := int(config.InstanceUid)
instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{UID: &instanceUID})
@@ -90,7 +88,7 @@ func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, planCh
return nil, errors.Errorf("database schema not found: %d", database.UID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)
@@ -158,9 +156,8 @@ func (e *StatementTypeExecutor) runForDatabaseTarget(ctx context.Context, planCh
return results, nil
}
-func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, planCheckRun *store.PlanCheckRunMessage, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
- changeType := planCheckRun.Config.ChangeDatabaseType
- config := planCheckRun.Config
+func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, config *storepb.PlanCheckRunConfig, databaseGroupUID int64) ([]*storepb.PlanCheckRunResult_Result, error) {
+ changeType := config.ChangeDatabaseType
databaseGroup, err := e.store.GetDatabaseGroup(ctx, &store.FindDatabaseGroupMessage{
UID: &databaseGroupUID,
@@ -199,7 +196,7 @@ func (e *StatementTypeExecutor) runForDatabaseGroupTarget(ctx context.Context, p
return nil, errors.Errorf("no matched databases found in database group %q", databaseGroup.ResourceID)
}
- sheetUID := int(planCheckRun.Config.SheetUid)
+ sheetUID := int(config.SheetUid)
sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID}, api.SystemBotID)
if err != nil {
return nil, errors.Wrapf(err, "failed to get sheet %d", sheetUID)