Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update Dec 5, 2011</strong></p> <p>Thanks to Brad Robertson and his team, there's the <a href="https://github.com/influitive/apartment" rel="nofollow noreferrer">Apartment gem</a>. It's very useful and does a lot of the heavy lifting.</p> <p>However, if you'll be tinkering with schemas, I strongly suggest knowing how it actually works. Familiarize yourself with <a href="http://blog.jerodsanto.net/2011/07/building-multi-tenant-rails-apps-with-postgresql-schemas/" rel="nofollow noreferrer">Jerod Santo's walkthrough</a> , so you'll know what the Apartment gem is more or less doing.</p> <p><strong>Update Aug 20, 2011 11:23 GMT+8</strong></p> <p>Someone created a <a href="http://blog.jerodsanto.net/2011/07/building-multi-tenant-rails-apps-with-postgresql-schemas/" rel="nofollow noreferrer">blog post</a> and walks though this whole process pretty well.</p> <p><strong>Update May 11, 2010 11:26 GMT+8</strong></p> <p>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.</p> <pre><code> 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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload