Two databases one app?

Ultimately what I need to make happen is have two domains point to the
same application, however depending on which domain is accessing it to
use a different database.

I suggested installing the application on two servers, but that option
is for some reason not good. I tried to install the application twice
into one account and just symlink them depending on the domain but
that did not work either, here’s why:

One domain has to point to ~/public_html, I have no option in that the
way our host is set up. The second domain has to point to a
subdirectory of ~/public_html. Adding a symlink to the first
application’s public folder didn’t work because Apache is unable to
follow through two symlinks.

Adding any extra characters to the URL is out of the question too, so
I can’t use domain.com/i/app

Ultimately I just need the question answered that is at the top, any
takers?

perhaps one way is to build your models like so:

class MyModel < ActiveRecord::Base
establish_connection(
:adapter => “whatever”,
:host => “database_#{request.host.gsub(/[^w], ‘_’)}”,
:username => ‘user’,
:password => ‘pass’
)
end

beware i have not tested this, so i’m not sure if this would run with
every model instantiation

and if the above works, maybe a DRY optimization would be to stuff it
into ActiveRecord::Base once instead of every model

On Apr 17, 2007, at 11:03 PM, jawgaping wrote:

way our host is set up. The second domain has to point to a
subdirectory of ~/public_html. Adding a symlink to the first
application’s public folder didn’t work because Apache is unable to
follow through two symlinks.

Adding any extra characters to the URL is out of the question too, so
I can’t use domain.com/i/app

Ultimately I just need the question answered that is at the top, any
takers?

What about having two separate checkouts in the same machine, each
with its own database.yml. Configure a virtual host for each one in
Apache. Each virtual host then has its own document root and mongrel
cluster (each instance of the application runs behind its own cluster).

– fxn

Although a low level solution would be more preferable, this works
wonderfully! Thank you jemm you probably saved my job.

On Apr 18, 2007, at 2:51 AM, jawgaping wrote:

:adapter  => "whatever",
:host     => "database_#{request.host.gsub(/[^w], '_')}",
:username => 'user',
:password => 'pass'

)
end

Careful, with that approach MyModel will always point to the database
of the domain that triggered its class loading. In development mode
that works because the class is reloaded at each request. However, in
production mode that means MyModel won’t point to the table of the
database of the other domain ever.

If that’s OK then you have your model layer really partitioned in two
databases known before hand, and in that case the regular
establish_connection + two keys in database.yml will suffice.

Otherwise you’d move that code to a high-priority filter in
ApplicationController:

def choose_database_for_host
ActiveRecord::Base.establish_connection …
end

because you need to select the connection per request. That has a
performance cost, but perhaps it’s fine with the load of that
applicaction. If that was not the case more intrusive solutions
involving some sort of connection pool would be needed.

– fxn

PS: The solution with two separate checkouts with respective mongrel
clusters didn’t fit?