Wednesday, March 23, 2011

Rails 2 to Rails 3 Migration

Recently I got engaged with assignment to help one of my client in migrating his Rails 2 app to Rails 3. The engagement was interesting and quite demanding. I am sharing this experience which may of interest to some of you guys.
Steps taken

1) First made sure that I have rvm installed so that i can switch between ruby versions easily.

$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Then put the following line in .bashrc. This line loads rvm on system.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
$ rvm install 1.9.2 $ rvm use 1.9.2
$ gem install rails --version=3.0.3


2) Switch to your existing system ruby verion(in my case it was ruby 1.8.7) and start migrating your existing rails 2 project. I have taken help of rails_upgrade plugin for the same

$ rvm system
$ cd myproject
$ script/plugin install git://github.com/rails/rails_upgrade.git


3) Once plugin installed then run rake tasks

# Check your app for required upgrades
rake rails:upgrade:check

# Backup your likely modified files that might be overwritten by the generator
rake rails:upgrade:backup

# Generate a new route file
rake rails:upgrade:routes

# Generate a Gemfile from your config.gem directives
rake rails:upgrade:gems

# Generate code for a new config/application.rb from your environment.rb
rake rails:upgrade:configuration

4) Now its time to go back to your backed up files and pick up your application specific code and put it in upgraded files
5) Now switch to rails 3 using rvm and start your app


$ rvm use 1.9.2
$ rails server

6) If you do not get any error then that will be surprise as you will definitely get tons of errors and its time to attack one by one

frequest type of error you will be sseing are
a) gem not installed -> please make sure that you have added entry in .Gemfile and used bundle install command
b) gem version mismatch -> find out verion that supports rails 3(ruby 1.9.2)
c) some modifications in application.rb
d) html corruption -> please use html safe functions .html_safe

7) now you need to check each page one by one and sort out any issues.

Overall its a great experience and offcourse I could do it with help of ample onlie help available on opensource communities.
I will like you not to miss following links
rails upgrate plugin
rails cast on rails 3 migration
rails cast on rails 3 routing

No comments: