Migrating across multiple databases with ActiveRecord

137 views
Skip to first unread message

Ivan Vanderbyl

unread,
Oct 24, 2008, 1:53:46 AM10/24/08
to Ruby or Rails Oceania
Hi,

I have a scenario, we have 2 applications running on the same server,
each on their own domain, (but that's irrelevant), both applications
have a User model (and table, in separate databases), both use Restful
auth plugin etc.
I have set app B to load users from the database of app A with a
custom connection in the User model, all works smashingly! What I'm
wondering is, lets say I wanted all users to be in a separate
database, maybe on another server, which only stores users, is there
some; possibly undocumented way of telling ActiveRecord::Migration to
connect to another database when running that migration?

Also, I haven't tried this, but maybe someone has, or knows someone
else who has: Will my record associations still work? e.g. User
has_many Orders? or User HABTM Roles if the roles table is on another
database?

Ivan

Mikel Lindsaar

unread,
Oct 26, 2008, 6:08:20 PM10/26/08
to rails-...@googlegroups.com

I have a similar situation, but not with a users model.

The way I handled this is using PostgreSQL and multiple schemas, then
one application owns the table, and the other just has a view of it.
This way you keep your DB implementation of this solution transparent
to Rails (Rails will treat a view called 'users' just like a table
called 'users'.

So your DB would look like this:

ProductionDatabase
Schemas
App1
Views
Users (select * from app2.users)
App2
Tables
Users

Now to make that work, you would need on insert and on delete and on
update rules on your users view obviously.

The other (and probably) more rails way to do it would be to set up a
third authentication application and expose the users table with
ActiveResource. That way you could then plug as many apps into it as
you want.

The third way I would look into is setting up an LDAP server and
authenticating from both apps against that.

Anyway, hope that helps.

--
http://lindsaar.net/
Rails, RSpec and Life blog....

Ivan Vanderbyl

unread,
Oct 28, 2008, 9:06:48 PM10/28/08
to Ruby or Rails Oceania
Thanks Mikel,

The view idea worked great with MySql. I didn't even know about views
until you mentioned it.

Ivan

On Oct 27, 9:08 am, "Mikel Lindsaar" <raasd...@gmail.com> wrote:
> --http://lindsaar.net/

Mikel Lindsaar

unread,
Oct 29, 2008, 1:26:51 AM10/29/08
to rails-...@googlegroups.com
On Wed, Oct 29, 2008 at 12:06 PM, Ivan Vanderbyl
<ivanva...@gmail.com> wrote:
> Thanks Mikel,
>
> The view idea worked great with MySql. I didn't even know about views
> until you mentioned it.

Cool :)

Reply all
Reply to author
Forward
0 new messages