Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
Fix gh-ost directives in versioned releases (#19648)
p0nya6203f9be18d
diff --git a/backend/runner/taskrun/database_migrate_executor.go b/backend/runner/taskrun/database_migrate_executor.goindex 3782597d77..f8bfde26bc 100644--- a/backend/runner/taskrun/database_migrate_executor.go+++ b/backend/runner/taskrun/database_migrate_executor.go@@ -288,26 +288,15 @@ func (exec *DatabaseMigrateExecutor) runStandardMigration(ctx context.Context, d}, nil}-func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {- // Parse ghost flags from sheet directive+func executeGhostMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, driver db.Driver) error {flags, err := ghost.ParseGhostDirective(sheet.Statement)if err != nil {- return nil, errors.Wrapf(err, "failed to parse ghost directive")+ return errors.Wrapf(err, "failed to parse ghost directive")}if flags == nil {flags = make(map[string]string)}- // Get database driver- driver, err := exec.dbFactory.GetAdminDatabaseDriver(ctx, instance, database, db.ConnectionContext{- TenantMode: project.Setting.GetPostgresDatabaseTenantMode(),- TaskRunUID: &taskRunUID,- })- if err != nil {- return nil, errors.Wrapf(err, "failed to get driver connection for instance %q", instance.ResourceID)- }- defer driver.Close(ctx)-slog.Debug("Start migration...",slog.String("instance", database.InstanceID),slog.String("database", database.DatabaseName),@@ -315,35 +304,24 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivslog.String("sheetSha256", sheet.Sha256),)- // Set up execute options- opts := db.ExecuteOptions{}- if project != nil && project.Setting != nil {- opts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())- }- opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {- return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)- }-- // Prepare gh-ost migration context before beginning migration- // Remove all Bytebase directives from statement before passing to gh-ost+ // Remove all Bytebase directives from statement before passing to gh-ost.cleanedStatement := parserbase.CleanDirectives(sheet.Statement)statement := strings.TrimSpace(cleanedStatement)- // Trim trailing semicolons.statement = strings.TrimRight(statement, ";")tableName, err := ghost.GetTableNameFromStatement(statement)if err != nil {- return nil, err+ return err}adminDataSource := utils.DataSourceFromInstanceWithType(instance, storepb.DataSourceType_ADMIN)if adminDataSource == nil {- return nil, common.Errorf(common.Internal, "admin data source not found for instance %s", instance.ResourceID)+ return common.Errorf(common.Internal, "admin data source not found for instance %s", instance.ResourceID)}migrationContext, err := ghost.NewMigrationContext(ctx, task.ID, database, adminDataSource, tableName, fmt.Sprintf("_%d", time.Now().Unix()), cleanedStatement, false, flags, 10000000)if err != nil {- return nil, errors.Wrap(err, "failed to init migrationContext for gh-ost")+ return errors.Wrap(err, "failed to init migrationContext for gh-ost")}defer func() {// Use migrationContext.Uuid as the tls_config_key by convention.@@ -352,25 +330,8 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivgomysql.DeregisterTLSConfig(migrationContext.Uuid)}()- // Begin migration - create pending changelog- changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{- InstanceID: database.InstanceID,- DatabaseName: database.DatabaseName,- Status: store.ChangelogStatusPending,- SyncHistory: nil,- Payload: &storepb.ChangelogPayload{- TaskRun: common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID),- GitCommit: exec.profile.GitCommit,- },- })- if err != nil {- return nil, errors.Wrapf(err, "failed to create changelog")- }-- // Execute gh-ost migration// set buffer size to 1 to unblock the sender because there is no listener if the task is canceled.migrationError := make(chan error, 1)-migrator := logic.NewMigrator(migrationContext, "bb")defer func() {@@ -399,15 +360,52 @@ func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, drivmigrationError <- nil}()- var migrationErr errorselect {case err := <-migrationError:- migrationErr = err+ return errcase <-driverCtx.Done():migrationContext.PanicAbort <- errors.New("task canceled")- migrationErr = errors.New("task canceled")+ return errors.New("task canceled")+ }+}++func (exec *DatabaseMigrateExecutor) runGhostMigration(ctx context.Context, driverCtx context.Context, task *store.TaskMessage, taskRunUID int64, sheet *store.SheetMessage, instance *store.InstanceMessage, database *store.DatabaseMessage, project *store.ProjectMessage) (*storepb.TaskRunResult, error) {+ // Get database driver+ driver, err := exec.dbFactory.GetAdminDatabaseDriver(ctx, instance, database, db.ConnectionContext{+ TenantMode: project.Setting.GetPostgresDatabaseTenantMode(),+ TaskRunUID: &taskRunUID,+ })+ if err != nil {+ return nil, errors.Wrapf(err, "failed to get driver connection for instance %q", instance.ResourceID)+ }+ defer driver.Close(ctx)++ // Set up execute options+ opts := db.ExecuteOptions{}+ if project != nil && project.Setting != nil {+ opts.MaximumRetries = int(project.Setting.GetExecutionRetryPolicy().GetMaximumRetries())+ }+ opts.CreateTaskRunLog = func(t time.Time, e *storepb.TaskRunLog) error {+ return exec.store.CreateTaskRunLog(ctx, database.ProjectID, taskRunUID, t.UTC(), exec.profile.ReplicaID, e)+ }++ // Begin migration - create pending changelog+ changelogID, err := exec.store.CreateChangelog(ctx, &store.ChangelogMessage{+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ Status: store.ChangelogStatusPending,+ SyncHistory: nil,+ Payload: &storepb.ChangelogPayload{+ TaskRun: common.FormatTaskRun(database.ProjectID, task.PlanID, task.Environment, task.ID, taskRunUID),+ GitCommit: exec.profile.GitCommit,+ },+ })+ if err != nil {+ return nil, errors.Wrapf(err, "failed to create changelog")}+ migrationErr := executeGhostMigration(ctx, driverCtx, task, sheet, instance, database, driver)+// Dump after migration and update changelogupdate := &store.UpdateChangelogMessage{ResourceID: changelogID,@@ -527,14 +525,17 @@ func (exec *DatabaseMigrateExecutor) runVersionedRelease(ctx context.Context, dr},})- slog.Debug("Start migration...",- slog.String("instance", database.InstanceID),- slog.String("database", database.DatabaseName),- slog.String("type", task.Type.String()),- )-- // Execute the SQL- _, err = driver.Execute(driverCtx, sheet.Statement, opts)+ // Execute the SQL.+ if ghost.IsGhostEnabled(sheet.Statement) {+ err = executeGhostMigration(ctx, driverCtx, task, sheet, instance, database, driver)+ } else {+ slog.Debug("Start migration...",+ slog.String("instance", database.InstanceID),+ slog.String("database", database.DatabaseName),+ slog.String("type", task.Type.String()),+ )+ _, err = driver.Execute(driverCtx, sheet.Statement, opts)+ }if err != nil {migrationErr = errors.Wrapf(err, "failed to execute release file %s (version %s)", file.Path, file.Version)breakdiff --git a/backend/tests/gitops_test.go b/backend/tests/gitops_test.goindex 25f8b6ede8..a89913b370 100644--- a/backend/tests/gitops_test.go+++ b/backend/tests/gitops_test.go@@ -10,6 +10,7 @@ import ("github.com/stretchr/testify/require""github.com/bytebase/bytebase/backend/common"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/plugin/advisor/code")@@ -310,6 +311,144 @@ func TestGitOpsRollout(t *testing.T) {a.Equal(rollout.Name, rolloutResp2.Msg.Name)}+func TestGitOpsRolloutGhostDirective(t *testing.T) {+ t.Parallel()+ a := require.New(t)+ ctx := context.Background()+ ctl := &controller{}+ ctx, err := ctl.StartServerWithExternalPg(ctx)+ a.NoError(err)+ defer ctl.Close(ctx)++ mysqlContainer, err := getMySQLContainer(ctx)+ a.NoError(err)+ defer func() {+ mysqlContainer.Close(ctx)+ }()++ const databaseName = "gitops_rollout_ghost_db"+ mysqlDB := mysqlContainer.db+ _, err = mysqlDB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %v", databaseName))+ a.NoError(err)+ _, err = mysqlDB.Exec("DROP USER IF EXISTS bytebase")+ a.NoError(err)+ _, err = mysqlDB.Exec("CREATE USER 'bytebase' IDENTIFIED WITH mysql_native_password BY 'bytebase'")+ a.NoError(err)+ _, err = mysqlDB.Exec("GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, DELETE, DROP, EVENT, EXECUTE, INDEX, INSERT, PROCESS, REFERENCES, SELECT, SHOW DATABASES, SHOW VIEW, TRIGGER, UPDATE, USAGE, REPLICATION CLIENT, REPLICATION SLAVE, LOCK TABLES, RELOAD ON *.* to bytebase")+ a.NoError(err)++ projectID := generateRandomString("gitops-rollout-ghost")+ projectResp, err := ctl.projectServiceClient.CreateProject(ctx, connect.NewRequest(&v1pb.CreateProjectRequest{+ Project: &v1pb.Project{+ Name: fmt.Sprintf("projects/%s", projectID),+ Title: projectID,+ AllowSelfApproval: true,+ },+ ProjectId: projectID,+ }))+ a.NoError(err)+ project := projectResp.Msg++ instanceResp, err := ctl.instanceServiceClient.CreateInstance(ctx, connect.NewRequest(&v1pb.CreateInstanceRequest{+ InstanceId: generateRandomString("instance"),+ Instance: &v1pb.Instance{+ Title: "gitops-rollout-ghost",+ Engine: v1pb.Engine_MYSQL,+ Environment: stringPtr("environments/test"),+ Activation: true,+ DataSources: []*v1pb.DataSource{{Type: v1pb.DataSourceType_ADMIN, Host: mysqlContainer.host, Port: mysqlContainer.port, Username: "bytebase", Password: "bytebase", Id: "admin"}},+ },+ }))+ a.NoError(err)+ instance := instanceResp.Msg++ backupDBName := common.BackupDatabaseNameOfEngine(storepb.Engine_MYSQL)+ err = ctl.createDatabase(ctx, project, instance, nil, backupDBName, "")+ a.NoError(err)+ err = ctl.createDatabase(ctx, project, instance, nil, databaseName, "")+ a.NoError(err)++ createReleaseResp, err := ctl.releaseServiceClient.CreateRelease(ctx, connect.NewRequest(&v1pb.CreateReleaseRequest{+ Parent: project.Name,+ Release: &v1pb.Release{+ Type: v1pb.Release_VERSIONED,+ Files: []*v1pb.Release_File{+ {+ Path: "migrations/001__create_book.sql",+ Version: "001",+ Statement: []byte(mysqlMigrationStatement),+ },+ {+ Path: "migrations/002__add_author.sql",+ Version: "002",+ Statement: []byte(`-- gh-ost = {}+ALTER TABLE book ADD author VARCHAR(54);`),+ },+ },+ },+ }))+ a.NoError(err)++ planResp, err := ctl.planServiceClient.CreatePlan(ctx, connect.NewRequest(&v1pb.CreatePlanRequest{+ Parent: project.Name,+ Plan: &v1pb.Plan{+ Title: "GitOps gh-ost deployment",+ Description: "Deploy release with gh-ost directive",+ Specs: []*v1pb.Plan_Spec{+ {+ Id: uuid.NewString(),+ Config: &v1pb.Plan_Spec_ChangeDatabaseConfig{+ ChangeDatabaseConfig: &v1pb.Plan_ChangeDatabaseConfig{+ Release: createReleaseResp.Msg.Name,+ Targets: []string{+ fmt.Sprintf("%s/databases/%s", instance.Name, databaseName),+ },+ },+ },+ },+ },+ },+ }))+ a.NoError(err)+ plan := planResp.Msg++ rolloutResp, err := ctl.rolloutServiceClient.CreateRollout(ctx, connect.NewRequest(&v1pb.CreateRolloutRequest{+ Parent: plan.Name,+ }))+ a.NoError(err)+ rollout := rolloutResp.Msg++ issueResp, err := ctl.issueServiceClient.CreateIssue(ctx, connect.NewRequest(&v1pb.CreateIssueRequest{+ Parent: project.Name,+ Issue: &v1pb.Issue{+ Title: "GitOps gh-ost rollout",+ Description: "Deploy release via versioned release workflow with gh-ost",+ Type: v1pb.Issue_DATABASE_CHANGE,+ Plan: plan.Name,+ },+ }))+ a.NoError(err)+ issue := issueResp.Msg++ err = ctl.waitRollout(ctx, issue.Name, rollout.Name)+ a.NoError(err)++ dbSchemaResp, err := ctl.databaseServiceClient.GetDatabaseSchema(ctx, connect.NewRequest(&v1pb.GetDatabaseSchemaRequest{+ Name: fmt.Sprintf("%s/databases/%s/schema", instance.Name, databaseName),+ }))+ a.NoError(err)+ a.Equal(wantDBSchema2, dbSchemaResp.Msg.Schema)++ revisionsResp, err := ctl.revisionServiceClient.ListRevisions(ctx, connect.NewRequest(&v1pb.ListRevisionsRequest{+ Parent: fmt.Sprintf("%s/databases/%s", instance.Name, databaseName),+ }))+ a.NoError(err)+ a.Len(revisionsResp.Msg.Revisions, 2)+ for _, revision := range revisionsResp.Msg.Revisions {+ a.Equal(createReleaseResp.Msg.Name, revision.Release)+ }+}+// TestGitOpsRolloutMultiTarget tests a more complex GitOps scenario:// - A release with 3 migration files targeting 2 databases (test and prod)// - Verifies that all files are applied to all target databases