How to Prepare Your Rails App for an Upgrade

How to Prepare Your Rails App for an Upgrade

This article is part of our Upgrade Rails series. To see more of them, click here opens a new window .

This article will cover the most important aspects that you need to know to prepare your Ruby on Rails opens a new window application for working on an upgrade.

  1. Test Coverage
  2. Staging and Production
  3. Patch Version
  4. Incompatabilities
  5. Dual Boot

1. Code Coverage

Not all applications are good candidates for a Rails upgrade project. We strongly advise against upgrading big applications which are running in production with minimal test coverage.

Make sure that you have at least 80% test coverage before starting the upgrade project. If you don’t have a solid test suite (or a dedicated QA team), you will likely find many problems which will force you to roll back the upgrades as soon as they hit production.

You can easily calculate your coverage by using simplecov opens a new window . Here is a tutorial opens a new window about how to use it.

2. Staging and Production

We advise all of our clients to follow a Git flow opens a new window workflow and to actively manage at least two environments: staging and production.

Every change should be code reviewed using pull requests and it should run the entire test suite. Once all checks have passed, changes should be deployed to staging. After your QA team has tested your critical flow in staging, you can deploy the changes to production.

3. Patch Version

Before working on an upgrade you should make sure that you are running the application with the latest patch version. That way, you make sure that the latest security patches have been installed. On top of that, with more recent versions, you will get a series of deprecation warnings that will be useful in finding out what needs to be done.

4. Incompatibilities

Have you ever gotten tangled in dependencies? You need to upgrade dependency A, but that’s incompatible with Rails 5.0 because dependency B (which is a dependency of A) is not compatible with that version of ActiveRecord).

Before you start going down the rabbit hole, make sure to check your Gemfile.lock for incompatibilities. For that, you can use this website: https://railsbump.org/

That will give you an idea of how many dependencies are not compatible with Rails 4, 5, or 6.

5. Dual Boot

To help you switch between your current Rails version and the new one, you can create a dual boot mechanism. The fastest way is to install the handy gem next_rails opens a new window . You can initialize it by doing

$ gem install next_rails
$ next --init

You can then set up two Rails versions in your Gemfile like this:

if next?
  gem 'rails', '~> 6.0.0'
else
  gem 'rails', '~> 5.2.3'
end

Sometimes dependencies are backwards compatible with the current version of Rails. Within the libraries, you will find code that looks like this:

if Rails::VERSION >= 5.1
  # does X
else
  # does Y
end

If that is the case, then you might be able to just upgrade the dependency using bundle update.

Next Steps

Now that you have laid the groundwork for you upgrade, it is time to start the work itself. For details on what you specific version upgrade will require, check out the corresponding guide:

Get the book