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

标签 ruby-on-rails postgresql

我刚刚通过常规迁移创建了一个新的自定义 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/

相关文章:

sql - 如何列出 PostgreSQL 上的事件连接?

ruby-on-rails - 无用户认证 (Facebook)

ruby-on-rails - Rails 什么是抓取网站的最佳 gem?

ruby-on-rails - 查找别名方法的源位置

hibernate - Tomcat 7、Hibernate 3.6、PostgreSQL 9.2 | Maven java.lang.NoClassDefFoundError

postgresql - FireDAC:如何避免 "Cannot describe type"错误? (在 postgres 几何列上)

ruby-on-rails - 使用 Rails Composer 工具创建 rails 应用程序时显示 "/Gemfile not found"错误

ruby-on-rails - 如何防止回形针gem在上传新文件时删除旧文件?

postgresql - Postgis 数据库结构 : Identical tables for multiple years?

java - 在 Java EE 6 应用程序(数据库/文件系统)中存储大量文件的最佳方式