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

十年热恋 提交于 2019-12-19 05:24:11

问题


If using the this option in config/application.rb:

config.active_record.schema_format = :sql

then when you do:

rake db:migrate

it only dumps the db/structure.sql. I know it isn't using the db/schema.rb since it is using the :sql option, but how can you make rake db:migrate generate db/schema.rb also?

We need that because RubyMine 4.5 and IntelliJ IDea 11 use db/schema.rb for autocompletion of columns.


回答1:


To generate/update db/schema.rb even if using the :sql option, you can put this in your Rakefile:

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

That should be fine for IDea and RubyMine.

For others that just want the file for reference, you might want to rename it to something else like db/schema.rb.backup so it won't be confusing. To do that:

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 Rakefile because __FILE__ evaluates to a path that ends in /Rakefile.)



来源:https://stackoverflow.com/questions/13646840/how-to-make-rake-dbmigrate-generate-schema-rb-when-using-sql-schema-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!