I am working on a Django app, and I would like my Database migrations to be run when deploying on Heroku.
我正在开发一个Django应用程序,我希望在Heroku上部署时可以运行我的数据库迁移。
So far we have simply put the following command in the Procfile:
到目前为止,我们只是将以下命令放在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.
当部署迁移确实运行时,但它们似乎每个dyno运行一次(我们使用几个dynos)。因此,数据迁移(与纯模式迁移相反)会多次运行,并且数据会重复。
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.
在部署不满意后运行heroku运行python manage.py migrate,因为我们希望数据库始终与代码同步。
What is the correct way to do this in Heroku?
在Heroku中执行此操作的正确方法是什么?
Thanks.
谢谢。
3 个解决方案
#1
36
This is my Procfile and it is working exactly as you describe:
这是我的Procfile,它完全按照你的描述工作:
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
有关定义发布过程的信息,请参阅Heroku文档: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:
发布命令在创建发布后立即运行,但在将发布部署到应用程序的dyno组合之前。这意味着它将在创建新版本的事件之后运行:
- An app build
- 应用程序构建
- A pipeline promotion
- 管道推广
- A config var change
- 配置变化
- A rollback
- 回滚
- A release via the platform API
- 通过平台API发布
The app dynos will not boot on a new release until the release command finishes successfully.
在发布命令成功完成之前,应用程序dynos将无法在新版本上启动。
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.
如果release命令以非零退出状态退出,或者由dyno管理器关闭,则释放将被丢弃,并且不会部署到应用程序的构造中。
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
当您具有删除模型和内容类型的迁移时,Django需要在控制台中进行确认
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.
Procfile中的migrate命令没有响应,release命令失败。在此方案中,删除迁移行,实时推送,手动运行迁移命令,然后将其重新添加以备将来部署。
#2
10
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
.
迁移会自动在Heroku上运行,但是现在,一旦使用heroku运行python manage.py migrate部署dyno,就可以安全地执行迁移。
If production, you can put your app in maintenance first with heroku maintenance:on
如果是生产,您可以先使用heroku维护将您的应用程序置于维护状态:打开
#3
2
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
您可以创建一个文件bin / post_compile,它将在构建后运行bash命令。请注意,它仍然被认为是实验性的。请阅读此处获取更多buildpack信息。请看这里的例子
Alternatively, Heroku is working on a new Releases feature, which aims to simplify and solve this process. (Currently in Beta).
另外,Heroku正在开发一个新的Releases功能,旨在简化和解决这个过程。 (目前处于测试阶段)。
Good luck!
祝你好运!
#1
36
This is my Procfile and it is working exactly as you describe:
这是我的Procfile,它完全按照你的描述工作:
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
有关定义发布过程的信息,请参阅Heroku文档: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:
发布命令在创建发布后立即运行,但在将发布部署到应用程序的dyno组合之前。这意味着它将在创建新版本的事件之后运行:
- An app build
- 应用程序构建
- A pipeline promotion
- 管道推广
- A config var change
- 配置变化
- A rollback
- 回滚
- A release via the platform API
- 通过平台API发布
The app dynos will not boot on a new release until the release command finishes successfully.
在发布命令成功完成之前,应用程序dynos将无法在新版本上启动。
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.
如果release命令以非零退出状态退出,或者由dyno管理器关闭,则释放将被丢弃,并且不会部署到应用程序的构造中。
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
当您具有删除模型和内容类型的迁移时,Django需要在控制台中进行确认
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.
Procfile中的migrate命令没有响应,release命令失败。在此方案中,删除迁移行,实时推送,手动运行迁移命令,然后将其重新添加以备将来部署。
#2
10
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
.
迁移会自动在Heroku上运行,但是现在,一旦使用heroku运行python manage.py migrate部署dyno,就可以安全地执行迁移。
If production, you can put your app in maintenance first with heroku maintenance:on
如果是生产,您可以先使用heroku维护将您的应用程序置于维护状态:打开
#3
2
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
您可以创建一个文件bin / post_compile,它将在构建后运行bash命令。请注意,它仍然被认为是实验性的。请阅读此处获取更多buildpack信息。请看这里的例子
Alternatively, Heroku is working on a new Releases feature, which aims to simplify and solve this process. (Currently in Beta).
另外,Heroku正在开发一个新的Releases功能,旨在简化和解决这个过程。 (目前处于测试阶段)。
Good luck!
祝你好运!