Skip to content

Discover dev db port from Docker at compile time - #4567

Open
sodic wants to merge 5 commits into
mainfrom
filip/4529/multiple-dev-dbs
Open

Discover dev db port from Docker at compile time#4567
sodic wants to merge 5 commits into
mainfrom
filip/4529/multiple-dev-dbs

Conversation

@sodic

@sodic sodic commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Description

Part of #4529 (running multiple dev databases in parallel). Stacked on #4568.

Before this PR:

  1. wasp start db always starts the database on port 5432.
  2. wasp start always looks for the dev database on port 5432.
  3. Since wasp start looks for the DB on localhost:5432:
    • If wasp start db is running, all good.
    • If there's no database on localhost:5432, it fails using the DbConnectionEstablished requirement. The user gets a nice error.
    • If there's a different on localhost:5432, wasp start will attempt to connect to it. It will either fail during compilation thanks to incorrect credentials or make it all the way to application code and hijack another app's database.

After this PR:

  1. wasp start always starts the database on port 5432, this is unchanged and coming in the next PR.
  2. wasp start discovers the dev database port by asking docker. It relies on the container's name, which is the project's path-based unique ID.
  3. Since wasp start looks for the DB on the output of docker port uniqueContainerName:
    • If wasp start db is running, docker port returns the correct port, and all good.
    • If wasp start db isn't running, the user gets a nice error. wasp start will no longer connect to whatever is running on 5432, wich means no more database hijacking (at least not until runtime).

Type of change

  • 🔧 Just code/docs improvement
  • 🐞 Bug fix
  • 🚀 New/improved feature
  • 💥 Breaking change

Checklist

  • I tested my change in a Wasp app to verify that it works as intended.

  • 🧪 Tests and apps:

    • I added unit tests for my change.
    • (if you fixed a bug) I added a regression test for the bug I fixed.
    • (if you added/updated a feature) I added/updated e2e tests in examples/kitchen-sink/e2e-tests.
    • (if you added/updated a feature) I updated the starter templates in waspc/data/Cli/templates, as needed.
    • (if you added/updated a feature) I updated the example apps in examples/, as needed.
      • (if you updated examples/tutorials) I updated the tutorial in the docs (and vice versa).
  • 📜 Documentation:

    • (if you added/updated a feature) I added/updated the documentation in web/docs/.
  • 🆕 Changelog: (if change is more than just code/docs improvement)

    • I updated waspc/ChangeLog.md with a user-friendly description of the change.
    • (if you did a breaking change) I added a step to the current migration guide in web/docs/migration-guides/.
    • I bumped the version in waspc/waspc.cabal to reflect the changes I introduced.

@sodic
sodic requested a review from a team as a code owner July 28, 2026 19:46
@sodic
sodic requested review from infomiho and removed request for a team July 28, 2026 19:46
@sodic
sodic marked this pull request as draft July 28, 2026 19:47
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 28, 2026

Copy link
Copy Markdown

Deploying wasp-docs-on-main with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5d46f74
Status: ✅  Deploy successful!
Preview URL: https://5b3cffbd.wasp-docs-on-main.pages.dev
Branch Preview URL: https://filip-4529-multiple-dev-dbs.wasp-docs-on-main.pages.dev

View logs

@sodic
sodic force-pushed the filip/4529/multiple-dev-dbs branch from fe5e788 to 5447ef7 Compare July 28, 2026 19:57
@sodic
sodic changed the base branch from main to filip/4529/move-dev-db-naming July 28, 2026 19:57
@sodic
sodic had a problem deploying to fly-deploy-test July 28, 2026 20:22 — with GitHub Actions Failure
@sodic
sodic temporarily deployed to railway-deploy-test July 28, 2026 20:22 — with GitHub Actions Inactive
@pkg-pr-new

pkg-pr-new Bot commented Jul 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

@wasp.sh/spec

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/spec@4567

@wasp.sh/wasp-cli

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli@4567

@wasp.sh/wasp-cli-darwin-arm64-unknown

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli-darwin-arm64-unknown@4567

@wasp.sh/wasp-cli-darwin-x64-unknown

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli-darwin-x64-unknown@4567

@wasp.sh/wasp-cli-linux-arm64-glibc

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli-linux-arm64-glibc@4567

@wasp.sh/wasp-cli-linux-x64-glibc

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli-linux-x64-glibc@4567

@wasp.sh/wasp-cli-linux-x64-musl

npx https://pkg.pr.new/wasp-lang/wasp/@wasp.sh/wasp-cli-linux-x64-musl@4567

commit: 99b5d9d

Base automatically changed from filip/4529/move-dev-db-naming to main July 29, 2026 12:11
@sodic
sodic force-pushed the filip/4529/multiple-dev-dbs branch 3 times, most recently from 22d8c8e to 792d8c2 Compare July 29, 2026 12:24
@sodic
sodic temporarily deployed to railway-deploy-test July 29, 2026 13:08 — with GitHub Actions Inactive
@sodic
sodic had a problem deploying to fly-deploy-test July 29, 2026 13:08 — with GitHub Actions Failure
@sodic
sodic force-pushed the filip/4529/multiple-dev-dbs branch from 792d8c2 to cfba802 Compare July 31, 2026 14:44
@sodic
sodic temporarily deployed to fly-deploy-test July 31, 2026 15:15 — with GitHub Actions Inactive
@sodic
sodic temporarily deployed to railway-deploy-test July 31, 2026 15:15 — with GitHub Actions Inactive

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was on the fence whether to include this or not. I definitely wouldn't spend time here if I didn't have claude.

But I guess its useful, it's a test after all. LMK what you think

"fail-outside-project"
(return [waspCliDbMigrateDevFails]),
TestCase
"fail-postgresql-project-when-dev-db-not-running"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test was missing, so I added it in.

maybeUserDockerfileContents <- loadUserDockerfileContents waspDir
let dbSystem = getValidDbSystemFromPrismaSchema parsedPrismaSchema
let devDbUrl = makeDevDatabaseUrl waspDir dbSystem decls
devDbUrl <- makeDevDatabaseUrl waspDir dbSystem decls

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Before this PR, devDbUrl would always be localhost:5432, even if wasp start db isn't running. This value would get written as DATABASE_URL into the server's .env file. If the database wasn't there, the compilation would fail on the DbConnectionEstablished requirement. But if there was something there, wasp start would always try to connect to it.

With this change, if wasp start db isn't running, Wasp does not write the DATABASE_URL into the server's .env file. As mentioned in the description, this prevents our app from trying to connect onto whatever process is running on localhost:5432.

import System.Process (readProcessWithExitCode)
import Text.Read (readMaybe)

type DockerContainerName = String

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not a big fan of this, but the standard was already established in this file and I didn't want to complicate too much.

@sodic
sodic marked this pull request as ready for review July 31, 2026 17:24
@sodic
sodic requested review from FranjoMindek and removed request for infomiho July 31, 2026 17:24
@sodic
sodic deployed to fly-deploy-test July 31, 2026 17:45 — with GitHub Actions Active
@sodic
sodic temporarily deployed to railway-deploy-test July 31, 2026 17:45 — with GitHub Actions Inactive
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.

1 participant