[Django]-Running ./manage.py migrate during Heroku deployment

81👍

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.

32👍

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>

👤cvng

5👍

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

3👍

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!

Leave a comment