Skip to content

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

feat: impl ghost sync plan check (#7584)

p0ny2c25f17598f7
diff --git a/backend/api/v1/rollout_service_plan_check.go b/backend/api/v1/rollout_service_plan_check.go
index 4b2cca01b3..c4cb22a11b 100644
--- a/backend/api/v1/rollout_service_plan_check.go
+++ b/backend/api/v1/rollout_service_plan_check.go
@@ -108,6 +108,20 @@ func getPlanCheckRunsForSpec(ctx context.Context, s *store.Store, plan *store.Pl
},
})
}
+ if config.ChangeDatabaseConfig.Type == storepb.PlanConfig_ChangeDatabaseConfig_MIGRATE_GHOST {
+ planCheckRuns = append(planCheckRuns, &store.PlanCheckRunMessage{
+ CreatorUID: api.SystemBotID,
+ UpdaterUID: api.SystemBotID,
+ PlanUID: plan.UID,
+ Status: store.PlanCheckRunStatusRunning,
+ Type: store.PlanCheckDatabaseGhostSync,
+ Config: &storepb.PlanCheckRunConfig{
+ SheetId: int32(sheetUID),
+ DatabaseId: int32(database.UID),
+ ChangeDatabaseType: convertToChangeDatabaseType(config.ChangeDatabaseConfig.Type),
+ },
+ })
+ }
case *storepb.PlanConfig_Spec_RestoreDatabaseConfig:
// TODO(p0ny): implement
}
diff --git a/backend/runner/plancheck/ghost_sync_executor.go b/backend/runner/plancheck/ghost_sync_executor.go
new file mode 100644
index 0000000000..4bc165b4a5
--- /dev/null
+++ b/backend/runner/plancheck/ghost_sync_executor.go
@@ -0,0 +1,136 @@
+package plancheck
+
+import (
+ "context"
+
+ "github.com/github/gh-ost/go/logic"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/common"
+ api "github.com/bytebase/bytebase/backend/legacyapi"
+ "github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+// NewGhostSyncExecutor creates a task check gh-ost sync executor.
+func NewGhostSyncExecutor(store *store.Store, secret string) Executor {
+ return &GhostSyncExecutor{
+ store: store,
+ secret: secret,
+ }
+}
+
+// GhostSyncExecutor is the task check gh-ost sync executor.
+type GhostSyncExecutor struct {
+ store *store.Store
+ secret string
+}
+
+// Run will run the task check database connector executor once.
+func (e *GhostSyncExecutor) Run(ctx context.Context, planCheckRun *store.PlanCheckRunMessage) (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.
+ defer func() {
+ if r := recover(); r != nil {
+ panicErr, ok := r.(error)
+ if !ok {
+ panicErr = errors.Errorf("%v", r)
+ }
+
+ results = []*storepb.PlanCheckRunResult_Result{
+ {
+ Status: storepb.PlanCheckRunResult_Result_ERROR,
+ Title: "gh-ost dry run failed",
+ Content: panicErr.Error(),
+ Code: common.Internal.Int64(),
+ Report: nil,
+ },
+ }
+ err = nil
+ }
+ }()
+
+ databaseID := int(planCheckRun.Config.DatabaseId)
+ database, err := e.store.GetDatabaseV2(ctx, &store.FindDatabaseMessage{UID: &databaseID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get database ID %v", databaseID)
+ }
+ if database == nil {
+ return nil, errors.Errorf("database not found %v", databaseID)
+ }
+ instance, err := e.store.GetInstanceV2(ctx, &store.FindInstanceMessage{ResourceID: &database.InstanceID})
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get instance %v", database.InstanceID)
+ }
+ if instance == nil {
+ return nil, errors.Errorf("instance not found %v", database.InstanceID)
+ }
+
+ adminDataSource := utils.DataSourceFromInstanceWithType(instance, api.Admin)
+ if adminDataSource == nil {
+ return nil, common.Errorf(common.Internal, "admin data source not found for instance %d", instance.UID)
+ }
+
+ instanceUsers, err := e.store.ListInstanceUsers(ctx, &store.FindInstanceUserMessage{InstanceUID: instance.UID})
+ if err != nil {
+ return nil, common.Errorf(common.Internal, "failed to find instance user by instanceID %d", instance.UID)
+ }
+
+ sheetID := int(planCheckRun.Config.SheetId)
+ sheet, err := e.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetID}, api.SystemBotID)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get sheet %d", sheetID)
+ }
+ if sheet == nil {
+ return nil, errors.Errorf("sheet %d not found", sheetID)
+ }
+ statement, err := e.store.GetSheetStatementByID(ctx, sheetID)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to get sheet statement %d", sheetID)
+ }
+
+ materials := utils.GetSecretMapFromDatabaseMessage(database)
+ // To avoid leaking the rendered statement, the error message should use the original statement and not the rendered statement.
+ renderedStatement := utils.RenderStatement(statement, materials)
+
+ tableName, err := utils.GetTableNameFromStatement(renderedStatement)
+ if err != nil {
+ 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)
+ if err != nil {
+ return nil, err
+ }
+
+ migrationContext, err := utils.NewMigrationContext(config)
+ if err != nil {
+ return nil, common.Wrapf(err, common.Internal, "failed to create migration context")
+ }
+
+ migrator := logic.NewMigrator(migrationContext, "bb")
+
+ if err := migrator.Migrate(); err != nil {
+ return []*storepb.PlanCheckRunResult_Result{
+ {
+ Status: storepb.PlanCheckRunResult_Result_ERROR,
+ Title: "gh-ost dry run failed",
+ Content: err.Error(),
+ Code: common.Internal.Int64(),
+ Report: nil,
+ },
+ }, nil
+ }
+
+ return []*storepb.PlanCheckRunResult_Result{
+ {
+ Status: storepb.PlanCheckRunResult_Result_SUCCESS,
+ Title: "OK",
+ Content: "gh-ost dry run succeeded",
+ Code: common.Ok.Int64(),
+ Report: nil,
+ },
+ }, nil
+}
diff --git a/backend/server/server.go b/backend/server/server.go
index 9f1b849374..4bf5bcabed 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -514,6 +514,8 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
s.PlanCheckScheduler.Register(store.PlanCheckDatabaseStatementType, statementTypeExecutor)
statementAdviseExecutor := plancheck.NewStatementAdviseExecutor(storeInstance, s.dbFactory, s.licenseService)
s.PlanCheckScheduler.Register(store.PlanCheckDatabaseStatementAdvise, statementAdviseExecutor)
+ ghostSyncExecutor := plancheck.NewGhostSyncExecutor(storeInstance, s.secret)
+ s.PlanCheckScheduler.Register(store.PlanCheckDatabaseGhostSync, ghostSyncExecutor)
}
// Anomaly scanner