Topic: Apartment Gem

I've written a large multi-tenant medical app in PHP.  After 5 years it is virtually impossible to maintain, so I am re-writing it in rails.  My learning curve with rails has been slow to say the least, as I have worked only with procedural languages.  I cannot go through another tutorial, I have to just dive in and start doing it before anything is going to stick.  Anyway, I was very happy to find the Apartment gem and start testing.  I've hit a road block, and I know it's me.  The docs make it look way too simple and after 2 days of google searches I'm not getting anywhere. 

Here's the gem -> https://github.com/bradrobertson/apartment

I put the following in /config/initializers/apartment.rb

Apartment.configure do |config|
  config.database_names = lambda{ Customer.select(:database_name).map(&:database_name) }
end

When I run rake apartment:migrate I get:
rake aborted!
uninitialized constant Customer

I tried changing Customer above to my app name 'Testapp' and I get:
rake aborted!
private method 'select' called for Testapp:Module

Any help or direction I'd really appreciate it.  I'm sure in a few months I realize how ridiculously simple my question is, but for now I'm stuck.

Re: Apartment Gem

That assumes you have an existing Customer model in your database,  do you?

do you have

app/models/customer.rb:

class Customer < ActiveRecord::Base
end

And is there a table called 'customers' in the database that you have configured in database.yml for your development environment?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Apartment Gem

Hi Brad, thank you for the reply.  When I read the documentation on this I have two options for migrating multiple databases:

In an initializer I can put:

Apartment.configure do |config|
  # set your options (described below) here
end

and the two options are:
# Dynamically get database names to migrate
config.database_names = lambda{ Customer.select(:database_name).map(&:database_name) }

# Use a static list of database names for migrate
config.database_names = ['db1', 'db2']

If i use option 2, it works.  If I use option 1 to dynamically get the database names it fails.  It doesn't make sense to me to need to specify individual models when the idea is that option 1 is supposed to dynamically retrieve a list of databases and iterate through each database to perform migrations.

But I did give it a shot.  I have a model called User, so I put that in place of Customer and I get the following error:
rake aborted!
PGError: ERROR:  column "database_name" does not exist
LINE 1: SELECT database_name FROM "users"
               ^
: SELECT database_name FROM "users"

Any other ideas on what I should try?

Re: Apartment Gem

I think maybe you are taking the example to literally, all the docs say is:

This object should yield an array of string representing each database name. Example:

I think at that point you are database dependent,  dumping a list of databases from your database engine is not standardized,  so you just have to play with your database.  find how to dump out a list of databases,  find a way to call that from your database connecter,  and somehow manage to turn the results into an array of strings.  There is no telling HOW that part will end up looking in code,  so the author made up an example.

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Apartment Gem

Ok, thanks for the input.  I'll keep working at it and post up when I get it figured out.

Re: Apartment Gem

I do have this working with the following workaround.  I don't know if it's the best, but it is working in my tests so far:

  sql = "SELECT nspname FROM pg_namespace WHERE nspname !~ '^pg_.*' and nspname !~ 'information_schema'"
  config.database_names = ActiveRecord::Base.connection.query(sql).flatten

Re: Apartment Gem

That's fine, it works, that's all that matters,  just remember that config if you ever change databases!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Apartment Gem

Investing in apartments is a very good idea and one that can help you in many other ways.
grand valley state university apartments