Skip to content

Commit

Permalink
Add branch promote command.
Browse files Browse the repository at this point in the history
  • Loading branch information
iheanyi committed Aug 11, 2021
1 parent fc76d85 commit 8189e0d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type DatabaseBranch struct {
Name string `header:"name" json:"name"`
Status string `header:"status" json:"status"`
ParentBranch string `header:"parent branch,n/a" json:"parent_branch"`
Production bool `header:"production" json:"production"`
CreatedAt int64 `header:"created_at,timestamp(ms|utc|human)" json:"created_at"`
UpdatedAt int64 `header:"updated_at,timestamp(ms|utc|human)" json:"updated_at"`

Expand All @@ -60,6 +61,7 @@ func toDatabaseBranch(db *ps.DatabaseBranch) *DatabaseBranch {
Name: db.Name,
Status: db.Status,
ParentBranch: db.ParentBranch,
Production: db.Production,
CreatedAt: db.CreatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
UpdatedAt: db.UpdatedAt.UTC().UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond)),
orig: db,
Expand Down
60 changes: 60 additions & 0 deletions internal/cmd/branch/promote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package branch

import (
"bytes"
"context"
"testing"

qt "github.com/frankban/quicktest"
"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/config"
"github.com/planetscale/cli/internal/mock"
"github.com/planetscale/cli/internal/printer"
ps "github.com/planetscale/planetscale-go/planetscale"
)

func TestBranch_PromoteCmd(t *testing.T) {
c := qt.New(t)

var buf bytes.Buffer
format := printer.JSON
p := printer.NewPrinter(&format)
p.SetResourceOutput(&buf)

org := "planetscale"
db := "planetscale"
branch := "development"

res := &ps.DatabaseBranch{Name: branch}

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

return res, nil
},
}

ch := &cmdutil.Helper{
Printer: p,
Config: &config.Config{
Organization: org,
},
Client: func() (*ps.Client, error) {
return &ps.Client{
DatabaseBranches: svc,
}, nil

},
}

cmd := PromoteCmd(ch)
cmd.SetArgs([]string{db, branch})
err := cmd.Execute()

c.Assert(err, qt.IsNil)
c.Assert(svc.PromoteFnInvoked, qt.IsTrue)
c.Assert(buf.String(), qt.JSONEquals, res)
}
8 changes: 8 additions & 0 deletions internal/mock/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type DatabaseBranchesService struct {

RefreshSchemaFn func(context.Context, *ps.RefreshSchemaRequest) error
RefreshSchemaFnInvoked bool

PromoteFn func(context.Context, *ps.PromoteBranchRequest) (*ps.DatabaseBranch, error)
PromoteFnInvoked bool
}

func (d *DatabaseBranchesService) Create(ctx context.Context, req *ps.CreateDatabaseBranchRequest) (*ps.DatabaseBranch, error) {
Expand Down Expand Up @@ -71,3 +74,8 @@ func (d *DatabaseBranchesService) RefreshSchema(ctx context.Context, req *ps.Ref
d.RefreshSchemaFnInvoked = true
return d.RefreshSchemaFn(ctx, req)
}

func (d *DatabaseBranchesService) Promote(ctx context.Context, req *ps.PromoteBranchRequest) (*ps.DatabaseBranch, error) {
d.PromoteFnInvoked = true
return d.PromoteFn(ctx, req)
}

0 comments on commit 8189e0d

Please sign in to comment.