Hi,
I’m trying to connect to a Mysql database using MysqlAdapter, so I can
migrate some data from a system that is being replaced by a Rails
application. This requires having an open connection to two different
databases, one of which I will query directly, and the other being
used for the new application’s models.
I started with ActiveRecord::Base.establish_connection to connect to
the previous apps database, but when I do that it changes the models
in my new application to attempt to use that database as well, making
it impossible to save the newly migrated data.
Can somebody give an example of how to create a standalone connection,
which isn’t being tracked by ActiveRecord? I’ve tried
MysqlAdapter.new, but I have no idea what the parameters are meant to
be (despite several hours of running in circles through ActiveRecord’s
internals), so I havn’t been able to open a connection yet.
Thanks,
Jon
Just in case anyone comes across this, I ended up solving it by
establishing a new connection for each ActiveRecord model as it’s
loaded - it’s a bit of a hack, but it’s got my code running, so I’m
happy for now.
class ActiveRecord::Base
def self.inherited(subclass)
super(subclass)
subclass.establish_connection(RAILS_ENV)
end
end
I’m not sure that I need the call to super there, but I’m leaving it
in just for safety.
Jon
What I did to migrate data from MS Access to SQL Server. I just created
a raw connection to Access and used DBI/DBD calls to pull back data.
There wasn’t much point in even trying to use ActiveRecord because the
Access DB didn’t conform to any model objects.
To put data into SQL Server, I used my ActiveRecord models. So, pulling
the data from Access, I would use it to create new models that I would
save. If you’re thinking “Gee, that’s inefficient,” wrap the creation
of model objects in transactions. Also, unless you’re migrating data on
a regular basis, you should only need to run this migration in the
process of developing your application (I did the data migration code
inside a Rails migration).
Another advantage of using the combination of raw DBI/DBD calls from the
source database and Models for the destination database is the case
where 1 row in the source table maps to multiple objects in the
destination database.
paul