Discover dev db port from Docker at compile time - #4567
Conversation
Deploying wasp-docs-on-main with
|
| 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 |
fe5e788 to
5447ef7
Compare
@wasp.sh/spec
@wasp.sh/wasp-cli
@wasp.sh/wasp-cli-darwin-arm64-unknown
@wasp.sh/wasp-cli-darwin-x64-unknown
@wasp.sh/wasp-cli-linux-arm64-glibc
@wasp.sh/wasp-cli-linux-x64-glibc
@wasp.sh/wasp-cli-linux-x64-musl
commit: |
22d8c8e to
792d8c2
Compare
792d8c2 to
cfba802
Compare
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Not a big fan of this, but the standard was already established in this file and I didn't want to complicate too much.
Description
Part of #4529 (running multiple dev databases in parallel). Stacked on #4568.
Before this PR:
wasp start dbalways starts the database on port 5432.wasp startalways looks for the dev database on port 5432.wasp startlooks for the DB onlocalhost:5432:wasp start dbis running, all good.localhost:5432, it fails using theDbConnectionEstablishedrequirement. The user gets a nice error.localhost:5432,wasp startwill 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:
wasp startalways starts the database on port 5432, this is unchanged and coming in the next PR.wasp startdiscovers the dev database port by asking docker. It relies on the container's name, which is the project's path-based unique ID.wasp startlooks for the DB on the output ofdocker port uniqueContainerName:wasp start dbis running,docker portreturns the correct port, and all good.wasp start dbisn't running, the user gets a nice error.wasp startwill no longer connect to whatever is running on 5432, wich means no more database hijacking (at least not until runtime).Type of change
Checklist
I tested my change in a Wasp app to verify that it works as intended.
🧪 Tests and apps:
examples/kitchen-sink/e2e-tests.waspc/data/Cli/templates, as needed.examples/, as needed.examples/tutorials) I updated the tutorial in the docs (and vice versa).📜 Documentation:
web/docs/.🆕 Changelog: (if change is more than just code/docs improvement)
waspc/ChangeLog.mdwith a user-friendly description of the change.web/docs/migration-guides/.versioninwaspc/waspc.cabalto reflect the changes I introduced.