這是本帖子Rails, data structure and performance的後續內容,我嘗試在Rails上創建計數器緩存。爲了不讓它默認爲0,我還添加了將列更新到數據庫中現有的計數值。在heroku上運行遷移時出現問題
運行rake db:遷移開發工作正常,即使花了幾個小時運行。
但使用了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
and er ROR是:
== 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>'
你的遷移文件的內容是什麼? – Dogbert
@Dogbert只編輯了這個問題,添加了遷移文件 – Emmanuel
Completey的幫助,但是你真的需要這種遷移嗎?你能不能在Option模型中創建一個名爲votes_count的方法,該方法返回votes.count? – Yule