自动迁移引擎迁移的Rake任务

3
我现在正在开发一个Rails应用程序,其中挂载了Engines。
我认为编写一个rake任务是个好主意,它会从Engine复制迁移并运行rake db:migrate。
然而,如果我只在engines-Arry中运行一个Engine的rake任务,rake任务将会从Engine复制迁移并迁移db。但如果我向数组中添加另一个Engine,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
      Rake::Task[engine + ':install:migrations'].invoke
    end
    puts 'Migrating the database...'
    Rake::Task['db:migrate'].invoke
    puts 'Done...'
  end

end

如何改进上述脚本,以便能够迁移多个引擎? 是否有其他脚本可以解决此问题(复制引擎中的迁移并运行它们)? 非常感谢! Philipp

没有错误。只有“puts”写入控制台,但没有其他操作发生。 - pmuens
尝试使用execute代替invoke - cortex
@pmuens:你尝试过回答中提到的解决方案吗? - Aman Garg
@AmanGarg,你的代码执行不起作用,抱歉。但是我尝试了下面的答案,对我很有帮助! - pmuens
1个回答

5
您需要运行rake任务来安装迁移,然后运行这些任务。尝试使用以下代码执行任务:

您将需要运行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

非常感谢!这个解决方案可行! 对于所有阅读此内容的人:请注意“特殊”的标题引号。否则,它将无法正常工作! - pmuens
很想知道为什么原始代码无法运行。我也遇到了完全相同的问题。 - Abe Petrillo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接