Skip to content

Commit 59b6d4c

Browse files
aezellclaude
andcommitted
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 <[email protected]>
1 parent e5c3ee0 commit 59b6d4c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

internal/command/launch/launch_databases.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package launch
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67
"time"
78

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

402+
// Build connection URI with the database name from the plan (if provided)
403+
connectionUri := cluster.Credentials.ConnectionUri
404+
dbName := state.Plan.Postgres.ManagedPostgres.DbName
405+
if dbName != "" {
406+
// Parse the base connection URI and replace the database name
407+
parsedUri, err := url.Parse(cluster.Credentials.ConnectionUri)
408+
if err != nil {
409+
return fmt.Errorf("failed to parse connection URI: %w", err)
410+
}
411+
parsedUri.Path = "/" + dbName
412+
connectionUri = parsedUri.String()
413+
}
414+
401415
// Set the connection string as a secret
402416
secrets := map[string]string{
403-
"DATABASE_URL": cluster.Credentials.ConnectionUri,
417+
"DATABASE_URL": connectionUri,
404418
}
405419

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

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

414428
return nil
415429
}

internal/command/launch/webui.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ func (state *launchState) EditInWebUi(ctx context.Context) error {
123123
}
124124

125125
// Apply settings from the form
126+
// Check both "db_name" (Go struct json tag) and "name" (API/UI convention)
126127
if dbName, ok := mpgData["db_name"].(string); ok && dbName != "" {
127128
state.Plan.Postgres.ManagedPostgres.DbName = dbName
129+
} else if dbName, ok := mpgData["name"].(string); ok && dbName != "" {
130+
state.Plan.Postgres.ManagedPostgres.DbName = dbName
128131
}
129132
if plan, ok := mpgData["plan"].(string); ok && plan != "" {
130133
state.Plan.Postgres.ManagedPostgres.Plan = plan

0 commit comments

Comments
 (0)