Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: obtain Postgres URL from branches get command #2996

Merged
merged 24 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0fe8088
fix:2964/obtain Postgres DB URL from ENV using branches get command
yaten2302 Dec 25, 2024
215116f
Merge branch 'supabase:develop' into develop
yaten2302 Dec 31, 2024
1342a93
added --output-env flag in branches get command
yaten2302 Dec 31, 2024
5f7573b
fixed typo in internal/branches/get/get.go
yaten2302 Jan 1, 2025
7898aca
updated the output-env flag in branches.go
yaten2302 Jan 1, 2025
031d8c2
changed output var to postgres_url var in branches.go
yaten2302 Jan 1, 2025
1b7a0d8
changed postgres_url to output
yaten2302 Jan 1, 2025
81657b0
changed checks condition from env to utils.OutputEnv
yaten2302 Jan 1, 2025
1d7773f
remove cmdFlags condition in cmd/branches.go
yaten2302 Jan 1, 2025
e275bb4
remove cmdFlags in cmd/branches.go
yaten2302 Jan 1, 2025
eca4f21
remove pgconn.Config{} param in get.Run()
yaten2302 Jan 1, 2025
69b6c8e
remove unused import in cmd/branches.go
yaten2302 Jan 1, 2025
b3d6e5f
get DB URL from utils.GetSupabaseDbHost(...)
yaten2302 Jan 1, 2025
76407f2
revert to prev table render structure
yaten2302 Jan 1, 2025
73a60fd
update get.go
yaten2302 Jan 1, 2025
139e739
added postgres connection string
yaten2302 Jan 3, 2025
188ecc6
removed GetPostgresURLNonPooling func from bootstrap.go
yaten2302 Jan 3, 2025
44257ab
remove vars which are not required
yaten2302 Jan 3, 2025
edf5b8b
minor changes in get.go & branches.go
yaten2302 Jan 3, 2025
77d76a2
convert port from int to uint
yaten2302 Jan 4, 2025
7acf452
Merge branch 'supabase:develop' into develop
yaten2302 Jan 4, 2025
1b61d70
attempt to fix linter errors
yaten2302 Jan 4, 2025
0114638
attempt to fix uint->uint16 linter error
yaten2302 Jan 5, 2025
bc371bf
fixed uint->uint16 overflow error
yaten2302 Jan 5, 2025
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
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
}

yaten2302 marked this conversation as resolved.
Show resolved Hide resolved
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)
yaten2302 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading