the-bed-stack icon indicating copy to clipboard operation
the-bed-stack copied to clipboard

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

Open yamcodes opened this issue 1 year ago • 1 comments

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

  • [X] Read the Contributing Guide.
  • [X] Read the README.md.
  • [X] Check that there isn't already an issue that requests the same feature.

yamcodes avatar Oct 09 '23 12:10 yamcodes

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

Hajbo avatar Oct 18 '23 19:10 Hajbo