Running ./manage.py migrate during Heroku deployment

DjangoHeroku

Django Problem Overview


I am working on a Django app, and I would like my Database migrations to be run when deploying on Heroku.

So far we have simply put the following command in the Procfile:

python manage.py migrate

When deploying the migrations are indeed run, but they seem to be run once for each dyno (and we use several dynos). As a consequence, data migrations (as opposed to pure schema migrations) are run several times, and data is duplicated.

Running heroku run python manage.py migrate after the deployment is not satisfactory since we want the database to be in sync with the code at all times.

What is the correct way to do this in Heroku?

Thanks.

Django Solutions


Solution 1 - Django

This is my Procfile and it is working exactly as you describe:

release: python manage.py migrate
web: run-program waitress-serve --port=$PORT settings.wsgi:application

See Heroku docs on defining a release process: https://devcenter.heroku.com/articles/release-phase#defining-a-release-command

> The release command is run immediately after a release is created, but before the release is deployed to the app’s dyno formation. That means it will be run after an event that creates a new release: > > - An app build > - A pipeline promotion > - A config var change > - A rollback > - A release via the platform API > >The app dynos will not boot on a new release until the release command finishes successfully. > >If the release command exits with a non-zero exit status, or if it’s shut down by the dyno manager, the release will be discarded and will not be deployed to the app’s formation.

Be aware, however, this feature is still in beta.

Update:

When you have migrations that remove models and content types, Django requires a confirmation in the console

>The following content types are stale and need to be deleted: > >... > >Any objects related to these content types by a foreign key will also be deleted. Are you sure you want to delete these content types? If you're unsure, answer 'no'. Type 'yes' to continue, or 'no' to cancel:

The migrate command in your Procfile does not respond and the release command fails. In this scenario, remove the migrate line, push live, run the migrate command manually, then add it back for future deploys.

Solution 2 - Django

The migrate does automatically runs on Heroku, but for now you can safely do it once your dyno is deployed with heroku run python manage.py migrate.

If production, you can put your app in maintenance first with heroku maintenance:on --app=<app name here>

Solution 3 - Django

Setup your Procfile like in the docs

release: python manage.py migrate
web: gunicorn myproject.wsgi --log-file -

documented at https://devcenter.heroku.com/articles/release-phase#specifying-release-phase-tasks

Solution 4 - Django

You can create a file bin/post_compile which will run bash commands after the build.
Note that it is still considered experimental.
Read here for more buildpack info.
See here for an example

Alternatively, Heroku is working on a new Releases feature, which aims to simplify and solve this process. (Currently in Beta).

Good luck!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVincent PerezView Question on Stackoverflow
Solution 1 - DjangotomcounsellView Answer on Stackoverflow
Solution 2 - DjangocvngView Answer on Stackoverflow
Solution 3 - DjangoHarry MorenoView Answer on Stackoverflow
Solution 4 - DjangogrokpotView Answer on Stackoverflow