Skip to content

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

feat: use sheet for gitops issues (#5785)

p0ny04871e9d6c3d
diff --git a/backend/legacyapi/sheet.go b/backend/legacyapi/sheet.go
index 70794d0367..79b2c2baf2 100644
--- a/backend/legacyapi/sheet.go
+++ b/backend/legacyapi/sheet.go
@@ -26,6 +26,8 @@ const (
SheetFromGitLab SheetSource = "GITLAB"
// SheetFromGitHub is the sheet synced from GitHub (for both GitHub.com and GitHub Enterprise).
SheetFromGitHub SheetSource = "GITHUB"
+ // SheetFromBitbucket is the sheet synced from Bitbucket.
+ SheetFromBitbucket SheetSource = "BITBUCKET"
)
// SheetType is the type of sheet.
diff --git a/backend/legacyapi/task.go b/backend/legacyapi/task.go
index 785e551b1f..4506cf249d 100644
--- a/backend/legacyapi/task.go
+++ b/backend/legacyapi/task.go
@@ -353,6 +353,7 @@ type TaskPatch struct {
Payload *string
Statement *string `jsonapi:"attr,statement"`
+ SheetID *int `jsonapi:"attr,sheetId"`
SchemaVersion *string
RollbackEnabled *bool `jsonapi:"attr,rollbackEnabled"`
RollbackSQLStatus *RollbackSQLStatus
diff --git a/backend/runner/taskrun/scheduler.go b/backend/runner/taskrun/scheduler.go
index a4f83f84dc..3e3b98048e 100644
--- a/backend/runner/taskrun/scheduler.go
+++ b/backend/runner/taskrun/scheduler.go
@@ -364,7 +364,11 @@ func (s *Scheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
// PatchTask patches the statement, earliest allowed time and rollbackEnabled for a task.
func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, taskPatch *api.TaskPatch, issue *store.IssueMessage) error {
- if taskPatch.Statement != nil {
+ if taskPatch.Statement != nil && taskPatch.SheetID != nil {
+ return errors.New("cannot update both statement and sheet_id")
+ }
+
+ if taskPatch.Statement != nil || taskPatch.SheetID != nil {
if err := canUpdateTaskStatement(task); err != nil {
return err
}
@@ -416,7 +420,7 @@ func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, task
}
// Trigger task checks.
- if taskPatch.Statement != nil {
+ if taskPatch.Statement != nil || taskPatch.SheetID != nil {
dbSchema, err := s.store.GetDBSchema(ctx, *task.DatabaseID)
if err != nil {
return err
@@ -502,6 +506,9 @@ func (s *Scheduler) PatchTask(ctx context.Context, task *store.TaskMessage, task
}
}
+ // TODO(p0ny): sheet, create update sheet activity
+ // if taskPatch.SheetID != nil {}
+
// Update statement activity.
if taskPatch.Statement != nil {
oldStatement, err := utils.GetTaskStatement(task.Payload)
diff --git a/backend/server/project.go b/backend/server/project.go
index 06653e7125..84f417fa13 100644
--- a/backend/server/project.go
+++ b/backend/server/project.go
@@ -927,6 +927,8 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
sheetSource = api.SheetFromGitLab
case vcsPlugin.GitHub:
sheetSource = api.SheetFromGitHub
+ case vcsPlugin.Bitbucket:
+ sheetSource = api.SheetFromBitbucket
}
vscSheetType := api.SheetForSQL
sheetFind := &api.SheetFind{
diff --git a/backend/server/webhook.go b/backend/server/webhook.go
index 2a91b624e7..d2496c3133 100644
--- a/backend/server/webhook.go
+++ b/backend/server/webhook.go
@@ -1043,6 +1043,8 @@ func (s *Server) createIssueFromMigrationDetailList(ctx context.Context, issueNa
return echo.NewHTTPError(http.StatusInternalServerError, errMsg).SetInternal(err)
}
+ // TODO(p0ny): sheet, for each sheet, update the payload to backtrace the issue.
+
// Create a project activity after successfully creating the issue from the push event.
activityPayload, err := json.Marshal(
api.ActivityProjectRepositoryPushPayload{
@@ -1222,12 +1224,26 @@ func (s *Server) prepareIssueFromSDLFile(ctx context.Context, repo *api.Reposito
return nil, []*api.ActivityCreate{activityCreate}
}
+ sheet, err := s.store.CreateSheet(ctx, &api.SheetCreate{
+ CreatorID: api.SystemBotID,
+ ProjectID: repo.ProjectID,
+ Name: file,
+ Statement: sdl,
+ Visibility: api.ProjectSheet,
+ Source: api.SheetFromBytebase,
+ Type: api.SheetForSQL,
+ })
+ if err != nil {
+ activityCreate := getIgnoredFileActivityCreate(repo.ProjectID, pushEvent, file, errors.Wrap(err, "Failed to create a sheet"))
+ return nil, []*api.ActivityCreate{activityCreate}
+ }
+
var migrationDetailList []*api.MigrationDetail
if repo.Project.TenantMode == api.TenantModeTenant {
migrationDetailList = append(migrationDetailList,
&api.MigrationDetail{
MigrationType: db.MigrateSDL,
- Statement: sdl,
+ SheetID: sheet.ID,
},
)
return migrationDetailList, nil
@@ -1244,7 +1260,7 @@ func (s *Server) prepareIssueFromSDLFile(ctx context.Context, repo *api.Reposito
&api.MigrationDetail{
MigrationType: db.MigrateSDL,
DatabaseID: database.UID,
- Statement: sdl,
+ SheetID: sheet.ID,
},
)
}
@@ -1270,10 +1286,24 @@ func (s *Server) prepareIssueFromFile(ctx context.Context, repo *api.Repository,
if repo.Project.TenantMode == api.TenantModeTenant {
// A non-YAML file means the whole file content is the SQL statement
if !fileInfo.item.IsYAML {
+ sheet, err := s.store.CreateSheet(ctx, &api.SheetCreate{
+ CreatorID: api.SystemBotID,
+ ProjectID: repo.ProjectID,
+ Name: fileInfo.item.FileName,
+ Statement: content,
+ Visibility: api.ProjectSheet,
+ Source: api.SheetFromBytebase,
+ Type: api.SheetForSQL,
+ })
+ if err != nil {
+ activityCreate := getIgnoredFileActivityCreate(repo.ProjectID, pushEvent, fileInfo.item.FileName, errors.Wrap(err, "Failed to create a sheet"))
+ return nil, []*api.ActivityCreate{activityCreate}
+ }
+
return []*api.MigrationDetail{
{
MigrationType: fileInfo.migrationInfo.Type,
- Statement: content,
+ SheetID: sheet.ID,
SchemaVersion: fileInfo.migrationInfo.Version,
},
}, nil
@@ -1292,6 +1322,20 @@ func (s *Server) prepareIssueFromFile(ctx context.Context, repo *api.Repository,
}
}
+ sheet, err := s.store.CreateSheet(ctx, &api.SheetCreate{
+ CreatorID: api.SystemBotID,
+ ProjectID: repo.ProjectID,
+ Name: fileInfo.item.FileName,
+ Statement: migrationFile.Statement,
+ Visibility: api.ProjectSheet,
+ Source: api.SheetFromBytebase,
+ Type: api.SheetForSQL,
+ })
+ if err != nil {
+ activityCreate := getIgnoredFileActivityCreate(repo.ProjectID, pushEvent, fileInfo.item.FileName, errors.Wrap(err, "Failed to create a sheet"))
+ return nil, []*api.ActivityCreate{activityCreate}
+ }
+
var migrationDetailList []*api.MigrationDetail
for _, database := range migrationFile.Databases {
dbList, err := s.findProjectDatabases(ctx, repo.ProjectID, database.Name, "")
@@ -1311,7 +1355,7 @@ func (s *Server) prepareIssueFromFile(ctx context.Context, repo *api.Repository,
&api.MigrationDetail{
MigrationType: fileInfo.migrationInfo.Type,
DatabaseID: db.UID,
- Statement: migrationFile.Statement,
+ SheetID: sheet.ID,
SchemaVersion: fileInfo.migrationInfo.Version,
},
)
@@ -1328,13 +1372,27 @@ func (s *Server) prepareIssueFromFile(ctx context.Context, repo *api.Repository,
}
if fileInfo.item.ItemType == vcs.FileItemTypeAdded {
+ sheet, err := s.store.CreateSheet(ctx, &api.SheetCreate{
+ CreatorID: api.SystemBotID,
+ ProjectID: repo.ProjectID,
+ Name: fileInfo.item.FileName,
+ Statement: content,
+ Visibility: api.ProjectSheet,
+ Source: api.SheetFromBytebase,
+ Type: api.SheetForSQL,
+ })
+ if err != nil {
+ activityCreate := getIgnoredFileActivityCreate(repo.ProjectID, pushEvent, fileInfo.item.FileName, errors.Wrap(err, "Failed to create a sheet"))
+ return nil, []*api.ActivityCreate{activityCreate}
+ }
+
var migrationDetailList []*api.MigrationDetail
for _, database := range databases {
migrationDetailList = append(migrationDetailList,
&api.MigrationDetail{
MigrationType: fileInfo.migrationInfo.Type,
DatabaseID: database.UID,
- Statement: content,
+ SheetID: sheet.ID,
SchemaVersion: fileInfo.migrationInfo.Version,
},
)
@@ -1356,6 +1414,7 @@ func (s *Server) prepareIssueFromFile(ctx context.Context, repo *api.Repository,
}
func (s *Server) tryUpdateTasksFromModifiedFile(ctx context.Context, databases []*store.DatabaseMessage, fileName, schemaVersion, statement string) error {
+ // TODO(p0ny): sheet, create new sheets and update task sheet id.
// For modified files, we try to update the existing issue's statement.
for _, database := range databases {
find := &api.TaskFind{
@@ -1385,11 +1444,25 @@ func (s *Server) tryUpdateTasksFromModifiedFile(ctx context.Context, databases [
log.Error("issue not found by pipeline ID", zap.Int("pipeline ID", task.PipelineID), zap.Error(err))
return nil
}
+
+ sheet, err := s.store.CreateSheet(ctx, &api.SheetCreate{
+ CreatorID: api.SystemBotID,
+ ProjectID: issue.Project.UID,
+ Name: fileName,
+ Statement: statement,
+ Visibility: api.ProjectSheet,
+ Source: api.SheetFromBytebase,
+ Type: api.SheetForSQL,
+ })
+ if err != nil {
+ return err
+ }
+
// TODO(dragonly): Try to patch the failed migration history record to pending, and the statement to the current modified file content.
log.Debug("Patching task for modified file VCS push event", zap.String("fileName", fileName), zap.Int("issueID", issue.UID), zap.Int("taskID", task.ID))
taskPatch := api.TaskPatch{
ID: task.ID,
- Statement: &statement,
+ SheetID: &sheet.ID,
UpdaterID: api.SystemBotID,
}
if err := s.TaskScheduler.PatchTask(ctx, task, &taskPatch, issue); err != nil {
diff --git a/backend/store/task.go b/backend/store/task.go
index c5d9077765..98587d30c6 100644
--- a/backend/store/task.go
+++ b/backend/store/task.go
@@ -435,8 +435,8 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *api.TaskPatch) (*TaskMe
if v := patch.DatabaseID; v != nil {
set, args = append(set, fmt.Sprintf("database_id = $%d", len(args)+1)), append(args, *v)
}
- if (patch.Statement != nil || patch.SchemaVersion != nil) && patch.Payload != nil {
- return nil, errors.Errorf("cannot set both statement/schemaVersion and payload for TaskPatch")
+ if (patch.Statement != nil || patch.SchemaVersion != nil || patch.SheetID != nil) && patch.Payload != nil {
+ return nil, errors.Errorf("cannot set both statement/sheetID/schemaVersion and payload for TaskPatch")
}
if (patch.RollbackEnabled != nil || patch.RollbackSQLStatus != nil || patch.RollbackStatement != nil || patch.RollbackError != nil) && patch.Payload != nil {
return nil, errors.Errorf("cannot set both rollbackEnabled/rollbackSQLStatus/rollbackStatement/rollbackError payload for TaskPatch")
@@ -445,6 +445,9 @@ func (s *Store) UpdateTaskV2(ctx context.Context, patch *api.TaskPatch) (*TaskMe
if v := patch.Statement; v != nil {
payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('statement', to_jsonb($%d::TEXT))`, len(args)+1)), append(args, *v)
}
+ if v := patch.SheetID; v != nil {
+ payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('sheetId', to_jsonb($%d::INT))`, len(args)+1)), append(args, *v)
+ }
if v := patch.SchemaVersion; v != nil {
payloadSet, args = append(payloadSet, fmt.Sprintf(`jsonb_build_object('schemaVersion', to_jsonb($%d::TEXT))`, len(args)+1)), append(args, *v)
}