From 59b6d4cbcbac99426e194ada5f8b7dbc687b2d9a Mon Sep 17 00:00:00 2001 From: Alex Ezell Date: Fri, 23 Jan 2026 10:12:36 -0600 Subject: [PATCH] fix(launch): use custom database name when attaching to existing MPG cluster When attaching to an existing Managed Postgres cluster during launch, the DATABASE_URL secret now correctly uses the database name provided by the user in the web customization form, instead of always defaulting to "fly-db". Changes: - webui.go: Check for both "db_name" and "name" keys in form data - launch_databases.go: Parse and modify connection URI to use custom database name when attaching to existing cluster Co-Authored-By: Claude Opus 4.5 --- internal/command/launch/launch_databases.go | 18 ++++++++++++++++-- internal/command/launch/webui.go | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/command/launch/launch_databases.go b/internal/command/launch/launch_databases.go index ea54adb87d..b4b99e60fc 100644 --- a/internal/command/launch/launch_databases.go +++ b/internal/command/launch/launch_databases.go @@ -3,6 +3,7 @@ package launch import ( "context" "fmt" + "net/url" "time" "github.com/avast/retry-go/v4" @@ -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) @@ -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 } diff --git a/internal/command/launch/webui.go b/internal/command/launch/webui.go index ffdeafe379..170b14587d 100644 --- a/internal/command/launch/webui.go +++ b/internal/command/launch/webui.go @@ -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