Skip to content

Commit

Permalink
fix: obtain Postgres URL from branches get command (#2996)
Browse files Browse the repository at this point in the history
* fix:2964/obtain Postgres DB URL from ENV using branches get command

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* added --output-env flag in branches get command

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* fixed typo in internal/branches/get/get.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* updated the output-env flag in branches.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* changed output var to postgres_url var in branches.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* changed postgres_url to output

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* changed checks condition from env to utils.OutputEnv

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* remove cmdFlags condition in cmd/branches.go

Co-authored-by: Han Qiao <sweatybridge@gmail.com>

* remove cmdFlags in cmd/branches.go

Co-authored-by: Han Qiao <sweatybridge@gmail.com>

* remove pgconn.Config{} param in get.Run()

Co-authored-by: Han Qiao <sweatybridge@gmail.com>

* remove unused import in cmd/branches.go

Co-authored-by: Han Qiao <sweatybridge@gmail.com>

* get DB URL from utils.GetSupabaseDbHost(...)

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* revert to prev table render structure

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* update get.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* added postgres connection string

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* removed GetPostgresURLNonPooling func from bootstrap.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* remove vars which are not required

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* minor changes in get.go & branches.go

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* convert port from int to uint

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* attempt to fix linter errors

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* attempt to fix uint->uint16 linter error

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

* fixed uint->uint16 overflow error

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>

---------

Signed-off-by: Yaten Dhingra <yaten598@gmail.com>
Co-authored-by: Han Qiao <sweatybridge@gmail.com>
  • Loading branch information
yaten2302 and sweatybridge authored Jan 5, 2025
1 parent 2cca98c commit 1bdc4ef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cmd/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var (
} else {
branchId = args[0]
}
return get.Run(ctx, branchId)
return get.Run(ctx, branchId, afero.NewOsFs())
},
}

Expand Down Expand Up @@ -165,6 +165,8 @@ func init() {
createFlags.Var(&branchRegion, "region", "Select a region to deploy the branch database.")
createFlags.Var(&size, "size", "Select a desired instance size for the branch database.")
createFlags.BoolVar(&persistent, "persistent", false, "Whether to create a persistent branch.")
getFlags := branchGetCmd.Flags()
getFlags.VarP(&utils.OutputFormat, "output", "o", "Output format of branch details.")
branchesCmd.AddCommand(branchCreateCmd)
branchesCmd.AddCommand(branchListCmd)
branchesCmd.AddCommand(branchGetCmd)
Expand Down
29 changes: 25 additions & 4 deletions internal/branches/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package get
import (
"context"
"fmt"
"os"

"github.com/go-errors/errors"
"github.com/jackc/pgconn"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/migration/list"
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/cast"
)

func Run(ctx context.Context, branchId string) error {
func Run(ctx context.Context, branchId string, fsys afero.Fs) error {
resp, err := utils.GetSupabase().V1GetABranchConfigWithResponse(ctx, branchId)
if err != nil {
return errors.Errorf("failed to retrieve preview branch: %w", err)
Expand All @@ -29,17 +33,34 @@ func Run(ctx context.Context, branchId string) error {
resp.JSON200.JwtSecret = &masked
}

table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|
|-|-|-|-|-|-|-|
config := pgconn.Config{
Host: utils.GetSupabaseDbHost(resp.JSON200.DbHost),
Port: cast.UIntToUInt16(cast.IntToUint(resp.JSON200.DbPort)),
User: *resp.JSON200.DbUser,
Password: *resp.JSON200.DbPass,
}

postgresConnectionString := utils.ToPostgresURL(config)
if utils.OutputFormat.Value != utils.OutputPretty {
envs := map[string]string{
"POSTGRES_URL": postgresConnectionString,
}
return utils.EncodeOutput(utils.OutputFormat.Value, os.Stdout, envs)
}

table := `|HOST|PORT|USER|PASSWORD|JWT SECRET|POSTGRES VERSION|STATUS|POSTGRES URL|
|-|-|-|-|-|-|-|-|
` + fmt.Sprintf(
"|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|\n",
"|`%s`|`%d`|`%s`|`%s`|`%s`|`%s`|`%s`|`%s`|\n",
resp.JSON200.DbHost,
resp.JSON200.DbPort,
*resp.JSON200.DbUser,
*resp.JSON200.DbPass,
*resp.JSON200.JwtSecret,
resp.JSON200.PostgresVersion,
resp.JSON200.Status,
postgresConnectionString,
)

return list.RenderTable(table)
}
8 changes: 8 additions & 0 deletions pkg/cast/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ func UintToInt(value uint) int {
return math.MaxInt
}

// UIntToUInt16 converts a uint to an uint16, handling potential overflow
func UIntToUInt16(value uint) uint16 {
if value <= math.MaxUint16 {
return uint16(value)
}
return math.MaxUint16
}

// IntToUint converts an int to a uint, handling negative values
func IntToUint(value int) uint {
if value < 0 {
Expand Down

0 comments on commit 1bdc4ef

Please sign in to comment.