-
Notifications
You must be signed in to change notification settings - Fork 17
Move django migrations to dokku release phase #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
66e6228
Move migrations into dokku `release` phase
rw251 b27998e
Fix CI yml files to output logs on failure
rw251 91a3e9f
Fix the smoke test
rw251 28c1e03
Fix docker/storage dir to always have correct owner
rw251 d2b6ab0
Added ADR
rw251 569792b
Tidy up following review
rw251 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| release: /usr/bin/env bash /app/deploy/release.sh | ||
| web: gunicorn --config /app/deploy/gunicorn/conf.py opencodelists.wsgi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Gets executed during dokku's release phase as specified in Procfile. | ||
| # This is where dokku recommends running db migrations. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| ./manage.py check --deploy | ||
| ./manage.py migrate |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # ADR: Run Django Migrations in Dokku Release Phase | ||
|
|
||
| Date: 2025-07 | ||
|
|
||
| ## Status | ||
|
|
||
| Accepted | ||
|
|
||
| ## Context | ||
|
|
||
| Previously, we had a `prod.sh` script that ran the database migrations and then called `exec "$@"` in order to execute whatever command was passed as arguments to the script. The Dockerfile was | ||
|
|
||
| ```docker | ||
| ENTRYPOINT ["/app/docker/entrypoints/prod.sh"] | ||
| CMD ["gunicorn", "--config", "/app/deploy/gunicorn/conf.py", "opencodelists.wsgi"] | ||
| ``` | ||
|
|
||
| Meaning that when the docker container was run, without additional arguments, this command would execute: | ||
|
|
||
| ```bash | ||
| prod.sh gunicorn --config /app/deploy/gunicorn/conf.py opencodelists.wsgi | ||
| ``` | ||
| Causing the migrations to run, and gunicorn to start. You could also pass a command to `docker run` which would then execute (instead of gunicorn) after the migrations. | ||
|
|
||
| With this setup, migrations were run every time the container started, including during the web process startup and health checks. This approach had caused issues, including an outage to job-server where migrations were not fully applied before the health checks timed out. | ||
|
|
||
| [Dokku recommend](https://dokku.com/docs/advanced-usage/deployment-tasks/) running database migrations in the `release` phase, which is executed before the new web process is started. This ensures migrations are applied atomically and safely, and that the web process only starts after the database schema is up to date. | ||
|
|
||
| ## Decision | ||
|
|
||
| - **Create** a `release.sh` script that runs the migrations and checks: | ||
| ```bash | ||
| #!/bin/bash | ||
| set -e | ||
|
|
||
| ./manage.py check --deploy | ||
| ./manage.py migrate | ||
| ``` | ||
| - **Move** the migration and check logic to the `release` phase in the `Procfile` and add the `web` command: | ||
| ``` | ||
| release: /usr/bin/env bash /app/deploy/release.sh | ||
| web: gunicorn --config /app/deploy/gunicorn/conf.py opencodelists.wsgi | ||
| ``` | ||
| - **Remove** the `ENTRYPOINT` from the production Docker image. | ||
| - **Delete** the now-obsolete `prod.sh` entrypoint script. | ||
| - **Keep** the `CMD` in the Dockerfile so `docker run` will still start Gunicorn by default, but can be overriden | ||
|
|
||
| ## Consequences | ||
|
|
||
| - Migrations are now run in the correct place (Dokku's release phase), before the web process starts. | ||
| - Deploys are safer and more predictable, with no risk of the health checks failing because they start at the same time as the container starts. | ||
| - No scripts or documentation referenced or used the old `prod.sh` entrypoint, so no further changes were needed following its deletion. | ||
| - The dev docker image overwrites the ENTRYPOINT and so is unaffected by its removal | ||
| - The `CMD` in the Dockerfile remains, allowing for overriding of the command when running the container. | ||
|
|
||
|
|
||
| ## Changes | ||
|
|
||
| - `docker/Dockerfile`: Removed ENTRYPOINT, kept CMD for Gunicorn. | ||
| - `docker/entrypoints/prod.sh`: Deleted. | ||
| - `deploy/release.sh`: Created for running migrations and checks. | ||
| - `Procfile`: Added release phase for migrations and checks, and web phase for Gunicorn. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.