Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(tenancy): implement pipeline generation from update schema issue context in backend (#194)
spinningbot6646b53d6104
diff --git a/api/issue.go b/api/issue.goindex ee9050749d..e6c6719f7b 100644--- a/api/issue.go+++ b/api/issue.go@@ -2,6 +2,8 @@ package apiimport ("context"++ "github.com/bytebase/bytebase/plugin/db")// IssueStatus is the status of an issue.@@ -126,6 +128,23 @@ type CreateDatabaseContext struct {BackupName string `json:"backupName"`}+// UpdateSchemaDetail is the detail of updating database schema.+type UpdateSchemaDetail struct {+ // DatabaseId is the ID of a database.+ DatabaseID int `json:"databaseId"`+ // Statement is the statement to update database schema.+ Statement string `json:"statement"`+ // Statement is the rollback statement of the statement.+ RollbackStatement string `json:"rollbackStatement"`+}++// UpdateSchemaContext is the issue create context for updating database schema.+type UpdateSchemaContext struct {+ // MigrationType is the type of a migration.+ MigrationType db.MigrationType `json:"migrationType"`+ UpdateSchemaDetailList []*UpdateSchemaDetail `json:"updateSchemaDetailList"`+}+// IssueFind is the API message for finding issues.type IssueFind struct {ID *intdiff --git a/frontend/src/types/issue.ts b/frontend/src/types/issue.tsindex 508aabc25a..d0e2f50fb4 100644--- a/frontend/src/types/issue.ts+++ b/frontend/src/types/issue.ts@@ -1,7 +1,8 @@-import { IssueId, PrincipalId, ProjectId } from "./id";+import { BackupId, DatabaseId, InstanceId, IssueId, PrincipalId, ProjectId } from "./id";import { Pipeline, PipelineCreate } from "./pipeline";import { Principal } from "./principal";import { Project } from "./project";+import { MigrationType } from "./instance";type IssueTypeGeneral = "bb.issue.general";@@ -19,7 +20,33 @@ export type IssueType =export type IssueStatus = "OPEN" | "DONE" | "CANCELED";-export type IssueCreateContext = { [key: string]: any };+export type CreateDatabaseContext = {+ instanceId: InstanceId;+ databaseName: string;+ characterSet: string;+ collation: string;+ backupId: BackupId;+ backupName: string;+};++export type UpdateSchemaDetail = {+ databaseId: DatabaseId;+ statement: string;+ rollbackStatement: string;+};++export type UpdateSchemaContext = {+ migrationType: MigrationType;+ updateSchemaDetailList: UpdateSchemaDetail[];+};++export type EmptyContext = {+};++export type IssueCreateContext =+ | CreateDatabaseContext+ | UpdateSchemaContext+ | EmptyContext;export type IssuePayload = { [key: string]: any };diff --git a/server/issue.go b/server/issue.goindex 038849d1e5..18e6735f6a 100644--- a/server/issue.go+++ b/server/issue.go@@ -339,7 +339,8 @@ func (s *Server) composeIssueRelationship(ctx context.Context, issue *api.Issue)}func (s *Server) createIssue(ctx context.Context, issueCreate *api.IssueCreate, creatorID int) (*api.Issue, error) {- if issueCreate.Type == api.IssueDatabaseCreate {+ // If frontend does not pass the stageList, we will generate it from backend.+ if len(issueCreate.Pipeline.StageList) == 0 {pc, err := s.getPipelineFromIssue(ctx, issueCreate)if err != nil {return nil, err@@ -602,8 +603,60 @@ func (s *Server) getPipelineFromIssue(ctx context.Context, issueCreate *api.Issu},},}, nil+ case api.IssueDatabaseSchemaUpdate:+ m := api.UpdateSchemaContext{}+ if err := json.Unmarshal([]byte(issueCreate.CreateContext), &m); err != nil {+ return nil, err+ }+ pc := &api.PipelineCreate{}+ switch m.MigrationType {+ case db.Baseline:+ pc.Name = "Establish database baseline pipeline"+ case db.Migrate:+ pc.Name = "Update database schema pipeline"+ default:+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid migration type %q", m.MigrationType))+ }++ for _, d := range m.UpdateSchemaDetailList {+ if m.MigrationType == db.Migrate && 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 {+ if common.ErrorCode(err) == common.NotFound {+ return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Database ID not found: %d", d.DatabaseID))+ }+ return nil, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch database ID: %v", d.DatabaseID)).SetInternal(err)+ }++ taskName := fmt.Sprintf("Establish %q baseline", database.Name)+ if m.MigrationType == db.Migrate {+ taskName = fmt.Sprintf("Update %q schema", database.Name)+ }+ pc.StageList = append(pc.StageList, api.StageCreate{+ Name: fmt.Sprintf("%s %s", database.Instance.Environment.Name, database.Name),+ EnvironmentID: database.Instance.Environment.ID,+ TaskList: []api.TaskCreate{+ {+ Name: taskName,+ InstanceID: database.Instance.ID,+ DatabaseID: &database.ID,+ Status: api.TaskPendingApproval,+ Type: api.TaskDatabaseSchemaUpdate,+ Statement: d.Statement,+ RollbackStatement: d.RollbackStatement,+ MigrationType: m.MigrationType,+ },+ },+ })+ }+ return pc, nil}- return nil, nil+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid issue type %q", issueCreate.Type))}func getDatabaseNameAndStatement(dbType db.Type, databaseName, characterSet, collation string) (string, string) {