This post originated from an RSS feed registered with Ruby Buzz
by Eric Stewart.
Original Post: Database Management in Rails
Feed Title: Ponderings On Ruby
Feed URL: http://blog.eric-stewart.com/category/programming-ruby.rss
Feed Description: This is the Ruby related section of Eric Stewart's weblog. These entries will be the commentary of a long time Java/C++ programmer that started exploring Ruby in 2003.
My current Rails project is a port of a previously deployed system. That means it already has lot’s of great data that the new version will want. But the client had no real attachment to the original DB schema and in fact prefers that it be migrated to a different DBMS for deployment on a different type of system.
After weighing a few different options, we found a great way to do this. SQL Server (the original DBMS) has a handy export facility to spit out data in CSV format. I found a great little Ruby CSV Library that will do all the reading we need (and would do lot’s of writing too if we needed it). Voila! A script to read the CSV. Now to write those records to the new DB.
ActiveRecord to the rescue! Fortunately ActiveRecord, a base component of Rails but quite useful on it’s own, is well suited to the task. Thanks to a little tip from the Rails Wiki, it’s extremely easy to add writing the CSV data we read into our new rails project database. And, as it turns out, very easy to reprocess it all the time as we find new uses for the data that was exported.
I’m sure there’s a fancier way to do all this, but what we found works quite well. Now migrating a table takes about 5 minutes (if we include designing the schema in the new DB and generating the model classes that is)!