Rails schema.rb不包含新的自定义Postgres函数 [英] Rails schema.rb does not include new custom Postgres function

查看:72
本文介绍了Rails schema.rb不包含新的自定义Postgres函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚通过常规迁移创建了一个新的自定义Postgres函数。

I have just created a new custom Postgres function via usual migration.

class CreateBestBowlingFigureFunction < ActiveRecord::Migration
  def change
    execute "CREATE OR REPLACE FUNCTION ......"
  end
end

迁移后,此新功能在schema.rb中不可用。

After the migration, this new function is not available in schema.rb.

根据官方doc,我在运行测试之前使用命令 db:schema:load 创建架构。

As per the official doc, I use the command db:schema:load to create the schema before running my tests.

因此,在运行测试之前创建自定义函数的最佳实践是什么?

So, what is the best practice to create custom functions before running the tests?

推荐答案

schema.rb 无法处理(请参见Rails 3.2.x指南的6.2节和Rails 4指南的7.2节)视图或自定义功能。我们的应用程序中有一个视图,该模式不适用于该视图。

schema.rb does not handle (see section 6.2 of the Rails 3.2.x guides and section 7.2 of the Rails 4 guides) views or custom functions. We have a view in our application and the schema does not work for it.

我们使用 structure.sql 代替,因为这样可以正确地设置我们的视图,我的理解是一样的这里是自定义功能。要使用structure.sql代替schema.rb:

We use structure.sql instead as this properly sets up our view and, my sense is the same would apply here for custom functions. To use structure.sql instead of schema.rb:


这是通过config.active_record.schema_format设置在config / application.rb中设置的,可能是:sql或:ruby。

This is set in config/application.rb by the config.active_record.schema_format setting, which may be either :sql or :ruby.

您也可以使用 schema.rb的组合(用于常规表和索引)和 structure.sql (用于自定义函数)。要在测试环境中设置此组合,请执行以下操作:

You can also use a combination of schema.rb (for regular tables & index) and structure.sql (for custom functions). To setup this combo for test environments:

bundle exec rake db:schema:load
bundle exec rake db:structure:load

在此设置中,请注意,在schema.rb时必须手动维护structure.sql将由Rails为您维护。

In this setup, note that the structure.sql has to be maintained manually while schema.rb will be maintained by Rails for you.

这篇关于Rails schema.rb不包含新的自定义Postgres函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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