用于安装仅执行一次的gem迁移的Rake任务

时间:2012-10-12 19:26:05

标签: ruby-on-rails ruby ruby-on-rails-3 rake

为什么这个rake任务

gems = %w(gem1 gem2 gem3)
namespace :gems do
  namespace :install do
    desc "Runs install:migrations for all gems"
    task :migrations do
      gems.each do |gem_name|
        print "\nInstalling migrations for the #{gem_name} gem...\n"
        Rake::Task["#{gem_name}:install:migrations"].invoke
      end
      print "\n\nGem migrations installed."
    end
  end
end

实际上只运行第一组迁移,无论我使用的geng / gem order / random调用reenable?

Installing migrations for the gem1 gem...
Copied migration whatever from gem1
Copied migration whatever from gem1
Copied migration whatever from gem1
Copied migration whatever from gem1

Installing migrations for the gem2 gem...
(nothing)

Installing migrations for the gem3 gem...
(nothing)

Gem migrations installed.

1 个答案:

答案 0 :(得分:5)

invoke方法仅“按需”运行,这基本上意味着一旦运行一次,除非重新启用,否则它不会再次运行。

您可以在每个.reenable之后调用.invoke进行重置,或使用.execute命令运行任务。

.execute的警告是,如果你拥有它们,它将不会运行任务的依赖项。

Why is Rake not able to invoke multiple tasks consecutively?

How to run Rake tasks from within Rake tasks?