问题描述
推送到 Heroku 的一个不好的一面是我必须在运行我的数据库迁移之前推送代码(并且服务器会自动重新启动).
A bad side of pushing to Heroku is that I must push the code (and the server restarts automatically) before running my db migrations.
这显然会导致用户在使用新代码但没有新表/属性的情况下浏览网站时出现大约 500 个错误:Heroku 提出的解决方案是使用维护模式,但我想要一种没有缺点的方法让我的 web 应用程序运行每次!
This can obviously cause some 500 errors on users navigating the website having the new code without the new tables/attributes: the solution proposed by Heroku is to use the maintenance mode, but I want a way with no downside letting my webapp running everytime!
有办法吗?以 Capistrano 为例:
Is there a way? For example with Capistrano:
- 我准备将代码部署到新目录中
- 我运行(向后)迁移,旧代码继续完美运行
- 我将 mongrel 实例切换到新目录并重新启动服务器
...而且我没有停机时间!
...and I have no downtime!
推荐答案
您可以设置第二个 Heroku 应用程序,该应用程序指向与您的主要生产应用程序相同的数据库,并使用辅助应用程序运行您的数据库迁移而不中断生产(假设迁移不会破坏您应用的先前版本).
You could setup a second Heroku app which points to the same DB as your primary production app and use the secondary app to run your DB migrations without interrupting production (assuming the migrations don't break the previous version of your app).
让我们将 Heroku 应用称为 PRODUCTION 和 STAGING.
Let's call the Heroku apps PRODUCTION and STAGING.
你的部署顺序会变成这样:
Your deploy sequence would become something like:
- 将新代码部署到STAGING
git push heroku 暂存
- 在 STAGING 上运行数据库迁移(以更新 PROD db)
heroku run -a staging-app rake db:migrate
- 将新代码部署到生产
git push heroku 制作
- Deploy new code to STAGING
git push heroku staging
- Run database migrations on STAGING (to update PROD db)
heroku run -a staging-app rake db:migrate
- Deploy new code to PRODUCTION
git push heroku production
暂存应用程序不会花费您任何费用,因为您不需要超过 Heroku 的免费套餐,而且设置一个 rake 部署脚本来自动为您执行此操作非常简单.
The staging app won't cost you anything since you won't need to exceed Heroku's free tier and it would be pretty trivial to setup a rake deploy script to do this for you automatically.
祝你好运!
这篇关于在 Heroku 上热部署,无需停机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!