Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: pass migration info from vcs webhook (#2614)
Yilong Li0f68960f2765
diff --git a/api/issue.go b/api/issue.goindex 77ec929703..417cfa960b 100644--- a/api/issue.go+++ b/api/issue.go@@ -157,7 +157,7 @@ type UpdateSchemaDetail struct {EarliestAllowedTs int64 `jsonapi:"attr,earliestAllowedTs"`}-// UpdateSchemaContext is the issue create context for updating database schema.+// UpdateSchemaContext is the shared issue create context for updating database schema and data.type UpdateSchemaContext struct {// MigrationType is the type of a migration.MigrationType db.MigrationType `json:"migrationType"`@@ -166,6 +166,9 @@ type UpdateSchemaContext struct {DetailList []*UpdateSchemaDetail `json:"updateSchemaDetailList"`// VCSPushEvent is the event information for VCS push.VCSPushEvent *vcs.PushEvent `json:"vcsPushEvent"`+ // MigrationInfo is only available when VCSPushEvent != nil.+ // It's parsed from VCSPushEvent.+ MigrationInfo *db.MigrationInfo `json:"migrationInfo,omitempty"`}// UpdateSchemaGhostDetail is the detail of updating database schema using gh-ost.diff --git a/api/task.go b/api/task.goindex e1856461ed..a6dc503ea5 100644--- a/api/task.go+++ b/api/task.go@@ -95,10 +95,12 @@ type TaskDatabaseCreatePayload struct {// TaskDatabaseSchemaUpdatePayload is the task payload for database schema update (DDL).type TaskDatabaseSchemaUpdatePayload struct {- MigrationType db.MigrationType `json:"migrationType,omitempty"`- Statement string `json:"statement,omitempty"`- SchemaVersion string `json:"schemaVersion,omitempty"`- VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`+ Statement string `json:"statement,omitempty"`+ SchemaVersion string `json:"schemaVersion,omitempty"`+ VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`+ // MigrationInfo is only available when VCSPushEvent != nil.+ // It's parsed from VCSPushEvent.+ MigrationInfo *db.MigrationInfo `json:"migrationInfo,omitempty"`}// TaskDatabaseSchemaUpdateGhostSyncPayload is the task payload for gh-ost syncing ghost table.@@ -121,6 +123,9 @@ type TaskDatabaseDataUpdatePayload struct {Statement string `json:"statement,omitempty"`SchemaVersion string `json:"schemaVersion,omitempty"`VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`+ // MigrationInfo is only available when VCSPushEvent != nil.+ // It's parsed from VCSPushEvent.+ MigrationInfo *db.MigrationInfo `json:"migrationInfo,omitempty"`}// TaskDatabaseBackupPayload is the task payload for database backup.@@ -207,7 +212,6 @@ type TaskCreate struct {Labels string `jsonapi:"attr,labels"`BackupID *int `jsonapi:"attr,backupId"`VCSPushEvent *vcs.PushEvent- MigrationType db.MigrationType `jsonapi:"attr,migrationType"`}// TaskFind is the API message for finding tasks.diff --git a/server/issue.go b/server/issue.goindex 99700e9d99..1dbfac7987 100644--- a/server/issue.go+++ b/server/issue.go@@ -639,6 +639,10 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Con}schemaVersion := common.DefaultMigrationVersion()+ // VCS push event contains the migration version in the file path.+ if c.MigrationInfo != nil {+ schemaVersion = c.MigrationInfo.Version+ }// Tenant mode project pipeline has its own generation.if project.TenantMode == api.TenantModeTenant {if !s.feature(api.FeatureMultiTenancy) {@@ -664,7 +668,7 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Conreturn nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", d.DatabaseID))}- taskCreate, err := getUpdateTask(database, c.MigrationType, c.VCSPushEvent, d, schemaVersion)+ taskCreate, err := getUpdateTask(database, c, d, schemaVersion)if err != nil {return nil, err}@@ -697,7 +701,7 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.ConenvironmentSet[database.Instance.Environment.Name] = trueenvironmentID = database.Instance.EnvironmentID- taskCreate, err := getUpdateTask(database, c.MigrationType, c.VCSPushEvent, d, schemaVersion)+ taskCreate, err := getUpdateTask(database, c, d, schemaVersion)if err != nil {return nil, err}@@ -743,7 +747,7 @@ func (s *Server) getPipelineCreateForDatabaseSchemaAndDataUpdate(ctx context.Conreturn nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", d.DatabaseID))}- taskCreate, err := getUpdateTask(database, c.MigrationType, c.VCSPushEvent, d, schemaVersion)+ taskCreate, err := getUpdateTask(database, c, d, schemaVersion)if err != nil {return nil, err}@@ -825,32 +829,32 @@ func (s *Server) getPipelineCreateForDatabaseSchemaUpdateGhost(ctx context.Contereturn create, nil}-func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPushEvent *vcs.PushEvent, d *api.UpdateSchemaDetail, schemaVersion string) (*api.TaskCreate, error) {+func getUpdateTask(database *api.Database, c api.UpdateSchemaContext, d *api.UpdateSchemaDetail, schemaVersion string) (*api.TaskCreate, error) {taskName := fmt.Sprintf("Establish %q baseline", database.Name)- switch migrationType {+ switch c.MigrationType {case db.Migrate:taskName = fmt.Sprintf("Update %q schema", database.Name)case db.Data:taskName = fmt.Sprintf("Update %q data", database.Name)}payload := api.TaskDatabaseSchemaUpdatePayload{}- payload.MigrationType = migrationTypepayload.Statement = d.Statementpayload.SchemaVersion = schemaVersion- if vcsPushEvent != nil {- payload.VCSPushEvent = vcsPushEvent+ if c.VCSPushEvent != nil {+ payload.VCSPushEvent = c.VCSPushEvent+ payload.MigrationInfo = c.MigrationInfo}bytes, err := json.Marshal(payload)if err != nil {errMsg := fmt.Sprintf("Failed to marshal database schema update payload: %v", err)- if migrationType == db.Data {+ if c.MigrationType == db.Data {errMsg = fmt.Sprintf("Failed to marshal database data update payload: %v", err)}return nil, echo.NewHTTPError(http.StatusInternalServerError, errMsg)}taskType := api.TaskDatabaseSchemaUpdate- if migrationType == db.Data {+ if c.MigrationType == db.Data {taskType = api.TaskDatabaseDataUpdate}return &api.TaskCreate{@@ -861,7 +865,6 @@ func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPuType: taskType,Statement: d.Statement,EarliestAllowedTs: d.EarliestAllowedTs,- MigrationType: migrationType,Payload: string(bytes),}, nil}@@ -1038,7 +1041,6 @@ func createGhostTaskList(database *api.Database, vcsPushEvent *vcs.PushEvent, deType: api.TaskDatabaseSchemaUpdateGhostSync,Statement: detail.Statement,EarliestAllowedTs: detail.EarliestAllowedTs,- MigrationType: db.Migrate,Payload: string(bytesSync),})diff --git a/server/task_executor.go b/server/task_executor.goindex 0099f22a51..14a12ce05b 100644--- a/server/task_executor.go+++ b/server/task_executor.go@@ -52,7 +52,7 @@ func RunTaskExecutorOnce(ctx context.Context, exec TaskExecutor, server *Server,return exec.RunOnce(ctx, server, task)}-func preMigration(ctx context.Context, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) (*db.MigrationInfo, error) {+func preMigration(ctx context.Context, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, mi *db.MigrationInfo) (*db.MigrationInfo, error) {if task.Database == nil {msg := "missing database when updating schema"if migrationType == db.Data {@@ -62,11 +62,11 @@ func preMigration(ctx context.Context, server *Server, task *api.Task, migration}databaseName := task.Database.Name- mi := &db.MigrationInfo{- ReleaseVersion: server.profile.Version,- Type: migrationType,- }if vcsPushEvent == nil {+ mi = &db.MigrationInfo{+ ReleaseVersion: server.profile.Version,+ Type: migrationType,+ }mi.Source = db.UIcreator, err := server.store.GetPrincipalByID(ctx, task.CreatorID)if err != nil {@@ -82,7 +82,8 @@ func preMigration(ctx context.Context, server *Server, task *api.Task, migration// TODO(d): support semantic versioning.mi.Version = schemaVersionmi.Description = task.Name- } else {+ } else if mi == nil {+ // TODO(dragonly): remove this branch when old task without MigrationInfo is rare.repo, err := findRepositoryByTask(ctx, server, task)if err != nil {return nil, err@@ -300,8 +301,8 @@ func postMigration(ctx context.Context, server *Server, task *api.Task, vcsPushE}, nil}-func runMigration(ctx context.Context, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent) (terminated bool, result *api.TaskRunResultPayload, err error) {- mi, err := preMigration(ctx, server, task, migrationType, statement, schemaVersion, vcsPushEvent)+func runMigration(ctx context.Context, server *Server, task *api.Task, migrationType db.MigrationType, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, mi *db.MigrationInfo) (bool, *api.TaskRunResultPayload, error) {+ mi, err := preMigration(ctx, server, task, migrationType, statement, schemaVersion, vcsPushEvent, mi)if err != nil {return true, nil, err}diff --git a/server/task_executor_data_update.go b/server/task_executor_data_update.goindex 4e4e4995db..9674bdc4de 100644--- a/server/task_executor_data_update.go+++ b/server/task_executor_data_update.go@@ -28,7 +28,7 @@ func (*DataUpdateTaskExecutor) RunOnce(ctx context.Context, server *Server, taskreturn true, nil, errors.Wrap(err, "invalid database data update payload")}- return runMigration(ctx, server, task, db.Data, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent)+ return runMigration(ctx, server, task, db.Data, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, payload.MigrationInfo)}// IsCompleted tells the scheduler if the task execution has completed.diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.goindex c18694f4b7..f7bc107474 100644--- a/server/task_executor_schema_update.go+++ b/server/task_executor_schema_update.go@@ -8,6 +8,7 @@ import ("github.com/pkg/errors""github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/plugin/db")// NewSchemaUpdateTaskExecutor creates a schema update (DDL) task executor.@@ -28,7 +29,7 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Servereturn true, nil, errors.Wrap(err, "invalid database schema update payload")}- return runMigration(ctx, server, task, payload.MigrationType, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent)+ return runMigration(ctx, server, task, db.Migrate, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, payload.MigrationInfo)}// IsCompleted tells the scheduler if the task execution has completed.diff --git a/server/task_executor_schema_update_ghost_cutover.go b/server/task_executor_schema_update_ghost_cutover.goindex 1f77f009d0..cbe22d7123 100644--- a/server/task_executor_schema_update_ghost_cutover.go+++ b/server/task_executor_schema_update_ghost_cutover.go@@ -18,7 +18,6 @@ import ("github.com/bytebase/bytebase/common/log""github.com/bytebase/bytebase/plugin/db""github.com/bytebase/bytebase/plugin/db/util"- vcsPlugin "github.com/bytebase/bytebase/plugin/vcs")// NewSchemaUpdateGhostCutoverTaskExecutor creates a schema update (gh-ost) cutover task executor.@@ -65,13 +64,13 @@ func (exec *SchemaUpdateGhostCutoverTaskExecutor) RunOnce(ctx context.Context, s}sharedGhost := value.(sharedGhostState)- return cutover(ctx, server, task, payload.Statement, payload.SchemaVersion, payload.VCSPushEvent, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)+ return cutover(ctx, server, task, payload, postponeFilename, sharedGhost.migrationContext, sharedGhost.errCh)}-func cutover(ctx context.Context, server *Server, task *api.Task, statement, schemaVersion string, vcsPushEvent *vcsPlugin.PushEvent, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {- statement = strings.TrimSpace(statement)+func cutover(ctx context.Context, server *Server, task *api.Task, payload *api.TaskDatabaseSchemaUpdateGhostSyncPayload, postponeFilename string, migrationContext *base.MigrationContext, errCh <-chan error) (terminated bool, result *api.TaskRunResultPayload, err error) {+ statement := strings.TrimSpace(payload.Statement)- mi, err := preMigration(ctx, server, task, db.Migrate, statement, schemaVersion, vcsPushEvent)+ mi, err := preMigration(ctx, server, task, db.Migrate, statement, payload.SchemaVersion, payload.VCSPushEvent, nil)if err != nil {return true, nil, err}@@ -140,7 +139,7 @@ func cutover(ctx context.Context, server *Server, task *api.Task, statement, schreturn true, nil, err}- return postMigration(ctx, server, task, vcsPushEvent, mi, migrationID, schema)+ return postMigration(ctx, server, task, payload.VCSPushEvent, mi, migrationID, schema)}func waitForCutover(ctx context.Context, migrationContext *base.MigrationContext) bool {diff --git a/server/webhook.go b/server/webhook.goindex 188196a163..8b5e419f66 100644--- a/server/webhook.go+++ b/server/webhook.go@@ -623,7 +623,7 @@ func (s *Server) prepareIssueFromPushEventDDL(ctx context.Context, repo *api.Rep// NOTE: We do not want to use filepath.Join here because we always need "/" as the path separator.migrationInfo, err := db.ParseMigrationInfo(file, path.Join(repo.BaseDirectory, repo.FilePathTemplate))if err != nil {- log.Debug("Failed to parse migration info",+ log.Error("Failed to parse migration info",zap.Int("project", repo.ProjectID),zap.Any("pushEvent", pushEvent),zap.String("file", file),@@ -631,6 +631,21 @@ func (s *Server) prepareIssueFromPushEventDDL(ctx context.Context, repo *api.Rep)return nil, nil}+ migrationInfo.Creator = pushEvent.FileCommit.AuthorName+ miPayload := &db.MigrationInfoPayload{+ VCSPushEvent: pushEvent,+ }+ bytes, err := json.Marshal(miPayload)+ if err != nil {+ log.Error("Failed to marshal vcs push event payload",+ zap.Int("project", repo.ProjectID),+ zap.Any("pushEvent", pushEvent),+ zap.String("file", file),+ zap.Error(err),+ )+ return nil, nil+ }+ migrationInfo.Payload = string(bytes)content, err := s.readFileContent(ctx, pushEvent, webhookEndpointID, file)if err != nil {@@ -744,9 +759,10 @@ func (s *Server) createIssueFromPushEvent(ctx context.Context, pushEvent *vcs.PucreateContext, err := json.Marshal(&api.UpdateSchemaContext{- MigrationType: db.Migrate,+ MigrationType: migrationInfo.Type,VCSPushEvent: pushEvent,DetailList: updateSchemaDetails,+ MigrationInfo: migrationInfo,},)if err != nil {