【问题标题】:Heroku database rake with loop带循环的 Heroku 数据库 rake
【发布时间】:2011-06-20 13:55:18
【问题描述】:

我正在尝试在 heroku 上运行此迁移,但它挂在循环中。

puts "0"
add_column :batches, :store_id, :integer
add_column :batches, :company_id, :integer
puts "1"
for batch in Batch.all()
    puts "2"
    batch.company_id = batch.register.store.company.id.to_i
    puts "3"
    batch.store_id = batch.register.store.id.to_i
    puts "4"
    batch.save
    puts "5"
end
puts "6"

不幸的是,我无法确切知道它挂在哪里,因为我的“放置”都没有显示在控制台中。我得到的最后一行是:

 -- add_column(:batches, :store_id, :integer)

我无法通过在 heroku 上运行的循环进行任何迁移,但它们在本地都可以正常工作,我做错了什么吗?

heroku 日志的输出:

2011-06-20T13:58:15+00:00 app[rake.14]: Starting process with command `rake db:migrate --trace`
2011-06-20T13:58:48+00:00 heroku[web.1]: Stopping process with SIGTERM
2011-06-20T13:58:48+00:00 app[web.1]: >> Stopping ...
2011-06-20T13:58:48+00:00 heroku[web.1]: Process exited

【问题讨论】:

    标签: sql ruby-on-rails heroku rake


    【解决方案1】:

    问题可能是因为您没有包括我在下面显示的环境;您也可以在 heroku 控制台中运行它

    In your apps shell/terminal

    heroku console
    

    然后将它作为一个块运行,因为 heroku 不会让你运行 do/end 块

    Batch.all.each {|b| b.update_attributes(:company => b.register.store.company, :store => b.register.store)}
    

    所以这应该在控制台中工作,除非其中一个字段为空,这就是为什么你需要一个 rake 任务来处理错误。

    试试这个:

    lib/tasks/add_bathces.rake

    task :add_bathces => :environment do 
      Batch.all.each do |batch|
        if batch.register.store.comapny &&  batch.register.store
            batch.update_attributes(:company => b.register.store.company, :store => b.register.store)
        else
          puts "Store or company were nill"
        end
      end  
    end
    

    【讨论】:

    • 有没有办法从迁移中调用此任务?还是这种类型的数据操作完全超出了 Heroku 迁移的范围?
    • sh#t,那是用来耙的。
    • 您可以从迁移中调用它。
    • 那么与您的数据库有关。我一直在做这些事情。
    猜你喜欢
    • 2011-08-03
    • 2011-10-04
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2014-11-29
    相关资源
    最近更新 更多