Steps to upgrade an old rails application to rails 5

Step 1: If you have BDD/TDD’s written then run it to see if there are deprecation warnings for future versions in logs. If not try to manually go through application as much as possible to track for possible deprecation warnings. If yes then fix them before upgrade.

Step 2: Find immediate next stable release version of ruby from — https://www.ruby-lang.org/en/downloads/releases/

2) Change .ruby-version, build.Dockerfile or Gemfile ruby version to 2.7.1 (Always upgrade to next immediate stable version and not directly to the latest version of ruby as it will lead to loads of errors). If you are using ruby 2.3 then upgrade it to 2.4 and then 2.5 until you reach the most stable version.

3) If you are using Docker then run docker-compose build and then docker-compose up else run bundle install.

5) Upgrade rails gem with changes as mentioned mentioned in the url.
a) http://railsdiff.org/4.0.0/4.0.13
b) http://railsdiff.org/4.0.13/4.1.16
c) http://railsdiff.org/4.1.16/4.2.11.3
d) http://railsdiff.org/4.2.11.3/5.0.0
e) http://railsdiff.org/5.0.0/5.0.7.2
f) http://railsdiff.org/5.0.7.2/5.1.7
g) http://railsdiff.org/5.1.7/5.2.4.4

Note: If you are upgrading from rails 4 then I recommend upgrading to rails 6 only after the application is well tested and stable.
Commit the code after each version upgrade so that you can easily revert to previous version which was successfully upgraded.

Follow the changes in step a-g and on each step do the following

a) Upgrade gems to the next immediate stable version
b) bundle update

c) Do step 6 to 8 below and then run the application and fix all deprecation warnings and possible errors then move on to step b mentioned above for next rails version.

Note: If rails version is very old then there will be lot of dependent gem errors when you upgrade from rails 4 to rails 5. Easiest way to fix this and get the application running to first set the application with default gems that will be created at the time of creating new rails 5 application and then keep adding necessary gems for your application. This way you can also remove gems that are no more required. For example:
when you upgrade from rails 4.2.11.3 to rails 5 remove all the gems that exist in your application and retain only the default gems that are created when a new rails application is created. bundle install is executed successfully then you can add required gems one by one. This will also help you to remove unwanted gems that you must have added and not used anymore.

6) Once all required gems are added and run rails s to see if there are any deprecation warnings by running tests. If there are any then fix them.

7) Once server is started successfully. You will get errors trying to load the application. Start fixing them one by one.

8) If you are upgrading from rails 4 to rails 5 then add following in application.rb as Rails 5 makes belongs_to association required by default

add the following to application.rb
config.active_record.belongs_to_required_by_default = true
or add the following in initializers
Rails.application.config.active_record.belongs_to_required_by_default = false

9) Do a global search for “redirect_to :back” and replace it with redirect_back(fallback_location: root_path)
This redirects to HTTP_REFERER when it is present and when HTTP_REFERER is not present then redirects to whatever is passed as fallback_location.

https://github.com/zdennis/activerecord-import/issues/138

10) If you are using resque then replace it with sidekiq as it is maintained and more frequently updated.
Useful links:
https://richardking.github.io/ruby/rails/2015/01/15/migrating-from-resque-to-sidekiq/
https://engineering.sharethrough.com/blog/2015/08/24/migrating-from-resque-to-sidekiq/
https://github.com/mperham/sidekiq/wiki/Delayed-Extensions#actionmailer

11) Sidekiq needs redis 4 or higher. If you are using redis 3 then you will need to upgrade it as well.

Errors encountered-
1) grpc-1.13.0-x86_64-linux requires ruby version >= 2.0, < 2.6, which is incompatible with the current version, ruby 2.6.2p47
Solution: gem install grpc — platform ruby
source: https://github.com/grpc/grpc/issues/17594

2) ERROR: Can’t install RMagick 4.1.2.
Can’t find the ImageMagick library or one of the dependent libraries.
Check the mkmf.log file for more detailed information.

Solution: sudo apt-get install libmagickwand-dev imagemagick

3) Node.js 8.x LTS Carbon is no longer actively supported!

You will not receive security or critical stability updates for this version.

You should migrate to a supported version of Node.js as soon as possible.
Use the installation script that corresponds to the version of Node.js you
wish to install. e.g.

* https://deb.nodesource.com/setup_10.x — Node.js 10 LTS “Dubnium”
* https://deb.nodesource.com/setup_12.x — Node.js 12 LTS “Erbium” (recommended)
* https://deb.nodesource.com/setup_14.x — Node.js 14 LTS “Fermium”

solution — replace step by step i.e 8x with 10x https://deb.nodesource.com/setup_10.x

4) NoMethodError: undefined method `for’ for #<Devise::ParameterSanitizer:0x000055c633277f50>

Solution: Replace
devise_parameter_sanitizer.for(:sign_in) {|u| u.permit(:username, :password, :remember_me)}
with devise_parameter_sanitizer.permit(:sign_in, keys: [:username, :password, :remember_me])

5)
Bundler could not find compatible versions for gem “actionmailer”:
Bundler could not find compatible versions for gem “activemodel”:
In snapshot (Gemfile.lock):
activemodel (= 5.0.7.2)

In Gemfile:
rails (= 5.0.0) was resolved to 5.0.0, which depends on
actionmailer (= 5.0.0)

resque_mailer (= 2.4.3) was resolved to 2.4.3, which depends on
actionmailer (>= 3.0)

This could be because some old versions of gem needs to be upgraded. Remove all gems and replace with only those gems which will be created by default when a new application is created and then add required gems one by one
https://github.com/redis-store/redis-activesupport

6) config/application.rb:36:in `<class:Application>’: uninitialized constant Rack::SslEnforcer
Solution:
spring stop
spring binstub — all
spring start
rails s

7) /fileutils.rb:239:in `mkdir’: Permission denied @ dir_s_mkdir — /home/shanaya/projects/PlayCricketRelease2/tmp/pids (Errno::EACCES)

Solution:
sudo mkdir tmp
sudo chmod 777 tmp

8) If you are using docker then you may encounter following error — ActiveRecord::AdapterNotSpecified: ‘postgresql’ database is not configured

To fix this you need to set the environment to your bash_profile or .env file
RACK_ENV=development (development or production)

9) Once application is running fix all deprecation warnings

10) If you have enabled turbolinks then you will have to replace page:load with turbolinks:load in assets.

11) You should be able to re-run rake generate devise:install and it will create the necessary files again. Any conflicts will be printed to the console and you can remove the necessary files and rerun the task again.

12) :nothing` option is deprecated and will be removed in Rails 5.1. Use `head` method to respond with empty response body
Solution: replace render nothing: true with render head :ok

13) ActionView::Template::Error (include_blank cannot be false for a required field.):

Solution: remove include_blank: false

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store