2013-08-22 72 views
2

现在我正在开发一个Rails应用程序与安装在发动机。Rake任务的自动迁移引擎迁移

我thoughed这将是一个好主意,编写一个耙TAST,这将迁移从引擎复制并运行rake db:migrate。但是,如果我在engine-Arry(见下文)中只使用一个引擎运行rake任务,则rake tast将复制引擎中的迁移并迁移数据库。但是如果我将另一个引擎添加到数组中,rake-Task将不再工作。

namespace :work_in_progress do 
    desc 'Migrate the engines db tables' 
    task migrate_migrations_from_engines: :environment do 
    # The array with the available engines (just add the new engine here) 
    engines = [ 
     'engine_one', 
     'engine_two' 
    ] 

    puts 'Migrating migrations from engines...' 
    engines.each do |engine| 
     puts 'Copying migrations from ' + engine 
     Rake::Task[engine + ':install:migrations'].invoke 
    end 
    puts 'Migrating the database...' 
    Rake::Task['db:migrate'].invoke 
    puts 'Done...' 
    end 

end 

如何改进上面的脚本,以便可以迁移多个引擎? 是否有解决此问题的其他脚本(复制从引擎迁移并运行它们?)?

非常感谢!

菲利普

+0

您是否收到错误? – cortex

+0

没有错误。只有“放入”写入控制台,但没有其他事情发生。 – pmuens

+0

尝试'执行'而不是'调用'。 – cortex

回答

4

你将不得不运行rake任务来安装迁移,然后运行这些。试试这个代码来执行任务:

namespace :work_in_progress do 
    desc 'Migrate the engines db tables' 
    task migrate_migrations_from_engines: :environment do 
    # The array with the available engines (just add the new engine here) 
    engines = ['engine_one','engine_two'] 
    puts 'Migrating migrations from engines...' 
    engines.each do |engine| 
     puts 'Copying migrations from ' + engine 
     `bundle exec rake #{engine}:install:migrations`  
    end 
    puts 'Migrating the database...' 
    `bundle exec rake db:migrate`  
    puts 'Done...' 
    end 

end 
+0

非常感谢!这个解决方案有效 对于每个阅读此内容的人:请注意标题为“特殊”的引号。否则,它不会工作! – pmuens

+0

想知道为什么原稿不运行。进入完全相同的问题。 –