使用:sql模式格式时如何使rake db:migrate生成schema.rb [英] How to make rake db:migrate generate schema.rb when using :sql schema format

查看:146
本文介绍了使用:sql模式格式时如何使rake db:migrate生成schema.rb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在 config / application.rb 中使用此选项:

  config.active_record.schema_format =:sql 

然后在执行操作时:

  rake db:migrate 

它仅转储 db / structure.sql 。我知道它没有使用 db / schema.rb ,因为它正在使用:sql 选项,但是如何使用你还能使 rake db:migrate 生成 db / schema.rb 吗?



我们需要这样做,因为RubyMine 4.5和IntelliJ IDea 11使用 db / schema.rb 自动完成列。

解决方案

即使使用:sql <,也可以生成/更新 db / schema.rb 。 / code>选项,可以将其放入您的 Rakefile

  Rake :: Task [ db:migrate]。如果ActiveRecord :: Base.schema_format ==:sql 
Rake :: Task [ db:schema:dump],则增强
。调用
结束
结束

对于IDea和RubyMine应该很好。 / p>

对于仅希望文件供参考的其他人,您可能希望将其重命名为 db / schema.rb.backup ,因此不会造成混淆。为此:

  Rake :: Task [ db:migrate]。如果ActiveRecord :: enhance做
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__))
结束
结束

(注意:在 Rakefile 的路径中使用 ../ 是因为 __ FILE __ 计算得出的路径以 / Rakefile 结尾。)


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.

解决方案

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.)

这篇关于使用:sql模式格式时如何使rake db:migrate生成schema.rb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆