Rails迁移不会在Google App Engine部署上运行 [英] Rails migrations don't run on Google App Engine deploy

查看:79
本文介绍了Rails迁移不会在Google App Engine部署上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在部署到Google App Engine时运行Rails迁移( gcloud app deploy )?



I尝试使用Cloud SQL将Rails 5(Ruby 2.3.1)应用部署到Google App Engine。我遵循Google的书架教程和他们的 GitHub云端SQL回购设置app.yaml和database.yml。



部署成功完成,我可以查看我的应用程序的登录页面,但是当我登录或注册页面时,我得到一个500错误,因为用户表尚未被创建。



部署日志的第8步似乎提到资产预编译

 第8步:运行if test -d app / assets -a -f config / application.rb;然后捆绑exec rake资产:预编译||真正; fi 

但是我没有看到任何类似于 db:create db:migrate ,我需要在部署时运行。



仅供参考运行 rake db:migrate ,并且所有内容都按预期在本地运行。



谢谢!

解决方案

运行 gcloud应用程序部署后无法运行。我们也可能不希望从容器中运行它,因为它们中有几个,如果我们将它们运行在所有容器上,那么会出现一种竞争状态,其中有时候一些迁移会运行两次,或者一个会在另一个完成之前运行。因此,在您部署的计算机上运行它们是有意义的。幸运的是,使用Google云端SQL代理很容易(假设您当然使用Cloud SQL)。请参阅以下链接以获取文档: https://cloud.google。 com / sql / docs / mysql / connect-external-app#proxy

首先,我们设置了自己的云SQL代理(这些都是针对Linux的) :




wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64
chmod + x cloud_sql_proxy.linux.amd64
sudo mv cloud_sql_proxy.linux.amd64 / usr / local / bin / cloud-sql-proxy



然后使用 gcloud ,登录并将默认项目设置为您当前使用的项目(因为云SQL代理使用gcloud配置来计算出如何连接):




gcloud config set project<您的项目名称> #我们不想部署或连接到错误的项目!
gcloud auth list#确保你正在处理的账户应该是
gcloud auth application-default login



最后一步是必要的,因为我正在从Google Compute Engine虚拟机进行部署,该虚拟机已使用没有正确凭据的服务帐户登录。



然后设置云端sql代理套接字目录。这也适用于Postgres。请确保它与生产配置所期望的目录相同:


sudo mkdir / cloudsql
sudo chown $ USER / cloudsql



然后在另一个窗口或后台启动云端SQL代理:




cloud-sql-proxy -dir / cloudsql



然后运行你的部署任务和迁移:
$ b $ p $ gcloud app deploy
RAILS_ENV =生产rake db:migrate



当我刚刚设置时,我感到沮丧的是,如果您在部署之前运行迁移需要很长时间!),您的网站将会关闭。如果您之后运行迁移,则根据运行状况检查的设置,它可能会检测到新版本不健康。由于缺省情况下未在生产环境中设置 config.active_record.migration_error ,因此我认为最好的选择是在部署之后运行迁移,除非您要设置运行状况检查如果存在挂起的迁移,健康检查端点将引发异常。然后,您可以在迁移完成的准确时间将应用程序移至新版本。但是,由于定期运行健康检查的性质,您仍然会有几秒的停机时间。上面的两行脚本(在部署之后运行迁移)可能是您无需付出更多努力即可获得的最佳选择。


How do you run Rails migrations upon deploy to Google App Engine (gcloud app deploy)?

I'm trying to deploy a Rails 5 (Ruby 2.3.1) app to Google App Engine using Cloud SQL. I followed the steps outlined in Google's Bookshelf tutorial and their GitHub repo for Cloud SQL to set up app.yaml and database.yml.

The deploy completes successfully and I'm able to view the landing page of my app, but when I go to the Sign In or Sign Up pages, I get a 500 error because the Users table hasn't been created.

Step 8 of the deploy log seems to mention asset precompile

Step 8 : RUN if test -d app/assets -a -f config/application.rb; then       bundle exec rake assets:precompile || true;     fi

But I don't see anything like db:create and db:migrate, which I need to run when I deploy.

FYI, I have run rake db:migrate and everything is working as expected on local.

Thanks!

解决方案

You're correct that it isn't run after running gcloud app deploy. We also probably wouldn't want to run it from the containers, because there are several of them, and if we ran it on all of them then there would be a race condition in which sometimes some of the migrations would run twice, or perhaps one would run before another is finished. Therefore it makes sense to run them on the machine from which you are deploying. Fortunately, that's easy using the Google Cloud SQL Proxy (this assumes you're using Cloud SQL of course). See this link for documentation: https://cloud.google.com/sql/docs/mysql/connect-external-app#proxy

First, we set up the cloud SQL proxy itself (these are for Linux):

wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 chmod +x cloud_sql_proxy.linux.amd64 sudo mv cloud_sql_proxy.linux.amd64 /usr/local/bin/cloud-sql-proxy

Then using gcloud, login and set the default project to the one you're currently working with (because the cloud SQL proxy uses the gcloud configuration to figure out how to connect):

gcloud config set project <your project name> # we don't want to deploy or connect to the wrong project! gcloud auth list # make sure you're dealing with the account you should be gcloud auth application-default login

The last step was necessary because I'm deploying from a Google Compute Engine VM, which already was logged in with a service account which didn't have the right credentials.

Then set up the cloud sql proxy socket directory. This works with Postgres as well. Just make sure that it's the same directory that your production config expects:

sudo mkdir /cloudsql sudo chown $USER /cloudsql

Then start the cloud sql proxy in another window or in the background:

cloud-sql-proxy -dir /cloudsql

Then run your deploy task and migration:

gcloud app deploy RAILS_ENV=production rake db:migrate

When I just set this up, I was frustrated by the fact that if you run the migration before your deployment (which takes a long time!), your site will be down. And if you run the migration after, then depending on how health checks are set up, it may detect that the new version is unhealthy. Since config.active_record.migration_error isn't set on production by default, I think the best option is to run the migration after the deploy unless you want to set up health checks in such a way that a health check endpoint will raise an exception if there are pending migrations. Then you could have your application move to the new version at the exact time when migrations are finished. However because of the nature of health checks running periodically, you'll still have a few seconds of downtime. The two-line script above (running the migrations after the deploy) is probably the best you can get without a whole lot more effort.

这篇关于Rails迁移不会在Google App Engine部署上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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