我正在学习如何在Rails中创建一个多租户应用程序,该应用程序基于用于查看应用程序的域或子域来提供来自不同架构的数据.
I'm learning how to create a multi-tenant application in Rails that serves data from different schemas based on what domain or subdomain is used to view the application.
我已经回答了一些问题:
I already have a few concerns answered:
这三点涵盖了我需要了解的许多常规知识.但是,在接下来的步骤中,我似乎有很多实现事情的方法.我希望有一种更好,更轻松的方法.
Those three points cover a lot of the general stuff I need to know. However, in the next steps I seem to have many ways of implementing things. I'm hoping that there's a better, easier way.
新用户注册后,我可以轻松创建架构.但是,什么是加载其余模式已经拥有的结构的最佳和最简便的方法?以下是一些可能使您有更好主意的问题/方案.
When a new user signs up, I can easily create the schema. However, what would be the best and easiest way to load the structure that the rest of the schemas already have? Here are some questions/scenarios that might give you a better idea.
谢谢,我希望那不会太久!
Thanks, and I hope that wasn't too long!
推荐答案2011年12月5日更新
多亏了布拉德·罗伯逊(Brad Robertson)和他的团队,这里有公寓宝石.这非常有用,并且可以完成很多繁重的工作.
Thanks to Brad Robertson and his team, there's the Apartment gem. It's very useful and does a lot of the heavy lifting.
但是,如果您要修改模式,我强烈建议您了解它的实际工作方式.熟悉 Jerod Santo的演练,这样您就会知道Apartment宝石或多或少在做什么.
However, if you'll be tinkering with schemas, I strongly suggest knowing how it actually works. Familiarize yourself with Jerod Santo's walkthrough , so you'll know what the Apartment gem is more or less doing.
更新2011年8月20日11:23 GMT + 8
有人创建了博客发布,并且在整个过程中都能很好地行走.
Someone created a blog post and walks though this whole process pretty well.
更新2010年5月11日格林尼治标准时间+8
自昨晚以来,我已经能够使用一种方法来创建一个新的架构并将schema.rb加载到其中.不知道我在做什么是否正确(到目前为止,似乎工作正常),但至少离它还差一步.如果有更好的方法,请告诉我.
Since last night I've been able to get a method to work that creates a new schema and loads schema.rb into it. Not sure if what I'm doing is correct (seems to work fine, so far) but it's a step closer at least. If there's a better way please let me know.
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更多推荐
使用PostgreSQL的架构和Rails创建多租户应用程序
发布评论