【问题标题】:Rails schema.rb does not include new custom Postgres functionRails schema.rb 不包含新的自定义 Postgres 函数
【发布时间】:2014-12-16 14:38:55
【问题描述】:

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

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

迁移后,schema.rb中没有这个新功能。

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

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

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

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

    我们使用structure.sql 代替,因为这可以正确设置我们的视图,而且我的感觉是同样适用于自定义函数。使用 structure.sql 代替 schema.rb:

    这是由 config.active_record.schema_format 设置在 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。
    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2016-02-01
    相关资源
    最近更新 更多