LoginSignup
9
9

More than 5 years have passed since last update.

RailsのEngineでmigrationを幾分シンプルにする方法

Posted at

railsでengineを利用する際に、migrationを実行する場合、

$ rake railties:install:migrations
$ rake db:migrate

RailsGuideだとこのような感じで、
一旦engine側のmigrationファイルをコピーしてから実行するというフローになっているのですが、
正直面倒だったりします。

そこで、rails db:migrate一発で済ます方法がこの記事にある通り。
ただ、これだと rake db:migrate:reset 叩いた時にengine側のmigrationが走ってくれないという問題があり、
最終的に以下に落ち着きました。

module EngineWithMigrations
  class Engine < ::Rails::Engine
    isolate_namespace EngineWithMigrations

    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
        ActiveRecord::Tasks::DatabaseTasks.migrations_paths = app.config.paths['db/migrate'].to_a # これがないと db:migrate:reset がうまく動かない
      end
    end
  end
end

動作環境: ruby2.3.0, rails4.2.5

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9