gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:20:39 25 4
gpt4 key购买 nike

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

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

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

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

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

最佳答案

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

我们使用 structure.sql 代替,因为这正确地设置了我们的 View ,我的感觉是同样适用于自定义函数。使用 structure.sql 而不是 schema.rb:

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(用于自定义函数)。要为测试环境设置此组合:

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

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

关于ruby-on-rails - Rails schema.rb 不包含新的自定义 Postgres 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27507094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com