Migrating the Database
Learn what ways we can adapt to migrate the database.
Ways to migrate DB
The postgres
image automatically creates the default database if it does not exist. However, currently, nothing ensures that the
migrations have been run. In development, we just did this manually by:
docker-compose exec web bin/rails db:migrate
Using .sh file
We need to ensure that our app has a fully migrated database when it launches. One way you might think of achieving this is with the entry-point concept we used to solve the server PID issue. We already have a docker-entrypoint.sh file that is run just prior to launching our app. This might lead you to try migrating the database as follows:
#!/bin/shset -eif [ -f tmp/pids/server.pid ]; thenrm tmp/pids/server.pidfi# BAD IDEA...bin/rails db:migrateexec "$@"
Why .sh won’t work?
Although it seems like this approach should work, unfortunately, it does not scale well. Were you to start three replicas of your app at the same time, each would try to migrate the database, which can lead to locking issues that prevent the app from starting. Another downside is that you cannot migrate the database independently; it is tied to the app launch. ...