在Heroku部署期间运行./manage.py进行迁移 [英] Running ./manage.py migrate during Heroku deployment

查看:165
本文介绍了在Heroku部署期间运行./manage.py进行迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Django应用程序,并且希望在部署到Heroku上时运行数据库迁移。



到目前为止,我们只需将以下命令在Procfile中:

  python manage.py migrate 

当部署迁移确实运行时,但它们对于每个dyno似乎只运行一次(并且我们使用多个dynos)。因此,数据迁移(与纯粹的模式迁移相对)会多次运行,并且数据将被复制。



运行 heroku run python manage .py migrate 在部署之后并不令人满意,因为我们希望数据库始终与代码同步。



什么是正确的方法在Heroku中做到这一点?



谢谢。

解决方案

这是我的 Procfile ,它的工作原理与您描述的完全一样:

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



请参阅Heroku文档定义发布流程:
https://devcenter.heroku.com/articles/release-phase#defining-a-release-command


释放命令是在发布之后立即运行,但在将发布部署到应用程序的测试版之前立即运行。这意味着它将在创建新版本的事件之后运行:
$ b


  • 应用程序构建
  • >管道促销
  • 配置变更

  • 回滚

  • 通过平台API发布



直到release命令成功完成,应用dynos才会在新版本上启动。



如果释放命令以非零退出状态退出,或者由dyno管理器关闭,则释放将被丢弃,并且不会部署到应用程序阵列。


但是,请注意,此功能仍处于测试阶段。



更新:



如果您有移除模型和内容类型的迁移,Django需要在控制台中进行确认


以下内容类型陈旧并需要删除: 任何对象与外键相关的这些内容类型也将被删除。您确定要删除这些内容类型吗?如果您不确定,请回答否。输入'yes'继续,或'no'取消:

您的Procfile中的migrate命令没有响应,释放命令失败。在这种情况下,移除迁移线,直播,手动运行迁移命令,然后将其添加回未来的部署。


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.

解决方案

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.

这篇关于在Heroku部署期间运行./manage.py进行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆