agnyz / the-bed-stack

Bun + ElysiaJS + DrizzleORM = the stack you don't want to sleep on 🛌💤
https://agnyz.github.io/the-bed-stack/
MIT License
39 stars 3 forks source link

Properly wait for database creation instead of relying on `sleep` #56

Open yamcodes opened 1 year ago

yamcodes commented 1 year ago

Clear and concise description of the problem

Currently in scripts/create-db.sh, we rely on the following command to ensure that the database has been created, before running migrations:

sleep 10

Link to line in codebase

Obviously, this is unreliable, since different machines can take different time to create the database.

Importantly, the reason this is happening is that we are applying the migrations through postCreateCommand. This command is executed without waiting for the database to be created.

Suggested solution

We can avoid doing this in postCreateCommand, and add logic to the database instructions directly inside docker-compose.yml.

The problem with this is I'm unsure how it will work with drizzle-orm, since the db image is created before initializing and installing bun.

Alternative

Alternatively, if we wish to keep using postCreateCommand:

  1. Rely on scripts like wait-for-it.sh. The problem with this approach is that it just waits for the db server to run, not for the database to be created.
  2. Rely on the healthcheck implemented in docker-compose.yml, through pg_isready.

Additional context

It would be great to find real world examples for devcontainers that succesfully apply migrations when the container is built.

Validations

Hajbo commented 1 year ago

I think we can solve it with some changes to the docker compose file, I'll play around with it a bit