<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by De Wet on Medium]]></title>
        <description><![CDATA[Stories by De Wet on Medium]]></description>
        <link>https://medium.com/@dewet_25828?source=rss-fee4439d07ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*8JYG-Pl5HWwj6jhC.</url>
            <title>Stories by De Wet on Medium</title>
            <link>https://medium.com/@dewet_25828?source=rss-fee4439d07ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:20:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dewet_25828/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Upgrading Rails using Bootboot, it’s not that simple]]></title>
            <link>https://medium.com/@dewet_25828/upgrading-rails-using-bootboot-its-not-that-simple-814c7e1442b8?source=rss-fee4439d07ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/814c7e1442b8</guid>
            <dc:creator><![CDATA[De Wet]]></dc:creator>
            <pubDate>Mon, 28 Aug 2023 20:11:22 GMT</pubDate>
            <atom:updated>2023-08-28T20:11:22.760Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why would you read this?</h3><p>You can use this post as a guide for upgrading your Rails application to a newer version of Rails.</p><h3>Background</h3><p>Recently, we had to upgrade a 6-year old Rails application that’s been in production use every day. In that time it has received 3532 commits and grew to 52305 lines of code. It is small in comparison to our original monolith, but we wanted to document everything we learned from this upgrade and our other services, in order to upgrade our monolith as smoothly as possible.</p><h3>Decide if you need Bootboot</h3><ul><li>Do you plan to work on doing a larger Rails upgrade after this one and take your learnings forward?</li><li>Are you on a team with other people that will continue making code changes while you work on the upgrade?</li><li>Do you want to do a bit of the upgrade now, and leave it for a few months before finishing?</li><li>If you answered “yes” to any of the above, use Bootboot. If not, this article might not be a great fit for you. I’m sure you can still get value out of it by skipping the BootBoot bits but there are probably a lot of other articles for more straightforward Rails upgrades.</li></ul><h3>Before you even think of beginning</h3><p>If you have Sidekiq workers that receive an Active Record Object or another complex object as an argument, fix it first. It is the 1st rule of Sidekiq best practices here <a href="https://github.com/mperham/sidekiq/wiki/Best-Practices">https://github.com/mperham/sidekiq/wiki/Best-Practices</a> and your code might be breaking this rule right now and running reliably. However, it will break any jobs in your queue when you upgrade because the serializer for Active Record objects is different and completely incompatible in different versions of Rails.</p><h3>Before you begin</h3><ul><li>Read the readme for Bootboot <a href="https://github.com/Shopify/bootboot">https://github.com/Shopify/bootboot</a></li><li>Create a Bash alias: alias dn=&quot;DEPENDENCIES_NEXT=1&quot;</li><li>While you are in there, create another one: alias dn=&quot;DEPENDENCIES_NEXT=1 bundle exec rspec&quot;</li><li>Your fingers will thank you, because now you can just run dn bundle exec &lt;command&gt; to have bundler use all the dependencies in Gemfile_next.lock (if you don’t know what that is, go read the Bootboot readme)</li></ul><h3>Installing Bootboot</h3><p>This will be a small PR.</p><ul><li>Install Bootboot.</li><li>Let your current and next versions of Rails just be an identical copy of your current version of Rails.</li><li>Make sure your CI runs twice, once for your current version of Rails and once for the next version (which right now is still the current).</li><li>Get this merged</li></ul><h3>Create a checklist in your PR</h3><p>There is a lot of work to do here. If you separate out the thinking about what needs to be done from the doing, you will go a lot faster because you will skip all the context switching, create a checklist by pasting the following markdown into your PR description</p><pre>- [ ] Remove all the version specifications from your Gemfile except the Ruby and Rails version<br>- [ ] Install Bootboot <a href="https://github.com/Shopify/bootboot">https://github.com/Shopify/bootboot</a><br>- [ ] Talk to your team about merging your changes in at this time, it should make no difference to the app because your<br>- [ ] Gemfile.lock should be unchanged and the Gemfile_next.lock can just be a copy of it.<br>- [ ] Upgrade to the latest patch version of the current Rails version you are on (eg from 4.2.1 to 4.2.11)<br>- [ ] Fix broken tests and commit test changes separately from other code changes<br>- [ ] Make sure the app runs, fix broken things if it doesn&#39;t<br>- [ ] Fix deprecation warnings</pre><p>You have the choice to merge your code after each iteration here, but the amount of QA required made us decide against that.</p><p>Now, copy the next snippet to continue your to-do list, but paste it again for every version of Rails you need to go through. Eg: Paste it once to go from 4.2.11 to 5.0.7 and then again for 5.0.7 to 5.1.7 and again for 5.1.7 to 5.2.6 and so forth.</p><pre>- [ ] Decide if we want to merge now and do the rest of this in a future PR. We only need to QA the current version of Rails because the next version of Rails will not be running in QA or Production<br>- [ ] Upgrade to the latest patch version of the next minor version of Rails (eg from 4.2.11 to 5.0.7 or from 5.0.7 to 5.1.7)<br>- [ ] Fix broken tests and commit test changes separately from other code changes<br>- [ ] Make sure the app runs, fix broken things if it doesn&#39;t<br>- [ ] Fix deprecation warnings<br>- [ ] Make commits all the time, but definitely make a commit before running the upgrade script<br>- [ ] Run the upgrade script</pre><p>The final checklist will be long, but not missing something and not wasting time trying to decide what is next will speed you up tremendously.</p><h3>Why stop at each minor version of Rails?</h3><p>Each time you are on the latest patch release of a minor version, it gives you a lot of helpful error messages about what is going to break in the next version of Rails. Fixing them here to remove deprecation warnings is less chaotic, and seeing all green tests before and after your fix makes it easier to know you didn’t break anything.</p><h3>A tip to reduce the PR size (optional)</h3><ul><li>Get your branch up to date with master (consider rebasing if you have not yet received any PR review comments that would be lost by a rebase, this will let you know up-front if you are heading toward merge conflicts vs waiting until later).</li><li>Start a new branch from master and git cherry-pickall the commits that changed tests and test dependencies only. This PR can be reviewed separately and merged with minimal QA effort to reduce the workload of whoever has to review this giant update PR.</li></ul><h3>Why remove the gem version specifications before a Rails upgrade?</h3><p>The Gemfile.lockspecifies which version of each gem your app will use, after you specify it in the Gemfileand do a bundle installor bundle update, you don’t need to keep specifying it in the Gemfile because it is automatically specified in the Gemfile.lock. Despecifying it allows Bundler more freedom to find compatible versions of all other dependencies when you change the version of Rails.</p><h3>Why upgrade all the gems you can?</h3><p>If you are still reading this, you are going to break things by upgrading Rails, you are going to incur all the cost of a full QA regression test on your app, now is the time to update all your dependencies. If you don’t you will just be updating them one at a time later when security vulnerabilities get reported.</p><h3>After every single change</h3><p>bundle exec rspec Run your tests with the old version of Rails</p><p>dn bundle exec rspecRun your tests with the new version of Rails</p><h3>After a big change or a few small changes (or every change if you roll that way)</h3><p>bundle exec foreman Start server with old version of rails and see that things work.</p><p>dn bundle exec foreman sStart server with new version of rails and see that things work.</p><p>My experience was that 90% of the time if the server starts, everything is fine, so clicking around the application functionality after every change would be a waste of time, but definitely do it after a couple of changes.</p><h3>Read the failed Bundler output</h3><p>Carefully read a failed bundle install or bundle update, even if it seems overwhelmingly large.</p><p>Read this carefully. It will sometimes list MANY gems for which it could not find compatible versions, but when you read the actual reasons why no compatible version was found, you might find that there are only one or two real conflicts.</p><p>Example:</p><pre>Bundler could not find compatible versions for gem &quot;rails&quot;:<br>  In Gemfile:<br>    rails (~&gt; 6.0.3)</pre><pre>    gem-one was resolved to 0.3.0, which depends on<br>      rails (&gt;= 4.2, &lt; 5.3)</pre><pre>    gem-two was resolved to 2.3.0, which depends on<br>      swagger-docs (~&gt; 0.2.9) was resolved to 0.2.9, which depends on<br>        rails (&gt;= 3)</pre><p>Let’s break this down gem-oneis the problem here, because it depends on Rails smaller than 5.3 and you are trying to upgrade to 6.0.6</p><p>gem-two is just useless output here, making you think that you can’t possibly resolve this long list of dependencies and putting you at risk of giving up on reading all the output and resolving all the dependencies. It depends on a version of swagger-docs which depends on a version of rails greater or equal to 3, which you certainly have here. This output will go away after resolving the dependency conflict with gem-one. I have seen pages of output with only one or two actual conflicts. Read it all and just make a note of every actual conflict you see and try to resolve it by specifying versions in your Gemfile. I wasted hours just trying different things trying to avoid reading this output in detail, it’s a trap!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*OQkqRXdNk06c-92k7TWCYw.jpeg" /></figure><h3>Running the Rails upgrade script</h3><p>Make commits all the time, but definitely make a commit before running the upgrade script.</p><p>dn bundle exec rails app:update</p><p>Say yes to all the changes, and then use Git to go through them. It will try to throw away a lot of your needed custom configurations, don’t commit that!</p><p>New files are good, keep them if you can, newer versions of Rails could expect them to be there. Removing lines of configuration that older versions of Rails generated is a good practice, but it might not be realistic to get every single line because it is not easy to spot them in the midst of everything else the script deleted, the number of changes here can be overwhelming. Don’t let perfect be the enemy of good here.</p><p>New lines added to your Rails environment and other configuration files are good, if a newer version of Rails expects it to be there by default, see if you can keep it.</p><h3>Workarounds</h3><ul><li>Rails 4.x and 5.x has a different base class for migrations, you can create this BaseMigration class and let all your migrations inherit from it instead of inheriting from ActiveRecord::Migration</li></ul><pre>if ENV[&quot;DEPENDENCIES_NEXT&quot;]<br>  class BaseMigration &lt; ActiveRecord::Migration[4.2]<br>  end<br>else<br>  class BaseMigration &lt; ActiveRecord::Migration<br>  end<br>end</pre><ul><li>A boolean typecasting method got a new name; I saw we use the same code to cast booleans in several projects. Create the following module you could call</li></ul><pre>module Boolean<br>  def self.value_to_boolean(val)<br>    if ENV[&quot;DEPENDENCIES_NEXT&quot;]<br>      !!ActiveRecord::Type::Boolean.new.deserialize(val)<br>    else<br>      !!ActiveRecord::Type::Boolean.new.type_cast_from_database(val)<br>    end<br>  end<br>end</pre><ul><li>When migrating to Rails 5 specs there should be a different way to pass API request body and headers, I used rails5-spec-converter to automate this migration and then this piece of code to keep supporting both versions</li></ul><pre>unless ENV[&quot;DEPENDENCIES_NEXT&quot;]<br>  # supporting rails4 specs after rails5-spec-converter<br>  module ActionDispatch<br>    module Integration #:nodoc:<br>      module RequestHelpers<br>        %w(get post patch put head delete).each do |method|<br>          define_method(method) do |*args|<br>            action, opts = args<br>            process(method, action, *opts&amp;.values)<br>          end<br>        end<br>      end<br>    end<br>  end</pre><pre>  class ActionController::TestCase<br>    module Behavior<br>      %w(get post patch put head delete).each do |method|<br>        define_method(method) do |*args|<br>          action, opts = args<br>          process(action, method.upcase, *opts&amp;.values)<br>        end<br>      end<br>    end<br>  end<br>end</pre><ul><li><a href="https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#actioncontroller-parameters-no-longer-inherits-from-hashwithindifferentaccess">https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#actioncontroller-parameters-no-longer-inherits-from-hashwithindifferentaccess</a> — this is the most tricky change because it is easy to miss some params and if the test suite won’t catch it it’s a serious source of possible defects.</li></ul><h3>CI</h3><p>If your cost of CI build minutes or running the tests locally is slow, that problem is about to double because you need to run all tests for the current version of Rails and the new version.</p><h3>k8s</h3><p>We had to create a separate primary app for canary deployments to ensure that the build process will be separate for Rails 4 and Rails 5 as they have a different set of dependencies. Canary ingress config defines the percentage of requests served by the canary deployment.</p><h3>Testing</h3><p>To find the source of the problems during the QA, I’ve added NEXT indication in several places:</p><ul><li>conditional response header for API requests</li><li>conditional Bugsnag app version</li></ul><h3>Deployment</h3><p>This is how I <strong>wrongly imagined</strong> deployment in my head:</p><ol><li>Deploy the code</li><li>Change the env var</li><li>Roll The Pods</li><li>Reverse these steps if there was a problem</li></ol><p>That didn’t work because the Docker image is built with a specific version of the dependencies, just rolling the pods on a K8S deployment means they still build from the same Docker image.</p><p>What we actually needed was the following:</p><ol><li>Merge your last PR that gets all your tests passing in your current and new version of Rails, double-checking everything in QA for the current version of Rails.</li><li>Create a smaller and simpler PR that uninstalls Bootboot and removes all the conditional logic and replace your Gemfile.lock with the Gemfile_next.lock</li></ol><p>How is Bootboot any good if you still need to do this in the end?</p><p>This PR is small, and easy to revert without worrying that someone else merged something that created a conflict. So the risk is still greatly reduced vs not using Bootboot. And, while you are still finishing up the upgrade, you can have everyone else’s new code be tested in CI against the new version of Rails.</p><h3>In closing</h3><p>Huge shoutout to <a href="https://github.com/dmitry-semenov">Dmitry Semenov</a> who started writing down things he learned from an upgrade he did before me, which meant I didn’t need to start at 0.</p><p>Thank you for reading to the end. I feel confident that the time you spent reading will be a lot less than the time you save with the knowledge you gained. Please let us know in the comments if you find a better way to do any part of this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=814c7e1442b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>