New Bamboo Web Development

Bamboo blog. Our thoughts on web technology.

Upgrading to Rails 2.3 with Git

by Oliver Legg

So with the release of Rails 2.3 hot off the presses, it is time to start upgrading some of our rails applications. While the process has got much easier since the 0.x days, some files still need to be updated in the application itself.

To help with the upgrade path the rails command prompts you to overwrite, keep or diff with the latest files. However this isn't always what you want - quite often you want to merge changes instead of just overwriting them.

So I thought I would experiment with a new method, using the power of git to handle the merging of the old and new. First I created a branch to do the upgrade in, so that I can commit and make sure everything is working before I merge back into master.

1 git branch rails_upgrade

Secondly, I create a blank branch imaginatively named fresh_rails_2.3.2. Then we remove all of the files from this branch to give us a blank slate.

1 git symbolic-ref HEAD refs/heads/fresh_rails_2.3.2
2 git rm -r --cached .
3 rm -rf *
4 rm .*

The next step is to generate a blank rails application (assuming you have the latest gems installed) and commit it to our detached branch.

1 rails .
2 git commit -m 'Generate Rails v.2.3.2 skeleton.'

Now we have the latest and greatest files we can merge back into our rails_upgrade branch.

1 git co rails_upgrade
2 git merge fresh_rails_2.3.2

Unless you have barely touched your application since it was previously generated, there should now be a number of conflicts that you need to resolve. Fix each one in turn and add it to the staging area. For example:

1 mate config/environment.rb
2 # Fix the conflicts and save
3 git add config/environment.rb

Note: If you have previously ignored any files from you application (config/database.yml for example) you will need to remove them from the staging area otherwise they will be added back into your project.

1 git reset config/database.yml

With the merge complete you can run your tests to make sure the merge was a success. If everything passes you can now merge the upgrade back into the master branch.

1 rake spec
2 rake features
3 git commit
4 git co master
5 git merge rails_upgrade

GitX shows my history looking like this. Overall I think the experiment worked quite well and it was defiantly useful to be able to manually pick and choose which bits of the new files I wanted.