comparing two databases with Active Record.

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

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.

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

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,   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

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

That helps!

Thanks Luke