在Rails中使用Postgres多模式数据库

时间:2013-08-12 12:16:07

标签: ruby-on-rails-4 multi-tenant rails-migrations rails-postgresql

我正在this article之后开发一个多租户应用。问题是我第一次运行所有迁移。在schema.rb文件中只有公共模式的表,但是其他模式会发生什么?如何创建与public不同结构的其他模式我不想使用宝石。

参见下面的示例

要为公共架构创建的表

class CreatePerspectives < ActiveRecord::Migration
  include MultiSchema
  def up
      with_in_schemas :only => :public do
         # Create table perspectives
      end
  end


  def down
    with_in_schemas :only => :public do
      drop_table :prespectives
    end
  end
end

要为私有架构创建的表

class CreateObjectives < ActiveRecord::Migration

  include MultiSchema

  def change
    with_in_schemas :except => :public do
        # Create objectives table
    end
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20130810172443) do

  create_table "perspectives", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end

1 个答案:

答案 0 :(得分:1)

你知道Apartment gem吗? https://github.com/influitive/apartment

我今年在一个项目中使用过,一个支持postgresql的多模式。一切都记录在案,易于使用。您可以选择中间件模式。例如,当您访问域customer.applicationdomain.com时,应用程序可以为您选择正确的架构。顺便说一句,你也可以使用sidekiq的后台作业。