【问题标题】:Use Postgres Multiple Schema Database in Rails在 Rails 中使用 Postgres 多模式数据库
【发布时间】:2013-08-13 18:01:03
【问题描述】:

我正在开发一个关注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

【问题讨论】:

  • 我不明白你为什么需要多个模式?您不能只使用一个并根据用户的角色使某些信息无法访问吗?
  • 多年来我一直使用并喜欢 Postgres,并且非常熟悉模式及其用法。我也是一名 Rails 开发人员。也就是说,除非必须,否则我永远不会同时使用这两者。你不必。只需设计出你的数据布局(通常我会称之为“模式”,但我不想混淆这个问题)并添加另一个名为“公司”的表或任何拥有记录的表。然后在各种表中赋予它所有权。我有几个这样的应用程序,并不难。

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


【解决方案1】:

你知道公寓宝石吗? https://github.com/influitive/apartment

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

【讨论】:

  • 该问题明确表示不能选择宝石。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
相关资源
最近更新 更多