【问题标题】:Rails migration paths introduced through engine通过引擎引入的 Rails 迁移路径
【发布时间】:2017-10-11 23:43:05
【问题描述】:

我有一个 rails 应用程序,它通过在 Gemfile 中明确声明来运行 rails 引擎。我设置了一个脚本,通过:rake railties:install:migrations 从引擎安装迁移,然后迁移。当我运行脚本时,迁移已安装,但当迁移运行时,我收到 DuplicateMigration 错误。我对此进行了进一步调查,发现

Rails.application.paths['db/migrate'].to_a
结果:

["/src/db/migrate","/bundle/bundler/gems/my_engine-w8ejw9jf/db/migrate"]

不知何故,来自 bundle 目录的迁移路径被添加到 Rails 应用程序路径中。这发生在哪里?有没有办法可以防止我的包目录中的迁移路径包含在应用程序路径中?

【问题讨论】:

    标签: ruby-on-rails rails-migrations rails-engines


    【解决方案1】:

    好的,我做了更多的挖掘,结果证明这实际上是在引擎本身中完成的。在engine.rb 文件中有一些逻辑会更改自动加载的路径。核心逻辑如下:

     class Engine < ::Rails::Engine
       isolate_namespace MyEngine
    
       initializer :append_migrations do |app|
         unless app.root.to_s.match root.to_s
           config.paths["db/migrate"].expanded.each do |expanded_path|
             app.config.paths["db/migrate"] << expanded_path
           end
         end
       end
    end
    

    这是采用迁移文件的扩展路径,并将它们推入路径配置。这就是我在路径配置中看到/bundle/bundler/gems/mycoolgem-w8ejw9jf/db/migrate 的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多