Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 16 additions & 2 deletions internal/command/launch/launch_databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package launch
import (
"context"
"fmt"
"net/url"
"time"

"github.com/avast/retry-go/v4"
Expand Down Expand Up @@ -398,9 +399,22 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID
state.Plan.AppName, appOrgSlug, clusterID, clusterOrgSlug)
}

// Build connection URI with the database name from the plan (if provided)
connectionUri := cluster.Credentials.ConnectionUri
dbName := state.Plan.Postgres.ManagedPostgres.DbName
if dbName != "" {
// Parse the base connection URI and replace the database name
parsedUri, err := url.Parse(cluster.Credentials.ConnectionUri)
if err != nil {
return fmt.Errorf("failed to parse connection URI: %w", err)
}
parsedUri.Path = "/" + dbName
connectionUri = parsedUri.String()
}

// Set the connection string as a secret
secrets := map[string]string{
"DATABASE_URL": cluster.Credentials.ConnectionUri,
"DATABASE_URL": connectionUri,
}

flapsClient := flapsutil.ClientFromContext(ctx)
Expand All @@ -409,7 +423,7 @@ func (state *launchState) attachToManagedPostgres(ctx context.Context, clusterID
}

fmt.Fprintf(io.Out, "Managed Postgres cluster %s is now attached to %s\n", clusterID, state.Plan.AppName)
fmt.Fprintf(io.Out, "The following secret was added to %s:\n DATABASE_URL=%s\n", state.Plan.AppName, cluster.Credentials.ConnectionUri)
fmt.Fprintf(io.Out, "The following secret was added to %s:\n DATABASE_URL=%s\n", state.Plan.AppName, connectionUri)

return nil
}
Expand Down
3 changes: 3 additions & 0 deletions internal/command/launch/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ func (state *launchState) EditInWebUi(ctx context.Context) error {
}

// Apply settings from the form
// Check both "db_name" (Go struct json tag) and "name" (API/UI convention)
if dbName, ok := mpgData["db_name"].(string); ok && dbName != "" {
state.Plan.Postgres.ManagedPostgres.DbName = dbName
} else if dbName, ok := mpgData["name"].(string); ok && dbName != "" {
state.Plan.Postgres.ManagedPostgres.DbName = dbName
}
if plan, ok := mpgData["plan"].(string); ok && plan != "" {
state.Plan.Postgres.ManagedPostgres.Plan = plan
Expand Down
Loading