Skip to content

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

feat(gh-ost): implement creation for issue type gh-ost migration non-tenant mode (#1134)

p0nye4b0ef9611d6
diff --git a/api/stage.go b/api/stage.go
index 4deec39851..5831620b23 100644
--- a/api/stage.go
+++ b/api/stage.go
@@ -75,9 +75,10 @@ type StageCreate struct {
CreatorID int
// Related fields
- EnvironmentID int `jsonapi:"attr,environmentId"`
- PipelineID int
- TaskList []TaskCreate `jsonapi:"attr,taskList"`
+ EnvironmentID int `jsonapi:"attr,environmentId"`
+ PipelineID int
+ TaskList []TaskCreate `jsonapi:"attr,taskList"`
+ TaskIndexDAGList []TaskIndexDAG `jsonapi:"attr,taskDAGList"`
// Domain specific fields
Name string `jsonapi:"attr,name"`
diff --git a/api/task.go b/api/task.go
index 6b2e30ce88..611bd44b83 100644
--- a/api/task.go
+++ b/api/task.go
@@ -108,8 +108,8 @@ type TaskDatabaseSchemaUpdateGhostSyncPayload struct {
VCSPushEvent *vcs.PushEvent `json:"pushEvent,omitempty"`
// SocketFileName is the socket file that gh-ost listens on.
// The name follows this template,
- // `./tmp/gh-ost.{{ISSUE_ID}}.{{TASK_ID}}.{{DATABASE_ID}}.{{DATABASE_NAME}}.{{TABLE_NAME}}.{{TIMESTAMP}}.sock`
- SocketFileName string
+ // `./tmp/gh-ost.{{ISSUE_ID}}.{{TASK_ID}}.{{DATABASE_ID}}.{{DATABASE_NAME}}.{{TABLE_NAME}}.sock`
+ // SocketFileName will be composed when needed. We don't store it explicitly.
}
// TaskDatabaseSchemaUpdateGhostCutoverPayload is the task payload for gh-ost switching the original table and the ghost table.
diff --git a/api/task_dag.go b/api/task_dag.go
index 2ae34db682..ed11329ed8 100644
--- a/api/task_dag.go
+++ b/api/task_dag.go
@@ -18,6 +18,7 @@ type TaskDAG struct {
// TaskDAGCreate is the API message to create TaskDAG.
type TaskDAGCreate struct {
+ // Domain Specific fields
FromTaskID int
ToTaskID int
Payload string
@@ -27,3 +28,10 @@ type TaskDAGCreate struct {
type TaskDAGFind struct {
ToTaskID int
}
+
+// TaskIndexDAG describes task dependency relationship using array index to represent task.
+// It is needed because we don't know task id before insertion.
+type TaskIndexDAG struct {
+ FromIndex int
+ ToIndex int
+}
diff --git a/server/issue.go b/server/issue.go
index 0a6e7602db..8e88172acc 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/vcs"
+ ghostsql "github.com/github/gh-ost/go/sql"
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
)
@@ -513,8 +514,21 @@ func (s *Server) createPipelineValidateOnly(ctx context.Context, pc *api.Pipelin
PipelineID: sc.PipelineID,
EnvironmentID: sc.EnvironmentID,
}
- for _, tc := range sc.TaskList {
+ // We don't know IDs before inserting, so we use array index instead.
+ // indexBlockedByIndex[indexA] holds indexes of the tasks that block taskList[indexA]
+ indexBlockedByIndex := make(map[int][]int)
+ for _, indexDAG := range sc.TaskIndexDAGList {
+ indexBlockedByIndex[indexDAG.ToIndex] = append(indexBlockedByIndex[indexDAG.ToIndex], indexDAG.FromIndex)
+ }
+ idOffset := id + 1
+ // The ID of sc.TaskList[index].ID equals index + idOffset.
+ for index, tc := range sc.TaskList {
id++
+ var blockedBy []string
+ for _, blockedByIndex := range indexBlockedByIndex[index] {
+ // Convert array index to ID.
+ blockedBy = append(blockedBy, strconv.Itoa(blockedByIndex+idOffset))
+ }
taskRaw := &api.TaskRaw{
ID: id,
Name: tc.Name,
@@ -532,6 +546,8 @@ func (s *Server) createPipelineValidateOnly(ctx context.Context, pc *api.Pipelin
DatabaseID: tc.DatabaseID,
}
task, err := s.composeTaskRelationship(ctx, taskRaw)
+ // We need to compose task.BlockedBy here because task and taskDAG are not inserted yet.
+ task.BlockedBy = blockedBy
if err != nil {
return nil, err
}
@@ -882,6 +898,63 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
}
pipelineCreate = pc
}
+ case api.IssueDatabaseSchemaUpdateGhost:
+ m := api.UpdateSchemaGhostContext{}
+ if err := json.Unmarshal([]byte(issueCreate.CreateContext), &m); err != nil {
+ return nil, err
+ }
+ if !s.feature(api.FeatureTaskScheduleTime) {
+ for _, detail := range m.UpdateSchemaDetailList {
+ if detail.EarliestAllowedTs != 0 {
+ return nil, echo.NewHTTPError(http.StatusForbidden, api.FeatureTaskScheduleTime.AccessErrorMessage())
+ }
+ }
+ }
+ pc := &api.PipelineCreate{}
+ pc.Name = "Update database schema (gh-ost) pipeline"
+ project, err := s.composeProjectByID(ctx, issueCreate.ProjectID)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch project ID: %v", issueCreate.ProjectID)).SetInternal(err)
+ }
+ schemaVersion := common.DefaultMigrationVersion()
+ if project.TenantMode == api.TenantModeTenant {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "not implemented yet")
+ }
+ for _, d := range m.UpdateSchemaDetailList {
+ if d.Statement == "" {
+ return nil, echo.NewHTTPError(http.StatusBadRequest, "failed to create issue, sql statement missing")
+ }
+ databaseFind := &api.DatabaseFind{
+ ID: &d.DatabaseID,
+ }
+ database, err := s.composeDatabaseByFind(ctx, databaseFind)
+ if err != nil {
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to fetch database ID: %v", d.DatabaseID)).SetInternal(err)
+ }
+ if database == nil {
+ return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("database ID not found: %d", d.DatabaseID))
+ }
+ taskStatus := api.TaskPendingApproval
+ policy, err := s.store.GetPipelineApprovalPolicy(ctx, database.Instance.EnvironmentID)
+ if err != nil {
+ return nil, fmt.Errorf("failed to get approval policy for environment ID %v, error %w", database.Instance.EnvironmentID, err)
+ }
+ if policy.Value == api.PipelineApprovalValueManualNever {
+ taskStatus = api.TaskPending
+ }
+ taskCreateList, taskIndexDAGList, err := getUpdateGhostTaskList(database, m.VCSPushEvent, d, schemaVersion, taskStatus)
+ if err != nil {
+ return nil, err
+ }
+ pc.StageList = append(pc.StageList, api.StageCreate{
+ Name: fmt.Sprintf("%s%s", database.Instance.Environment.Name, database.Name),
+ EnvironmentID: database.Instance.Environment.ID,
+ TaskList: taskCreateList,
+ TaskIndexDAGList: taskIndexDAGList,
+ })
+ }
+ pipelineCreate = pc
+
default:
return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid issue type %q", issueCreate.Type))
}
@@ -918,14 +991,30 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
return nil, fmt.Errorf("failed to create stage for issue, error %v", err)
}
- for _, taskCreate := range stageCreate.TaskList {
+ taskID := make(map[int]int)
+
+ for index, taskCreate := range stageCreate.TaskList {
taskCreate.CreatorID = creatorID
taskCreate.PipelineID = pipelineRawCreated.ID
taskCreate.StageID = createdStage.ID
- if _, err = s.TaskService.CreateTask(ctx, &taskCreate); err != nil {
+ task, err := s.TaskService.CreateTask(ctx, &taskCreate)
+ if err != nil {
return nil, fmt.Errorf("failed to create task for issue, error %v", err)
}
+ taskID[index] = task.ID
+ }
+
+ for _, indexDAG := range stageCreate.TaskIndexDAGList {
+ taskDAGCreate := api.TaskDAGCreate{
+ FromTaskID: taskID[indexDAG.FromIndex],
+ ToTaskID: taskID[indexDAG.ToIndex],
+ Payload: "{}",
+ }
+ if _, err := s.store.CreateTaskDAG(ctx, &taskDAGCreate); err != nil {
+ return nil, fmt.Errorf("failed to create task DAG for issue, error %w", err)
+ }
}
+
}
pipelineCreated, err := s.composePipelineRelationship(ctx, pipelineRawCreated)
@@ -976,6 +1065,85 @@ func getUpdateTask(database *api.Database, migrationType db.MigrationType, vcsPu
}, nil
}
+func getUpdateGhostTaskList(database *api.Database, vcsPushEvent *vcs.PushEvent, d *api.UpdateSchemaDetail, schemaVersion string, taskStatus api.TaskStatus) ([]api.TaskCreate, []api.TaskIndexDAG, error) {
+ var taskCreateList []api.TaskCreate
+ {
+ payload := api.TaskDatabaseSchemaUpdateGhostSyncPayload{}
+ payload.Statement = d.Statement
+ payload.SchemaVersion = schemaVersion
+ if vcsPushEvent != nil {
+ payload.VCSPushEvent = vcsPushEvent
+ }
+ bytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to marshal database schema update gh-ost sync payload, error: %v", err))
+ }
+ taskCreateList = append(taskCreateList, api.TaskCreate{
+ Name: fmt.Sprintf("Update %q schema gh-ost sync", database.Name),
+ InstanceID: database.InstanceID,
+ DatabaseID: &database.ID,
+ Status: taskStatus,
+ Type: api.TaskDatabaseSchemaUpdateGhostSync,
+ Statement: d.Statement,
+ EarliestAllowedTs: d.EarliestAllowedTs,
+ MigrationType: db.Migrate,
+ Payload: string(bytes),
+ })
+ }
+ {
+ payload := api.TaskDatabaseSchemaUpdateGhostCutoverPayload{}
+ bytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to marshal database schema update ghost cutover payload, error: %v", err))
+ }
+ taskCreateList = append(taskCreateList, api.TaskCreate{
+ Name: fmt.Sprintf("Update %q schema gh-ost cutover", database.Name),
+ InstanceID: database.InstanceID,
+ DatabaseID: &database.ID,
+ Status: taskStatus,
+ Type: api.TaskDatabaseSchemaUpdateGhostCutover,
+ EarliestAllowedTs: d.EarliestAllowedTs,
+ Payload: string(bytes),
+ })
+ }
+ {
+ parser := ghostsql.NewParserFromAlterStatement(d.Statement)
+ tableName := ""
+ if parser.HasExplicitTable() {
+ tableName = fmt.Sprintf("_%v_del", parser.GetExplicitTable())
+ }
+ payload := api.TaskDatabaseSchemaUpdateGhostDropOriginalTablePayload{
+ DatabaseName: database.Name,
+ TableName: tableName,
+ }
+ bytes, err := json.Marshal(payload)
+ if err != nil {
+ return nil, nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("failed to marshal database schema update ghost drop original table payload, error: %v", err))
+ }
+ taskCreateList = append(taskCreateList, api.TaskCreate{
+ Name: "Update %q schema gh-ost drop original table",
+ InstanceID: database.Instance.ID,
+ DatabaseID: &database.ID,
+ Status: taskStatus,
+ Type: api.TaskDatabaseSchemaUpdateGhostDropOriginalTable,
+ EarliestAllowedTs: d.EarliestAllowedTs,
+ Payload: string(bytes),
+ })
+ }
+ // The below list means that taskCreateList[0] blocks taskCreateList[1], taskCreateList[1] blocks taskCreateList[2], which makes sense because the "sync" task blocks the "cutover" task, and the "drop original table" task can only be done after "cutover".
+ taskIndexDAGList := []api.TaskIndexDAG{
+ {
+ FromIndex: 0,
+ ToIndex: 1,
+ },
+ {
+ FromIndex: 1,
+ ToIndex: 2,
+ },
+ }
+ return taskCreateList, taskIndexDAGList, nil
+}
+
func getDatabaseNameAndStatement(dbType db.Type, databaseName, characterSet, collation, schema string) (string, string) {
// Snowflake needs to use upper case of DatabaseName.
if dbType == db.Snowflake {