Use schema.rb in gem

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

However, in config.paths there is no db/schema.rb and config.paths['db'] = Core::Engine.paths['db'].existent does not work.

What is the easiest way to do this?

+4
source share
5 answers

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 , the value of the SCHEMA env variable should help:

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

As I recall, database tasks were significantly changed in Rails 4, so this approach does not necessarily work in Rails 4.

Another option is to redefine the rake task in your stone.

+4
source

For anyone who stumbles upon this, with Rails 4.0 you can set the โ€œdbโ€ key in your engine configuration, and the main application will look for your circuit there.

active_record / railties / databases.rake

 module MyEngine class Engine < ::Rails::Engine initializer :override_db_dir do |app| app.config.paths["db"] = config.paths['db'].expanded end end end 
+5
source

You can get the migration from your gem-rake gem_name: install: migrations

+2
source

why don't you use custom rake task?

 desc 'Load a custom.rb file into the database' task :load_default_schema do file = ENV['SCHEMA'] || "path_to_your_file" if File.exists?(file) load(file) else abort %{#{file} doesn't exist yet.} end end 
+1
source

Not quite the same thing you can do the following so that the migration in the gem acts as if they are part of the application. Which I found more elegant solutions for my purposes instead of trying to use a circuit. Hope this helps.

Migrations to the Rails Engine?

0
source

All Articles