diff --git a/internal/cmd/branch/create.go b/internal/cmd/branch/create.go index 56171132..8061e48e 100644 --- a/internal/cmd/branch/create.go +++ b/internal/cmd/branch/create.go @@ -80,9 +80,6 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { if flags.backupID != "" && flags.parentBranch != "" && flags.restorePoint == "" { return fmt.Errorf("--from and --restore cannot be used together") } - if cmd.Flags().Changed("replicas") && flags.backupID == "" && flags.restorePoint == "" { - return fmt.Errorf("--replicas can only be used with a PostgreSQL backup restore or point-in-time recovery") - } if (cmd.Flags().Changed("storage-iops") || cmd.Flags().Changed("storage-throughput")) && flags.backupID == "" && flags.restorePoint == "" { return fmt.Errorf("--storage-iops and --storage-throughput can only be used with a PostgreSQL backup restore or point-in-time recovery") } @@ -140,7 +137,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { if db.Kind == "mysql" { if cmd.Flags().Changed("replicas") { - return fmt.Errorf("--replicas is only supported for PostgreSQL backup restores and point-in-time recovery") + return fmt.Errorf("--replicas is only supported for PostgreSQL databases") } if cmd.Flags().Changed("min-storage") || cmd.Flags().Changed("max-storage") { return fmt.Errorf("--min-storage and --max-storage are only supported for PostgreSQL databases") @@ -313,7 +310,7 @@ func CreateCmd(ch *cmdutil.Helper) *cobra.Command { cmd.Flags().BoolVar(&flags.dataBranching, "seed-data", false, "Add seed data using the Data Branching™ feature. This branch will be created with the same resources as the base branch.") cmd.Flags().BoolVar(&flags.wait, "wait", false, "Wait until the branch is ready") cmd.Flags().StringVar(&flags.majorVersion, "major-version", "", "For PostgreSQL databases, the PostgreSQL major version to use for the branch. Defaults to the major version of the parent branch if it exists or the database's default branch major version. Ignored for branches restored from backups.") - cmd.Flags().IntVar(&flags.replicas, "replicas", 0, "Number of additional replicas for a PostgreSQL restore. 0 creates a single-node branch; omit to use the target cluster size default.") + cmd.Flags().IntVar(&flags.replicas, "replicas", 0, "Number of replicas for a PostgreSQL branch. 0 is a single-node branch with no replicas; 2-8 is HA. Omit to use the cluster size default.") cmd.Flags().Int64Var(&flags.minStorage, "min-storage", 0, "Minimum storage size in bytes") cmd.Flags().Int64Var(&flags.maxStorage, "max-storage", 0, "Maximum storage size in bytes for autoscaling") cmd.Flags().Int64Var(&flags.storageIOPS, "storage-iops", 0, "Storage IOPS for a PostgreSQL restore. Requires --restore or --restore-point.") diff --git a/internal/cmd/branch/create_test.go b/internal/cmd/branch/create_test.go index 54d4f26d..7c055392 100644 --- a/internal/cmd/branch/create_test.go +++ b/internal/cmd/branch/create_test.go @@ -1275,15 +1275,19 @@ func TestBranch_CreateCmdWithRestorePointMySQLError(t *testing.T) { c.Assert(svc.CreateFnInvoked, qt.IsFalse) } -func TestBranch_CreateCmdWithPostgresRestoreReplicas(t *testing.T) { +func TestBranch_CreateCmdWithPostgresReplicas(t *testing.T) { zero := 0 + two := 2 tests := []struct { name string args []string wantReplicas *int + wantBackupID string }{ - {name: "omitted", args: []string{"planetscale", "restored", "--restore", "backup-id"}}, - {name: "explicit zero", args: []string{"planetscale", "restored", "--restore", "backup-id", "--replicas", "0"}, wantReplicas: &zero}, + {name: "restore omitted", args: []string{"planetscale", "restored", "--restore", "backup-id"}, wantBackupID: "backup-id"}, + {name: "restore explicit zero", args: []string{"planetscale", "restored", "--restore", "backup-id", "--replicas", "0"}, wantReplicas: &zero, wantBackupID: "backup-id"}, + {name: "ordinary branch", args: []string{"planetscale", "development", "--replicas", "2"}, wantReplicas: &two}, + {name: "ordinary explicit zero", args: []string{"planetscale", "development", "--replicas", "0"}, wantReplicas: &zero}, } for _, tt := range tests { @@ -1295,10 +1299,10 @@ func TestBranch_CreateCmdWithPostgresRestoreReplicas(t *testing.T) { p := printer.NewPrinter(&format) p.SetResourceOutput(&buf) - res := &ps.PostgresBranch{Name: "restored"} + res := &ps.PostgresBranch{Name: tt.args[1]} svc := &mock.PostgresBranchesService{ CreateFn: func(ctx context.Context, req *ps.CreatePostgresBranchRequest) (*ps.PostgresBranch, error) { - c.Assert(req.BackupID, qt.Equals, "backup-id") + c.Assert(req.BackupID, qt.Equals, tt.wantBackupID) if tt.wantReplicas == nil { c.Assert(req.Replicas, qt.IsNil) } else { @@ -1331,26 +1335,6 @@ func TestBranch_CreateCmdWithPostgresRestoreReplicas(t *testing.T) { } } -func TestBranch_CreateCmdRejectsReplicasWithoutRestore(t *testing.T) { - tests := []struct { - name string - args []string - }{ - {name: "ordinary branch", args: []string{"planetscale", "development", "--replicas", "2"}}, - {name: "data branching", args: []string{"planetscale", "development", "--seed-data", "--replicas", "2"}}, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := qt.New(t) - cmd := CreateCmd(&cmdutil.Helper{}) - cmd.SetArgs(tt.args) - - c.Assert(cmd.Execute(), qt.ErrorMatches, ".*--replicas can only be used with a PostgreSQL backup restore or point-in-time recovery.*") - }) - } -} - func TestBranch_CreateCmdRejectsRestoreReplicasForMySQL(t *testing.T) { c := qt.New(t)