Rails 3, Postgresql Schemas, and schema.rb

Hi All,

Has anyone worked with Rails 3 and Postgresql schemas? I’m working on a
multi-tenant Rails3 (beta 4) application where each account will have
it’s
own schema in a postgresql database. What I’ve done so far (with help
from
Creating a multi-tenant application using PostgreSQL's schemas and Rails - Developer IT),
is to create an Account and then call a method that creates a new
postgres
schema and then load the rails schema.rb into the newly created schema.

The method I call after creation is the following:

def self.create_new_schema_and_load_rails_schema(schema)
conn = ActiveRecord::Base.connection
conn.execute(“CREATE SCHEMA #{schema}”)
conn.execute(“SET search_path TO #{schema}”)
file = “#{Rails.root}/db/schema.rb”
load(file)
conn.execute(“SET search_path TO #{conn.schema_search_path}”)
end

This fails at ‘load(file)’. When I attempt to do something similar in
the
console: *

Account.create(:name => ‘Draper’, :domain => ‘dondraper.com’)
conn.execute(“create schema dondraper”)
conn.execute(“SET search_path TO dondraper”)
file = “#{Rails.root}/db/schema.rb”
load(file)

I get the following error:*

– create_table(“accounts”, {:force=>true})
ActiveRecord::StatementInvalid: PGError: ERROR: table “accounts” does
not
exist
: DROP TABLE “accounts”
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:210:in
log' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/postgresql_adapter.rb:464:in execute’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/abstract/schema_statements.rb:192:in
drop_table' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/connection_adapters/abstract/schema_statements.rb:112:in create_table’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/migration.rb:356:in
send' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/migration.rb:356:in method_missing’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/migration.rb:332:in
say_with_time' from /usr/local/lib/ruby/1.8/benchmark.rb:293:in measure’
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/migration.rb:332:in
say_with_time' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/migration.rb:352:in method_missing’
from /home/admin/a2/db/schema.rb:14
from
/usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/schema.rb:49:in
instance_eval' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.0.beta4/lib/active_record/schema.rb:49:in define’
from /home/admin/a2/db/schema.rb:12
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:208:in
load' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:208:in load’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in
load_dependency' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:554:in new_constants_in’
from
/usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in
load_dependency' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:208:in load’

However*,
When I reset the search path to the public schema and attempt to load
the
file it works flawlessly.

Does anyone have any idea what might be the problem? Or perhaps, someone
has
some ideas or online resources about creating new postgresql schemas
from
within a rails application?

Any help will be much appreciated.

Thanks,
Mike

[email protected] wrote:

Hi All,

Has anyone worked with Rails 3 and Postgresql schemas?
I don’t think Rails 2 supports them. Has that changed in 3?
I’m working on a
multi-tenant Rails3 (beta 4) application where each account will have
it’s
own schema in a postgresql database.

I cringe every time I see this. Just put all the data together – it
will be much easier to manipulate.

This is fixed in Rails 3.0.8+ and soon into 3.1