Search code examples
ruby-on-railsrubyrake

Use schema.rb in gem


I'd like rake db:schema:load to use a db/schema.rb that's not located in my app, but in one of my gems. This already works for db:seed by putting config.paths['db/seeds'] = Core::Engine.paths['db/seeds'].existent in my application.rb. (Core is a gem that's also a Rails engine).

However, there is no db/schema.rb path in config.paths and config.paths['db'] = Core::Engine.paths['db'].existent has no effect.

What's the easiest way to get this done?


Solution

  • According to the Rails 3.2 source code https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/railties/databases.rake#L400 , setting SCHEMA env variable should help:

    ENV['SCHEMA'] = Core::Engine.paths['db'].existent
    

    As I remember, the database tasks has been changed significantly in Rails 4, so this approach doesn't necessarily work in Rails 4.

    Another option is to override the rake task itself in your gem.