Running ./manage.py migrate during Heroku deployment

后端 未结 4 973
遥遥无期
遥遥无期 2020-12-23 13:47

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 Proc

相关标签:
4条回答
  • 2020-12-23 13:58

    Procfile

    release: python manage.py migrate --noinput
    web: gunicorn mysite.wsgi
    

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

    0 讨论(0)
  • 2020-12-23 14:00

    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.

    0 讨论(0)
  • 2020-12-23 14:07

    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>

    0 讨论(0)
  • 2020-12-23 14:16

    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!

    0 讨论(0)
提交回复
热议问题