Ruby on rails Rails schema.rb不包含新的自定义Postgres函数

Ruby on rails Rails schema.rb不包含新的自定义Postgres函数,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我刚刚通过通常的迁移创建了一个新的自定义Postgres函数 class CreateBestBowlingFigureFunction < ActiveRecord::Migration def change execute "CREATE OR REPLACE FUNCTION ......" end end class CreateBestBowlingFigureFunction

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

class CreateBestBowlingFigureFunction < ActiveRecord::Migration
  def change
    execute "CREATE OR REPLACE FUNCTION ......"
  end
end
class CreateBestBowlingFigureFunction
迁移后,schema.rb中没有此新函数

根据官方文档,在运行测试之前,我使用命令
db:schema:load
创建模式

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

schema.rb
(请参阅Rails 3.2.x指南的第6.2节和Rails 4指南的第7.2节)视图或自定义函数。我们的应用程序中有一个视图,模式不适用于它

我们使用
structure.sql
,因为这可以正确地设置我们的视图,我的感觉与这里的自定义函数相同。要使用structure.sql而不是schema.rb,请执行以下操作:

这是通过config.active\u record.schema\u格式设置在config/application.rb中设置的,可以是:sql或:ruby

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

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

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

我将结合使用schema.rb和structure.sql。并使用
db:structure:load
创建自定义函数。很好!我以前没搞过组合动作。我很想知道结果如何(可能会让我的工作生活更轻松,哈哈)。太好了!谢谢很高兴看到,“structure.sql必须手动维护”不是真的,在Rails 4或5中不是这样。每次运行rake db:migrate时,都会转储db/structure.sql。