从Rails 4在Amazon RDS中启用hstore扩展 [英] Enabling the hstore extension in Amazon RDS from Rails 4

查看:95
本文介绍了从Rails 4在Amazon RDS中启用hstore扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ActiveRecord与PostgreSQL 9.3数据库进行交互的Rails 4应用程序。该应用程序利用PostgreSQL中的 hstore 扩展名将键值对存储在单个数据库字段中。因此,Rails会自动检测到我对该迁移所做的操作,并在您的schema.rb文件中创建以下行

I have a Rails 4 application that uses ActiveRecord to interact with a PostgreSQL 9.3 database. The application makes use of the hstore extension in PostgreSQL for storing key-value pairs in a single database field. Accordingly, Rails automatically detects what I did with that migration and creates the following line in your schema.rb file

enable_extension "hstore"

在开发和测试环境中,这很好,因为我正在部署到独立的PostgreSQL实例。但是在生产中,我将部署到Amazon RDS中的PostgreSQL实例,在该实例中,扩展必须由 rds_superuser 组的成员安装。我不想让我的应用程序用户扮演该角色。

In the development and test environments, that works great, as I am deploying to stand-alone PostgreSQL instances. However in production, I am deploying to a PostgreSQL instance in Amazon RDS, where the extension must be installed by a member of the rds_superuser group. I'd rather not put my application user in that role.

有没有办法使代码的执行取决于环境?例如,在测试中运行开发中的代码,而不在测试中运行该代码?

我尝试创建有条件的迁移,但它没有生成架构时,请不要删除上面的行。示例:

I've tried creating a migration that's conditional, but it doesn't eliminate the line above when the schema is generated. Example:

class ModifyHstoreSetup < ActiveRecord::Migration
  def self.up
    if %w(staging, production).include?(ENV["RAILS_ENV"] || "development")
      enable_extension "plpgsql"
      enable_extension "hstore"
    end
  end
  def self.down
    if %w(staging, production).include?(ENV["RAILS_ENV"] || "development")
      disable_extension "plpgsql"
      disable_extension "hstore"
    end
  end
end


推荐答案

只需使您的用户成为rds_superuser组的成员即可。这样您的迁移就会成功。

Just make your user member of rds_superuser group. Then your migration will succeed.

这篇关于从Rails 4在Amazon RDS中启用hstore扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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