user2977636
user2977636

Reputation: 2296

Rails migration paths introduced through engine

I have a rails application that runs a rails engine by explicitly declaring it in the Gemfile. I have a script set up to install the migrations from the engine via: rake railties:install:migrations and then migrates. When I run the script, the migrations are installed but when the migrations run I get a DuplicateMigration error. I investigated this further and saw that

Rails.application.paths['db/migrate'].to_a
resulted in:

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

Somehow the migration path from the bundle directory is getting added to the Rails application paths. Where does this happen? Is there a way I can prevent the migration paths from my bundle directory from being included in the application paths?

Upvotes: 2

Views: 775

Answers (1)

user2977636
user2977636

Reputation: 2296

Okay I did some more digging and it turns out that this is actually being done in the Engine itself. In the engine.rb file there is some logic that changes the paths that are autoloaded. The core logic was as follows:

 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

This was taking the expanded paths of the migration files, and shoving them into the paths config. This is why I was seeing /bundle/bundler/gems/mycoolgem-w8ejw9jf/db/migrate in the paths config.

Upvotes: 5

Related Questions