How to make rake db: migrate generate schema.rb when using: sql schema format

If this parameter is used in config/application.rb :

 config.active_record.schema_format = :sql 

then when you do:

 rake db:migrate 

it only removes db/structure.sql . I know that it does not use db/schema.rb since it uses the :sql parameter, but how can you make rake db:migrate generate db/schema.rb as well?

We need RubyMine 4.5 and IntelliJ IDea 11 to use db/schema.rb to autocomplete columns.

+6
ruby ruby-on-rails-3 rake
source share
1 answer

To generate / update db/schema.rb even if you use the :sql parameter, you can put it in your Rakefile :

 Rake::Task["db:migrate"].enhance do if ActiveRecord::Base.schema_format == :sql Rake::Task["db:schema:dump"].invoke end end 

This should be good for IDea and RubyMine.

For others who just want the file to reference it, you can rename it to something like db/schema.rb.backup so that it doesn't get confused. For this:

 Rake::Task["db:migrate"].enhance do if ActiveRecord::Base.schema_format == :sql Rake::Task["db:schema:dump"].invoke File.rename(File.expand_path('../db/schema.rb', __FILE__), File.expand_path('../db/schema.rb.backup', __FILE__)) end end 

(Note: Using ../ in paths in a Rakefile , because __FILE__ evaluates a path ending in /Rakefile .)

+8
source share

All Articles