Skip to content

Use the new Managed Postgres API in the fly launch command - #5076

Open
miguel-fly wants to merge 1 commit into
masterfrom
migrate-launch-command-to-new-mpg-client
Open

Use the new Managed Postgres API in the fly launch command#5076
miguel-fly wants to merge 1 commit into
masterfrom
migrate-launch-command-to-new-mpg-client

Conversation

@miguel-fly

@miguel-fly miguel-fly commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Change Summary

What and Why:

fly launch still creates managed Postgres clusters through the old v1 API. This moves it to the new /v1/postgres API (served through flaps) with a new internal/mpg client.

This is a first step toward retiring v1 cluster creation in flyctl.

How:

  • New internal/mpg client: create, get, and user credentials
  • DATABASE_URL is built from the cluster's pooler endpoint and default user
  • MPG region availability now comes from /v1/platform/regions

Documentation

  • Fresh Produce
  • In superfly/docs, or asked for help from docs team
  • n/a

@tomr-a5 tomr-a5 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates fly launch to provision Managed Postgres clusters via the newer Machines/Flaps-served /v1/postgres API by introducing a new internal internal/mpg client, and switches MPG region availability checks to use the Machines regions listing.

Changes:

  • Added a new internal/mpg HTTP client for creating clusters, fetching clusters, and retrieving user credentials.
  • Updated fly launch managed-Postgres provisioning flow to use the new client and build DATABASE_URL from the cluster pooler endpoint + default user.
  • Switched MPG region validation to use Machines/Flaps region data (and updated tests accordingly).

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
internal/mpg/regions.go Adds region availability helpers backed by Flaps/Machines regions.
internal/mpg/client.go Introduces the new Managed Postgres API client and models.
internal/command/launch/webui.go Updates web UI plan editing to validate MPG regions via the new helpers.
internal/command/launch/plan/postgres.go Updates default/forced MPG selection logic to use new region validation helpers.
internal/command/launch/plan/postgres_test.go Updates tests to mock region availability via the Flaps regions client.
internal/command/launch/launch_databases.go Switches MPG cluster creation/polling/credentials + DATABASE_URL to the new client.
internal/cmdutil/preparers/preparers.go Ensures the new internal/mpg client is initialized into context.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/mpg/client.go
Comment on lines +178 to +181
var env clusterEnvelope
if err := c.do(ctx, http.MethodGet, "/v1/postgres/"+id, nil, &env, http.StatusOK); err != nil {
return Cluster{}, err
}
Comment thread internal/mpg/client.go
// GetUserCredentials returns the username and current password for a named user.
func (c *httpClient) GetUserCredentials(ctx context.Context, id, username string) (UserCredentials, error) {
var env userCredentialsEnvelope
path := fmt.Sprintf("/v1/postgres/%s/users/%s/credentials", id, username)
Comment on lines 168 to 172
var (
io = iostreams.FromContext(ctx)
pgPlan = state.Plan.Postgres.ManagedPostgres
mpgClient = mpgv1.ClientFromContext(ctx)
mpgClient = mpgapi.ClientFromContext(ctx)
)
Comment on lines 246 to 248
// Create a separate context for the wait loop with 15 minute timeout
waitCtx := context.Background()
waitCtx, cancel := context.WithTimeout(waitCtx, 15*time.Minute)
err = retry.Do(
func() error {
cluster, err := mpgClient.GetManagedClusterById(ctx, response.Data.Id)
current, err := mpgClient.GetCluster(ctx, clusterID)
Comment thread internal/mpg/client.go
Comment on lines +128 to +137
func (c Cluster) ConnectionURI(password string) string {
u := url.URL{
Scheme: "postgres",
User: url.UserPassword(DefaultUsername, password),
Host: fmt.Sprintf("%s:%d", c.Endpoints.Primary.Pooler.Host, DefaultPort),
Path: "/" + DefaultDatabase,
}

return u.String()
}
@tomr-a5

tomr-a5 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@NullHypothesis ah my bad goat, didnt realize you were already reviewing this one. Didnt mean to step on your toes, would still love your eyes on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants