Friday, April 6, 2007

Use ActiveRecord To Migrate Legacy DBs

I'm pretty sure you can find this in Rails Recipes, but for what it's worth, here's the short version. Say for the sake of argument that you have a legacy app and you need to convert the tables into a format Rails will work with easily and nicely. You can in fact wire ActiveRecord up in such a way that it'll work with any set of tables and indices, but again, just for the sake of argument.

What you do is write a small Ruby app which uses ActiveRecord, and set up two database connections within that app. One's a standard connection, the other one is set up to use the legacy database (and I know for a fact that this part is in Rails Recipes). Then all you do is create a bunch of objects from the crufty legacy DB, and a bunch of shiny new Rails-y models, and you populate the models with data from the crufty apps. Then all you do is you turn the switch, and test all the new models in the new DB to ensure they match your expectations (which are of course derived from the legacy DB).

It's easy, it's clean, it's quick, and the difference between the two relational models is essentially documented automatically with this code. Very highly recommended technique.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.