45

我已经想通的东西

我正在学习如何在 Rails 中创建一个多租户应用程序,该应用程序根据用于查看应用程序的域或子域来提供来自不同模式的数据。

我已经回答了一些问题:

  1. 如何让 subdomain-fu 也与域一起使用?有人问了同样的问题,导致您访问此博客
  2. 什么数据库,它的结构如何?这是Guy Naor 的精彩演讲,以及关于 PostgreSQL 和 schemas的好问题。
  3. 我已经知道我的模式都将具有相同的结构。他们持有的数据会有所不同。那么,如何为所有模式运行迁移?这是一个答案

这三点涵盖了我需要知道的很多一般知识。但是,在接下来的步骤中,我似乎有很多实现方式。我希望有更好,更简单的方法。

最后,我的问题

当新用户注册时,我可以轻松创建架构。但是,加载其他模式已经拥有的结构的最佳和最简单的方法是什么?这里有一些问题/场景可能会给你一个更好的主意。

  1. 我是否应该将它传递给一个shell 脚本,将公共模式转储到一个临时模式中,然后将其导入我的主数据库(很像 Guy Naor 在他的视频中所说的)?这是我从 freenode 上有用的 #postgres 获得的快速摘要/脚本。虽然这可能会奏效,但我将不得不在 Rails 之外做很多事情,这让我有点不舒服......这也让我想到了下一个问题。
  2. 有没有办法直接从 Ruby on Rails 做到这一点?就像创建一个 PostgreSQL 模式一样,然后只需将 Rails 数据库模式(schema.rb - 我知道,这很混乱)加载到该 PostgreSQL 模式中。
  3. 是否有已经拥有这些东西的 gem/插件?像“create_pg_schema_and_load_rails_schema(the_new_schema_name)”这样的方法。如果没有,我可能会努力制作一个,但我怀疑它对所有移动部件的测试效果如何(特别是如果我最终使用 shell 脚本来创建和管理新的 PostgreSQL 模式)。

谢谢,我希望不会太长!

4

3 回答 3

12

2011 年 12 月 5 日更新

感谢 Brad Robertson 和他的团队,这就是Apartment gem。它非常有用,可以完成很多繁重的工作。

但是,如果您要修改模式,我强烈建议您了解它的实际工作原理。熟悉Jerod Santo 的演练 ,这样您就会知道 Apartment gem 或多或少在做什么。

2011 年 8 月 20 日 11:23 GMT+8 更新

有人创建了一篇博文,并很好地完成了整个过程。

更新 2010 年 5 月 11 日 11:26 GMT+8

从昨晚开始,我已经能够找到一种可以创建新模式并将 schema.rb 加载到其中的方法。不确定我所做的是否正确(到目前为止似乎工作正常)但至少更近了一步。如果有更好的方法请告诉我。


  module SchemaUtils
   def self.add_schema_to_path(schema)
    conn = ActiveRecord::Base.connection
    conn.execute "SET search_path TO #{schema}, #{conn.schema_search_path}"
   end

   def self.reset_search_path
    conn = ActiveRecord::Base.connection
    conn.execute "SET search_path TO #{conn.schema_search_path}"
   end

   def self.create_and_migrate_schema(schema_name)
    conn = ActiveRecord::Base.connection

    schemas = conn.select_values("select * from pg_namespace where nspname != 'information_schema' AND nspname NOT LIKE 'pg%'")

    if schemas.include?(schema_name)
     tables = conn.tables
     Rails.logger.info "#{schema_name} exists already with these tables #{tables.inspect}"
    else
     Rails.logger.info "About to create #{schema_name}"
     conn.execute "create schema #{schema_name}"
    end

    # Save the old search path so we can set it back at the end of this method
    old_search_path = conn.schema_search_path

    # Tried to set the search path like in the methods above (from Guy Naor)
    # [METHOD 1]: conn.execute "SET search_path TO #{schema_name}"
    # But the connection itself seems to remember the old search path.
    # When Rails executes a schema it first asks if the table it will load in already exists and if :force => true. 
    # If both true, it will drop the table and then load it. 
    # The problem is that in the METHOD 1 way of setting things, ActiveRecord::Base.connection.schema_search_path still returns $user,public.
    # That means that when Rails tries to load the schema, and asks if the tables exist, it searches for these tables in the public schema.
    # See line 655 in Rails 2.3.5 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
    # That's why I kept running into this error of the table existing when it didn't (in the newly created schema).
    # If used this way [METHOD 2], it works. ActiveRecord::Base.connection.schema_search_path returns the string we pass it.
    conn.schema_search_path = schema_name

    # Directly from databases.rake. 
    # In Rails 2.3.5 databases.rake can be found in railties/lib/tasks/databases.rake
    file = "#{Rails.root}/db/schema.rb"
    if File.exists?(file)
     Rails.logger.info "About to load the schema #{file}"
     load(file)
    else
     abort %{#{file} doesn't exist yet. It's possible that you just ran a migration!}
    end

    Rails.logger.info "About to set search path back to #{old_search_path}."
    conn.schema_search_path = old_search_path
   end
  end
于 2010-10-29T13:19:32.823 回答
3

将第 38 行更改为:

conn.schema_search_path = "#{schema_name}, #{old_search_path}"

我认为 postgres 在加载 schema.rb 时会尝试查找现有表名,并且由于您已将 search_path 设置为仅包含新模式,因此它会失败。当然,这是假设您的数据库中仍然有公共模式。

希望有帮助。

于 2011-03-18T08:42:06.013 回答
0

是否有已经拥有这些东西的 gem/插件?

pg_power提供了在迁移中创建/删除 PostgreSQL 模式的功能,如下所示:

def change
  # Create schema
  create_schema 'demography'

  # Create new table in specific schema
  create_table "countries", :schema => "demography" do |t|
    # columns goes here
  end

  # Drop schema
  drop_schema 'politics'
end

它还负责将模式正确转储到 schema.rb 文件中。

于 2013-08-14T09:32:09.370 回答