Skip to content

Commit

Permalink
Merge branch 'main' into passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
Phani Raj committed Aug 13, 2021
2 parents e9217a8 + ed531ca commit 5875424
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20201112035734-206646e67786
github.com/pkg/errors v0.9.1
github.com/planetscale/planetscale-go v0.38.0
github.com/planetscale/planetscale-go v0.39.0
github.com/planetscale/sql-proxy v0.8.0
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
github.com/xelabs/go-mysqlstack v1.0.0
go.uber.org/zap v1.18.1
go.uber.org/zap v1.19.0
golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/planetscale/planetscale-go v0.33.0/go.mod h1:99n+tnrvJaaUako3UZNCiC1dTtSPKr81GqAUNibw5pc=
github.com/planetscale/planetscale-go v0.38.0 h1:Sn6Xx1/u3xpU3v/mt1CdR38JXcTjej56rUpwuA3A5EY=
github.com/planetscale/planetscale-go v0.38.0/go.mod h1:99n+tnrvJaaUako3UZNCiC1dTtSPKr81GqAUNibw5pc=
github.com/planetscale/planetscale-go v0.39.0 h1:1vq4bnowa9WVWinzaGzFrH+257Bv5iiewJH9/6Qe7yE=
github.com/planetscale/planetscale-go v0.39.0/go.mod h1:99n+tnrvJaaUako3UZNCiC1dTtSPKr81GqAUNibw5pc=
github.com/planetscale/sql-proxy v0.8.0 h1:EeWQhocldn0ldrTxPIIIrbHWWqNqHrNFYiSdp4tUY0E=
github.com/planetscale/sql-proxy v0.8.0/go.mod h1:vOcL5jRMzaza+8q79hRRhMj3ABpYHy3cgenr2FOhDWU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -322,8 +322,9 @@ go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.17.0/go.mod h1:MXVU+bhUf/A7Xi2HNOnopQOrmycQ5Ih87HtOu4q5SSo=
go.uber.org/zap v1.18.1 h1:CSUJ2mjFszzEWt4CdKISEuChVIXGBn3lAPwkRGyVrc4=
go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.19.0 h1:mZQZefskPPCMIBCSEH0v2/iUqqLrYtaeqwD6FUGUnFE=
go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/branch/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func BranchCmd(ch *cmdutil.Helper) *cobra.Command {
cmd.AddCommand(DiffCmd(ch))
cmd.AddCommand(SchemaCmd(ch))
cmd.AddCommand(RefreshSchemaCmd(ch))
cmd.AddCommand(PromoteCmd(ch))

return cmd
}
Expand All @@ -38,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 @@ -59,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
6 changes: 0 additions & 6 deletions internal/cmd/branch/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {
source := args[0]
branch := args[1]

// Simplest case, the names are equivalent
if source == branch {
return fmt.Errorf("a branch named '%s' already exists", branch)
}

createReq.Database = source
createReq.Name = branch
createReq.Organization = ch.Config.Organization
Expand Down Expand Up @@ -114,7 +109,6 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {

cmd.Flags().StringVar(&createReq.ParentBranch, "from", "", "branch to be created from")
cmd.Flags().StringVar(&createReq.Region, "region", "", "region for the database")
cmd.Flags().MarkHidden("region")
cmd.RegisterFlagCompletionFunc("region", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
client, err := ch.Client()
Expand Down
93 changes: 93 additions & 0 deletions internal/cmd/branch/promote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package branch

import (
"fmt"

"github.com/planetscale/cli/internal/cmdutil"
"github.com/planetscale/cli/internal/printer"
"github.com/spf13/cobra"

ps "github.com/planetscale/planetscale-go/planetscale"
)

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

cmd := &cobra.Command{
Use: "promote <database> <branch> [options]",
Short: "Promote a new branch from a database",
Args: cmdutil.RequiredArgs("source-database", "branch"),
Aliases: []string{"b"},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}

client, err := ch.Client()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

org := ch.Config.Organization // --org flag
if org == "" {
cfg, err := ch.ConfigFS.DefaultConfig()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

org = cfg.Organization
}

databases, err := client.Databases.List(cmd.Context(), &ps.ListDatabasesRequest{
Organization: org,
})
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}

candidates := make([]string, 0, len(databases))
for _, db := range databases {
candidates = append(candidates, db.Name)
}

return candidates, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
source := args[0]
branch := args[1]

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

client, err := ch.Client()
if err != nil {
return err
}

end := ch.Printer.PrintProgress(fmt.Sprintf("Promoting %s branch in %s to production...", printer.BoldBlue(branch), printer.BoldBlue(source)))
defer end()
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))
default:
return cmdutil.HandleError(err)
}
}

end()

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Branch %s in %s was successfully promoted.\n", printer.BoldBlue(dbBranch.Name), printer.BoldBlue(source))
return nil
}

return ch.Printer.PrintResource(toDatabaseBranch(dbBranch))
},
}

return cmd
}
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)
}
1 change: 0 additions & 1 deletion internal/cmd/database/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command {

cmd.Flags().StringVar(&createReq.Notes, "notes", "", "notes for the database")
cmd.Flags().StringVar(&createReq.Region, "region", "", "region for the database")
cmd.Flags().MarkHidden("region")
cmd.RegisterFlagCompletionFunc("region", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
client, err := ch.Client()
Expand Down
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 5875424

Please sign in to comment.