Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 8 additions & 79 deletions internal/cmd/branch/promote.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package branch

import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
Expand All @@ -15,7 +11,7 @@ import (
)

func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
promoteReq := &ps.RequestPromotionRequest{}
promoteReq := &ps.PromoteRequest{}

cmd := &cobra.Command{
Use: "promote <database> <branch> [options]",
Expand Down Expand Up @@ -56,10 +52,10 @@ func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
return candidates, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
source := args[0]
db := args[0]
branch := args[1]

promoteReq.Database = source
promoteReq.Database = db
promoteReq.Organization = ch.Config.Organization
promoteReq.Branch = branch

Expand All @@ -68,63 +64,21 @@ func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {
return err
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Promoting %s branch in %s to production...", printer.BoldBlue(branch), printer.BoldBlue(source)))
end := ch.Printer.PrintProgress(fmt.Sprintf("Promoting %s branch in %s to production...", printer.BoldBlue(branch), printer.BoldBlue(db)))
defer end()
promotionRequest, err := client.DatabaseBranches.RequestPromotion(cmd.Context(), promoteReq)
dbBranch, err := client.DatabaseBranches.Promote(cmd.Context(), promoteReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("branch %s does not exist in database %s",
printer.BoldBlue(branch), printer.BoldBlue(source))
return fmt.Errorf("branch %s does not exist in database %s", printer.BoldBlue(branch), printer.BoldBlue(db))
default:
return cmdutil.HandleError(err)
}
}

if promotionRequest.State == "pending" {
getReq := &ps.GetPromotionRequestRequest{
Organization: ch.Config.Organization,
Database: source,
Branch: branch,
}

promotionRequest, err = waitPromoteState(cmd.Context(), client, getReq)
if err != nil {
switch cmdutil.ErrCode(err) {
case ps.ErrNotFound:
return fmt.Errorf("promotion request for branch %s does not exist in database %s", printer.BoldBlue(branch), printer.BoldBlue(source))
default:
return cmdutil.HandleError(err)
}
}
}

end()

if ch.Printer.Format() == printer.Human {
if promotionRequest.State == "lint_error" {

var sb strings.Builder
sb.WriteString(printer.Red("Branch promotion failed. "))
sb.WriteString("Fix the following errors and then try again:\n\n")
for _, lintError := range promotionRequest.LintErrors {
fmt.Fprintf(&sb, "• %s\n", lintError.ErrorDescription)
}

return errors.New(sb.String())
} else {
ch.Printer.Printf("Branch %s in %s was successfully promoted.\n", printer.BoldBlue(promotionRequest.Branch), printer.BoldBlue(source))
return nil
}
}

dbBranch, err := client.DatabaseBranches.Get(cmd.Context(), &ps.GetDatabaseBranchRequest{
Organization: ch.Config.Organization,
Database: source,
Branch: branch,
})
if err != nil {
return cmdutil.HandleError(err)
ch.Printer.Printf("Branch %s in %s was successfully promoted to production. Note: In order to use deploy requests to deploy schema changes to this branch, safe migrations must also be enabled.\n", printer.BoldBlue(branch), printer.BoldBlue(db))
return nil
}

return ch.Printer.PrintResource(ToDatabaseBranch(dbBranch))
Expand All @@ -133,28 +87,3 @@ func PromoteCmd(ch *cmdutil.Helper) *cobra.Command {

return cmd
}

func waitPromoteState(ctx context.Context, client *ps.Client, getReq *ps.GetPromotionRequestRequest) (*ps.BranchPromotionRequest, error) {
ctx, cancel := context.WithTimeout(ctx, 1*time.Minute)
defer cancel()

ticker := time.NewTicker(time.Second)

var promotionRequest *ps.BranchPromotionRequest
var err error
for {
select {
case <-ctx.Done():
return nil, errors.New("branch promotion timed out")
case <-ticker.C:
promotionRequest, err = client.DatabaseBranches.GetPromotionRequest(ctx, getReq)
if err != nil {
return nil, err
}

if promotionRequest.State != "pending" {
return promotionRequest, nil
}
}
}
}
26 changes: 5 additions & 21 deletions internal/cmd/branch/promote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,13 @@ func TestBranch_PromoteCmd(t *testing.T) {
branch := "development"

res := &ps.DatabaseBranch{
Name: branch,
Name: branch,
Production: true,
SafeMigrations: false,
}

svc := &mock.DatabaseBranchesService{
RequestPromotionFn: func(ctx context.Context, req *ps.RequestPromotionRequest) (*ps.BranchPromotionRequest, error) {
c.Assert(req.Branch, qt.Equals, branch)
c.Assert(req.Database, qt.Equals, db)
c.Assert(req.Organization, qt.Equals, org)

return &ps.BranchPromotionRequest{
Branch: branch,
State: "pending",
}, nil
},
GetPromotionRequestFn: func(ctx context.Context, req *ps.GetPromotionRequestRequest) (*ps.BranchPromotionRequest, error) {
return &ps.BranchPromotionRequest{
Branch: branch,
State: "promoted",
}, nil
},
GetFn: func(ctx context.Context, req *ps.GetDatabaseBranchRequest) (*ps.DatabaseBranch, error) {
PromoteFn: func(ctx context.Context, req *ps.PromoteRequest) (*ps.DatabaseBranch, error) {
c.Assert(req.Branch, qt.Equals, branch)
c.Assert(req.Database, qt.Equals, db)
c.Assert(req.Organization, qt.Equals, org)
Expand All @@ -73,8 +59,6 @@ func TestBranch_PromoteCmd(t *testing.T) {
err := cmd.Execute()

c.Assert(err, qt.IsNil)
c.Assert(svc.RequestPromotionFnInvoked, qt.IsTrue)
c.Assert(svc.GetFnInvoked, qt.IsTrue)
c.Assert(svc.GetPromotionRequestFnInvoked, qt.IsTrue)
c.Assert(svc.PromoteFnInvoked, qt.IsTrue)
c.Assert(buf.String(), qt.JSONEquals, res)
}