在heroku上运行迁移的问题

时间:2011-09-27 09:36:46

标签: ruby-on-rails heroku migration counter-cache

这是对这篇文章Rails, data structure and performance的跟进,我试图在Rails上创建一个Counter Cache。为了不将它设置为默认值0,我还添加了将列更新为数据库中的现有计数值。

运行rake db:migrate on development工作正常,即使它花了几个小时才能运行。

但是使用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>'

1 个答案:

答案 0 :(得分:2)

对于那些可能面临同样错误的人,我与heroku联系,他们告诉我用这个来运行迁移:

$ heroku run bash --app appname

然后

$ rake db:migrate

出于某种原因,它有效......