Revisiting rails:update

Lately I’ve been revisiting some Rails apps which have been around for a while. These are Rails apps that started life somewhere in the 2.x era and are now on the latest (4.2.3 as of this writing), and they were upgraded in a series of steps over the past couple of years. So all that to say that these apps have been through a lot of changes.

When I’m doing Rails upgrades I tend to do the minimum necessary to get the app to the next version. Then I make some notes on things to revisit after the dust has settled. Sometimes those things happen, sometimes not. One thing I hadn’t done before is run rake rails:update on those apps. I had tried it a couple of times, but I would see a huge diff and shelve it for later.

But I think I was approaching it in the wrong way. Here’s what I’ve been doing lately:

bundle exec rake rails:update # and hit 'a' to accept all changes
git diff > ../rails_update.diff
git add . && git reset --hard # throw changes away
git checkout -b newify

Now in a terminal window I more ../rails_update.diff and just kind of page through it, looking for useful bits and fixing them as I go. Stuff like:

  • Changing Foo::Application to Rails.application in Rakefile and whatnot.
  • Removing unnecessary MIME type registrations like Mime::Type.register("text/plain", :txt).
  • Removing any explicit requiring of rubygems in config/boot.rb.
  • Simplifying the bundler required invocation in application.rb from Bundler.require(:default, Rails.env) if defined?(Bundler) down to Bundler.require(*Rails.groups).
  • Seeing if there are any redundant configuration item settings in application.rb. For example, I might be explicitly setting ActiveRecord::Base.include_root_in_json to false, but that’s the default value in 4.2.3.
  • Similarly, seeing if I’m adding any now-non-existent (due to code cleanups or whatever) directories to the autoload path.
  • Seeing if I’m adding any autoload paths that are unnecessary since modern Rails autoloads all directories under app/. I’ve found apps that were adding app/mailers to the autoload path, for example.

There are usually some other things I spot when poking through these files – old constant settings, old environment variable checks, etc. Usually a git blame will help me figure out if this is something that was added 5 years ago and never deleted or will at least let me know who I can talk to about it.

None of these changes are big wins, it’s just a few milliseconds here and there. But they all add up and help to make an old and crufty application more approachable. Plus, red commits always feel good!

Thanks to Jeremy Stuckey for reviewing this post.