在 heroku 上运行迁移的问题
这是这篇文章Rails、数据结构和性能的后续内容,我在其中尝试在 Rails 上创建计数器缓存。为了不将其设置为默认 0,我还添加了将该列更新为数据库中现有计数值的功能。
在开发中运行 rake db:migrate 效果很好,尽管运行需要几个小时。
但是使用heroku运行迁移给了我以下错误:
== AddVotesCount: migrating ==================================================
-- add_column(:options, :votes_count, :integer, {:default=>0})
-> 0.0196s
/Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': Operation timed out - connect(2) (Errno::ETIMEDOUT)
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:554:in `process'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:532:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:290:in `read'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:311:in `each'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command/run.rb:50:in `rake'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command.rb:114:in `run'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/bin/heroku:14:in `<top (required)>'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `load'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `<main>'
我尝试了一些不同的事情,但此时似乎迷失了,任何帮助表示赞赏!奇怪的是,错误消息并不总是立即出现,有时需要 10/20 分钟,就好像迁移正在运行一样。
谢谢
更新:这是我的迁移文件的内容:
class AddVotesCount < ActiveRecord::Migration
def self.up
add_column :options, :votes_count, :integer, :default => 0
Option.all.each do |option|
option.votes_count = option.votes.count
option.save!
end
end
def self.down
remove_column :options, :votes_count
end
end
更新2:这可以在带有heroku的暂存环境中运行...但不能在生产环境中运行
经过一些修改后,迁移代码现在是:
def self.up
add_column :options, :votes_count, :integer, :default => 0
Option.reset_column_information
Option.find_each do |option|
Option.reset_counters option.id, :votes
end
错误是:
== AddVotesCount: migrating ==================================================
-- add_column(:options, :votes_count, :integer, {:default=>0})
-> 0.0224s
/Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': getaddrinfo: nodename nor servname provided, or not known (SocketError)
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:554:in `process'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:532:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:290:in `read'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:311:in `each'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command/run.rb:50:in `rake'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command.rb:114:in `run'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/bin/heroku:14:in `<top (required)>'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `load'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `<main>'
This is a follow up to this post Rails, data structure and performance, where I have attempted to create a Counter Cache on Rails. To not have it to default 0, I have also added updating the column to the existing count value in the database.
Running rake db:migrate on development works fine, even though it took a few hours to run.
But running the migration with heroku is giving me the following error :
== AddVotesCount: migrating ==================================================
-- add_column(:options, :votes_count, :integer, {:default=>0})
-> 0.0196s
/Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': Operation timed out - connect(2) (Errno::ETIMEDOUT)
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:554:in `process'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:532:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:290:in `read'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:311:in `each'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command/run.rb:50:in `rake'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command.rb:114:in `run'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/bin/heroku:14:in `<top (required)>'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `load'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `<main>'
I have tried a few different things but seem lost at this point, any help appreciated ! The strange thing is that the error message does not always come up instantly, sometimes it takes 10/20 minutes as if the migration was running.
Thanks
Update : here is the content of my migration file :
class AddVotesCount < ActiveRecord::Migration
def self.up
add_column :options, :votes_count, :integer, :default => 0
Option.all.each do |option|
option.votes_count = option.votes.count
option.save!
end
end
def self.down
remove_column :options, :votes_count
end
end
Update 2 : This works in staging environment with heroku... but not in production
After a few modifications, the migration code is now :
def self.up
add_column :options, :votes_count, :integer, :default => 0
Option.reset_column_information
Option.find_each do |option|
Option.reset_counters option.id, :votes
end
and the error is :
== AddVotesCount: migrating ==================================================
-- add_column(:options, :votes_count, :integer, {:default=>0})
-> 0.0224s
/Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `initialize': getaddrinfo: nodename nor servname provided, or not known (SocketError)
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `open'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:644:in `connect'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
from /Users/ebellity/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/net/http.rb:626:in `start'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:554:in `process'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:532:in `get'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:290:in `read'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/client.rb:311:in `each'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command/run.rb:50:in `rake'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/lib/heroku/command.rb:114:in `run'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/gems/heroku-2.8.4/bin/heroku:14:in `<top (required)>'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `load'
from /Users/ebellity/.rvm/gems/ruby-1.9.2-p136/bin/heroku:19:in `<main>'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于那些可能面临同样错误的人,我联系了heroku,他们告诉我用这个运行迁移:
然后
出于某种原因,它起作用了......
For those who might face the same error, I got in touch with heroku and they told me to run the migration with this:
then
For some reason, it worked...