Forum: Ruby on Rails comparing two databases with Active Record.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Don M. (Guest)
on 2007-03-13 18:55
(Received via mailing list)
I have two databases in SQLite with the same schema. The other
database was populated by an external
customer and then returned. I want to use ActiveRecord to do the
comparison.


I am not sure of the best way to access the tables in each database
with ActiveRecord models. I could
change the connection dynamically whenever I want to switch between
the databases, but this would seem to be clumsy and error prone.

Another approach would be to create a 2nd set of classes at runtime
which cloned the original ActiveRecord
model classes and inherits from a different base class with a
different connection.

If anyone has done anything like this or has any suggestions on the
best course of action, I would appreciate it.

Thanks in advance,
Don McClean
Luke I. (Guest)
on 2007-03-13 19:08
(Received via mailing list)
Honestly, were it me, I would just copy the model files, rename them to
something like ModelOtherDB, and use establish_connection and
set_table_name.

Might be somewhat clunky, and might not be the route to go if this is
something you're going to need to abstract to multiple applications, but
if
it's just the one, then that should probably work fine.
Don M. (Guest)
on 2007-03-13 19:26
(Received via mailing list)
Luke,
  Thanks for the quick response. It is something that I will probably
need to
abstract for future application. I believe your suggestion is good, I
would
probably something similar at runtime if possible to create the
additional classes and
connection. Thanks for your comment on 'set_table_name', I forgot
about
that completely.

Regards,
Don
Phlip (Guest)
on 2007-03-13 19:32
(Received via mailing list)
Don.Mc wrote:

> I have two databases in SQLite with the same schema. The other
> database was populated by an external
> customer and then returned. I want to use ActiveRecord to do the
> comparison.

Read /Rails Recipes/; it shows how to open two databases at the same
time.

(But seriously, if this is a one-shot with sqlite3, I would just .dump
the databases and diff them...

--
  Phlip
  http://c2.com/cgi/wiki?ZeekLand  <-- NOT a blog!!
Don M. (Guest)
on 2007-03-13 19:46
(Received via mailing list)
Phlip,
  Thanks, I looked at the Recipes book previously, it is only
applicable when you
have different schemas in the two databases.

And it's not a one-shot deal, unfortunately, I need an long term
automated solution.

Thanks,
Don
Luke I. (Guest)
on 2007-03-13 20:05
(Received via mailing list)
On 3/13/07, Don.Mc <removed_email_address@domain.invalid> wrote:
> about
> that completely.
>
> Regards,
> Don


So, just thought I'd drop you another note showing some neat stuff you
can
do that will probably help.

class User < ActiveRecord::Base
end

(somewhere else in your code)
UserOtherDB = User.dup
UserOtherDB.class_eval <<EOS
  establish_connection :blah => blah, :blah => blah
  set_table_name 'users'
EOS
Don M. (Guest)
on 2007-03-13 20:07
(Received via mailing list)
That helps!

Thanks Luke
This topic is locked and can not be replied to.