Skip to content

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

feat(vcs): write the latest schema back to the same branch as the original commit triggering the migration (#4313)

h3n4l2ae57d9160f9
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.go
index 3c934551aa..2a5e04502c 100644
--- a/plugin/vcs/github/github.go
+++ b/plugin/vcs/github/github.go
@@ -1402,6 +1402,11 @@ func (p *Provider) DeleteWebhook(ctx context.Context, oauthCtx common.OauthConte
return nil
}
+// GetBranchNameFromRef returns the branch name from the refs.
+func (*Provider) GetBranchNameFromRef(ref string) string {
+ return strings.TrimPrefix(ref, "refs/heads/")
+}
+
// oauthContext is the request context for refreshing oauth token.
type oauthContext struct {
ClientID string `json:"client_id"`
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 7789d8e046..eda585524b 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -1383,6 +1383,11 @@ func (p *Provider) readFile(ctx context.Context, oauthCtx common.OauthContext, i
}, nil
}
+// GetBranchNameFromRef returns the branch name from the refs.
+func (*Provider) GetBranchNameFromRef(ref string) string {
+ return strings.TrimPrefix(ref, "refs/heads/")
+}
+
// oauthContext is the request context for refreshing oauth token.
type oauthContext struct {
ClientID string `json:"client_id"`
diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.go
index cb6d2e2759..6640ac0d83 100644
--- a/plugin/vcs/vcs.go
+++ b/plugin/vcs/vcs.go
@@ -321,6 +321,8 @@ type Provider interface {
PatchWebhook(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, webhookID string, payload []byte) error
// Deletes a webhook.
DeleteWebhook(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, webhookID string) error
+ // GetBranchNameFromRef gets the branch name from the refs.
+ GetBranchNameFromRef(ref string) string
}
var (
diff --git a/server/project.go b/server/project.go
index 7158ec8f62..7f1c47bd96 100644
--- a/server/project.go
+++ b/server/project.go
@@ -259,11 +259,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Branch must be specified.")
}
- hasWildcard := strings.Contains(repositoryCreate.BranchFilter, "*")
- if hasWildcard && repositoryCreate.SchemaPathTemplate != "" {
- return echo.NewHTTPError(http.StatusBadRequest, "Schema path template is supported only if branch doesn't have wildcard.")
- }
-
// We need to check the FilePathTemplate in create repository request.
// This avoids to a certain extent that the creation succeeds but does not work.
if err := vcsPlugin.IsAsterisksInTemplateValid(path.Join(repositoryCreate.BaseDirectory, repositoryCreate.FilePathTemplate)); err != nil {
@@ -287,7 +282,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
// When the branch names doesn't contain wildcards, we should make sure the branch exists in the repo.
- if !hasWildcard {
+ if !strings.Contains(repositoryCreate.BranchFilter, "*") {
if notFound, err := isBranchNotFound(ctx, vcs, repositoryCreate.AccessToken, repositoryCreate.RefreshToken, repositoryCreate.ExternalID, repositoryCreate.BranchFilter); notFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Branch %q not found in repository %s.", repositoryCreate.BranchFilter, repositoryCreate.Name)).SetInternal(err)
} else if err != nil {
@@ -491,10 +486,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
repo := repoList[0]
repoPatch.ID = &repo.ID
- newSchemaPathTemplate, newBranchFilter := repo.SchemaPathTemplate, repo.BranchFilter
- if repoPatch.SchemaPathTemplate != nil {
- newSchemaPathTemplate = *repoPatch.SchemaPathTemplate
- }
+ newBranchFilter := repo.BranchFilter
if repoPatch.BranchFilter != nil {
newBranchFilter = *repoPatch.BranchFilter
}
@@ -502,11 +494,6 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Branch must be specified.")
}
- hasWildcard := strings.Contains(newBranchFilter, "*")
- if hasWildcard && newSchemaPathTemplate != "" {
- return echo.NewHTTPError(http.StatusBadRequest, "Schema path template is supported only if branch doesn't have wildcard.")
- }
-
vcs, err := s.store.GetVCSByID(ctx, repo.VCSID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to find VCS for creating repository: %d", repo.VCSID)).SetInternal(err)
@@ -516,7 +503,7 @@ func (s *Server) registerProjectRoutes(g *echo.Group) {
}
// When the branch names doesn't contain wildcards, we should make sure the branch exists in the repo.
- if !hasWildcard {
+ if !strings.Contains(newBranchFilter, "*") {
if notFound, err := isBranchNotFound(ctx, vcs, repo.AccessToken, repo.RefreshToken, repo.ExternalID, newBranchFilter); notFound {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Branch %q not found in repository %s.", newBranchFilter, repo.Name)).SetInternal(err)
} else if err != nil {
diff --git a/server/runner/taskrun/executor.go b/server/runner/taskrun/executor.go
index ea0dac5eae..3263c0e19d 100644
--- a/server/runner/taskrun/executor.go
+++ b/server/runner/taskrun/executor.go
@@ -297,13 +297,13 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
return true, nil, errors.Errorf("failed to update database %q for instance %q", databaseName, task.Database.Instance.Name)
}
}
- // On the presence of schema path template and non-wildcard branch filter, We write back the latest schema after migration for VCS-based projects for
- // 1) baseline migration for SDL,
+ // On the presence of schema path template We write back the latest schema after migration for VCS-based projects for
+ // 1) Non-wildcard branch filter and baseline migration for SDL.
// 2) all DDL/Ghost migrations.
writeBack := false
- if repo != nil && repo.SchemaPathTemplate != "" && !strings.Contains(repo.BranchFilter, "*") {
+ if repo != nil && repo.SchemaPathTemplate != "" {
if repo.Project.SchemaChangeType == api.ProjectSchemaChangeTypeSDL {
- if task.Type == api.TaskDatabaseSchemaBaseline {
+ if task.Type == api.TaskDatabaseSchemaBaseline && !strings.Contains(repo.BranchFilter, "*") {
writeBack = true
// Transform the schema to standard style for SDL mode.
if task.Database.Instance.Engine == db.MySQL {
@@ -364,7 +364,7 @@ func postMigration(ctx context.Context, stores *store.Store, activityManager *ac
bytebaseURL = fmt.Sprintf("%s/issue/%s?stage=%d", profile.ExternalURL, api.IssueSlug(issue), task.StageID)
}
- commitID, err := writeBackLatestSchema(ctx, stores, repo, vcsPushEvent, mi, repo.BranchFilter, latestSchemaFile, schema, bytebaseURL)
+ commitID, err := writeBackLatestSchema(ctx, stores, repo, vcsPushEvent, mi, latestSchemaFile, schema, bytebaseURL)
if err != nil {
return true, nil, err
}
@@ -460,7 +460,7 @@ func findIssueByTask(ctx context.Context, store *store.Store, task *api.Task) (*
// Writes back the latest schema to the repository after migration
// Returns the commit id on success.
-func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *api.Repository, pushEvent *vcsPlugin.PushEvent, mi *db.MigrationInfo, branch string, latestSchemaFile string, schema string, bytebaseURL string) (string, error) {
+func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *api.Repository, pushEvent *vcsPlugin.PushEvent, mi *db.MigrationInfo, latestSchemaFile string, schema string, bytebaseURL string) (string, error) {
schemaFileMeta, err := vcsPlugin.Get(repository.VCS.Type, vcsPlugin.ProviderConfig{}).ReadFileMeta(
ctx,
common.OauthContext{
@@ -473,7 +473,7 @@ func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *
repository.VCS.InstanceURL,
repository.ExternalID,
latestSchemaFile,
- branch,
+ pushEvent.Ref,
)
createSchemaFile := false
@@ -525,7 +525,7 @@ func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *
}
schemaFileCommit := vcsPlugin.FileCommitCreate{
- Branch: branch,
+ Branch: vcsPlugin.Get(repo2.VCS.Type, vcsPlugin.ProviderConfig{}).GetBranchNameFromRef(pushEvent.Ref),
CommitMessage: commitMessage,
Content: schema,
}
@@ -599,7 +599,7 @@ func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *
repo2.VCS.InstanceURL,
repo2.ExternalID,
latestSchemaFile,
- branch,
+ pushEvent.Ref,
)
if err != nil {