Skip to content

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

Extract the method to write back the latest schema to the repository after migration

天舟fe9285a4994b
diff --git a/server/task_executor_schema_update.go b/server/task_executor_schema_update.go
index 8b3f6cf4bb..adddf51750 100644
--- a/server/task_executor_schema_update.go
+++ b/server/task_executor_schema_update.go
@@ -11,6 +11,7 @@ import (
"strings"
"github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/external/gitlab"
"github.com/bytebase/bytebase/plugin/db"
"go.uber.org/zap"
@@ -153,115 +154,123 @@ func (exec *SchemaUpdateTaskExecutor) RunOnce(ctx context.Context, server *Serve
// If VCS based, then we will write back the latest schema file after migration.
if payload.VCSPushEvent != nil {
+ latestSchemaFile := fmt.Sprintf("%s__#LATEST.sql", databaseName)
+ repository.VCS, err = server.ComposeVCSById(context.Background(), repository.VCSId)
+ if err != nil {
+ return true, "", fmt.Errorf("failed to sync schema file %s after applying migration %s to %q", latestSchemaFile, mi.Version, databaseName)
+ }
+
bytebaseURL := ""
if issue != nil {
bytebaseURL = fmt.Sprintf("%s:%d/issue/%s?stage=%d", server.frontendHost, server.frontendPort, api.IssueSlug(issue), task.StageId)
}
- // Writes back the latest schema file to the same branch as the push event.
- // Ref format refs/heads/<<branch>>
- refComponents := strings.Split(payload.VCSPushEvent.Ref, "/")
- branch := refComponents[len(refComponents)-1]
- latestSchemaFile := fmt.Sprintf("%s__#LATEST.sql", databaseName)
- filePath := fmt.Sprintf("projects/%s/repository/files/%s", repository.ExternalId, url.QueryEscape(latestSchemaFile))
- getFilePath := filePath + "?ref=" + url.QueryEscape(branch)
-
- repository.VCS, err = server.ComposeVCSById(context.Background(), repository.VCSId)
- if err != nil {
- return true, "", fmt.Errorf("failed to sync schema file %s after applying migration %s to %q", filePath, mi.Version, databaseName)
+ if err := writeBackLatestSchema(server, repository, payload.VCSPushEvent, mi, latestSchemaFile, schema, bytebaseURL); err != nil {
+ return true, "", err
}
+ }
+
+ detail = fmt.Sprintf("Applied migration version %s to database %q", mi.Version, databaseName)
+ if mi.Type == db.Baseline {
+ detail = fmt.Sprintf("Established baseline version %s for database %q", mi.Version, databaseName)
+ }
+
+ return true, detail, nil
+}
+
+// Writes back the latest schema to the repository after migration
+func writeBackLatestSchema(server *Server, repository *api.Repository, pushEvent *common.VCSPushEvent, mi *db.MigrationInfo, latestSchemaFile string, schema string, bytebaseURL string) error {
+ // Writes back the latest schema file to the same branch as the push event.
+ // Ref format refs/heads/<<branch>>
+ refComponents := strings.Split(pushEvent.Ref, "/")
+ branch := refComponents[len(refComponents)-1]
+ filePath := fmt.Sprintf("projects/%s/repository/files/%s", repository.ExternalId, url.QueryEscape(latestSchemaFile))
+ getFilePath := filePath + "?ref=" + url.QueryEscape(branch)
+
+ getResp, err := gitlab.GET(
+ repository.VCS.InstanceURL,
+ getFilePath,
+ repository.AccessToken,
+ )
+ if err != nil {
+ return fmt.Errorf("failed to fetch latest schema file from %s, err: %w", repository.VCS.InstanceURL, err)
+ }
+ defer getResp.Body.Close()
- getResp, err := gitlab.GET(
+ createSchemaFile := false
+ verb := "Update"
+ if getResp.StatusCode >= 300 && getResp.StatusCode != 404 {
+ return fmt.Errorf("failed to fetch latest schema file from %s, status code: %d",
repository.VCS.InstanceURL,
- getFilePath,
- repository.AccessToken,
+ getResp.StatusCode,
)
+ } else if getResp.StatusCode == 404 {
+ createSchemaFile = true
+ verb = "Create"
+ }
+
+ commitTitle := fmt.Sprintf("[Bytebase] %s latest schema for %q after migration %s", verb, mi.Database, mi.Version)
+ commitBody := "THIS COMMIT IS AUTO-GENERATED BY BTYEBASE"
+ if bytebaseURL != "" {
+ commitBody += "\n\n" + bytebaseURL
+ }
+ commitBody += "\n\n--------Original migration change--------\n\n"
+ commitBody += fmt.Sprintf("%s\n\n%s",
+ pushEvent.FileCommit.URL,
+ pushEvent.FileCommit.Message,
+ )
+
+ schemaFileCommit := gitlab.FileCommit{
+ Branch: branch,
+ CommitMessage: fmt.Sprintf("%s\n\n%s", commitTitle, commitBody),
+ Content: schema,
+ }
+ if createSchemaFile {
+ body, err := json.Marshal(schemaFileCommit)
if err != nil {
- return true, "", fmt.Errorf("failed to fetch latest schema file from %s, err: %w", repository.VCS.InstanceURL, err)
- }
- defer getResp.Body.Close()
-
- createSchemaFile := false
- verb := "Update"
- if getResp.StatusCode >= 300 && getResp.StatusCode != 404 {
- return true, "", fmt.Errorf("failed to fetch latest schema file from %s, status code: %d",
- repository.VCS.InstanceURL,
- getResp.StatusCode,
- )
- } else if getResp.StatusCode == 404 {
- createSchemaFile = true
- verb = "Create"
+ return fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
}
- commitTitle := fmt.Sprintf("[Bytebase] %s latest schema for %q after migration %s", verb, databaseName, mi.Version)
- commitBody := "THIS COMMIT IS AUTO-GENERATED BY BTYEBASE"
- if bytebaseURL != "" {
- commitBody += "\n\n" + bytebaseURL
+ resp, err := gitlab.POST(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
+ if err != nil {
+ return fmt.Errorf("failed to create file %s after applying migration %s to %q, err: %w", filePath, mi.Version, mi.Database, err)
}
- commitBody += "\n\n--------Original migration change--------\n\n"
- commitBody += fmt.Sprintf("%s\n\n%s",
- payload.VCSPushEvent.FileCommit.URL,
- payload.VCSPushEvent.FileCommit.Message,
- )
+ defer resp.Body.Close()
- schemaFileCommit := gitlab.FileCommit{
- Branch: branch,
- CommitMessage: fmt.Sprintf("%s\n\n%s", commitTitle, commitBody),
- Content: schema,
+ if resp.StatusCode >= 300 {
+ return fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
+ filePath,
+ mi.Version,
+ mi.Database,
+ resp.StatusCode,
+ )
+ }
+ } else {
+ file := &gitlab.File{}
+ if err := json.NewDecoder(getResp.Body).Decode(file); err != nil {
+ return fmt.Errorf("failed to unmarshal file response %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
}
- if createSchemaFile {
- body, err := json.Marshal(schemaFileCommit)
- if err != nil {
- return true, "", fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, databaseName)
- }
-
- resp, err := gitlab.POST(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
- if err != nil {
- return true, "", fmt.Errorf("failed to create file %s after applying migration %s to %q, err: %w", filePath, mi.Version, databaseName, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode >= 300 {
- return true, "", fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
- filePath,
- mi.Version,
- databaseName,
- resp.StatusCode,
- )
- }
- } else {
- file := &gitlab.File{}
- if err := json.NewDecoder(getResp.Body).Decode(file); err != nil {
- return true, "", fmt.Errorf("failed to unmarshal file response %s after applying migration %s to %q", filePath, mi.Version, databaseName)
- }
- schemaFileCommit.LastCommitId = file.LastCommitId
- body, err := json.Marshal(schemaFileCommit)
- if err != nil {
- return true, "", fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, databaseName)
- }
+ schemaFileCommit.LastCommitId = file.LastCommitId
+ body, err := json.Marshal(schemaFileCommit)
+ if err != nil {
+ return fmt.Errorf("failed to marshal file request %s after applying migration %s to %q", filePath, mi.Version, mi.Database)
+ }
- resp, err := gitlab.PUT(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
- if err != nil {
- return true, "", fmt.Errorf("failed to create file %s after applying migration %s to %q, error: %w", filePath, mi.Version, databaseName, err)
- }
- defer resp.Body.Close()
-
- if resp.StatusCode >= 300 {
- return true, "", fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
- filePath,
- mi.Version,
- databaseName,
- resp.StatusCode,
- )
- }
+ resp, err := gitlab.PUT(repository.VCS.InstanceURL, filePath, repository.AccessToken, bytes.NewBuffer(body))
+ if err != nil {
+ return fmt.Errorf("failed to create file %s after applying migration %s to %q, error: %w", filePath, mi.Version, mi.Database, err)
}
- }
+ defer resp.Body.Close()
- detail = fmt.Sprintf("Applied migration version %s to database %q", mi.Version, databaseName)
- if mi.Type == db.Baseline {
- detail = fmt.Sprintf("Established baseline version %s for database %q", mi.Version, databaseName)
+ if resp.StatusCode >= 300 {
+ return fmt.Errorf("failed to create file %s after applying migration %s to %q, status code: %d",
+ filePath,
+ mi.Version,
+ mi.Database,
+ resp.StatusCode,
+ )
+ }
}
-
- return true, detail, nil
+ return nil
}