skip to content

In Rails, how would you upgrade a municipal-library catalogue app from 7.1 to 8.1 safely, and why one minor version at a time?

level: seniorimportance: must knowfreq 55%

answer

  1. Ruby first, then Rails
  2. latest patch, zero deprecations
  3. 7.2, then 8.0, then 8.1
  4. bin/rails app:update after each bump
  5. flip defaults, then raise load_defaults

basics

~20 s

Move Ruby to 3.2+ on 7.1, reach the latest 7.1 patch with no deprecation warnings, then step through 7.2, 8.0 and 8.1: bump the gem, run bin/rails app:update, fix removals, deploy, flip the new defaults, raise load_defaults.

solid answer

~50 s

Start with good test coverage. Upgrade Ruby separately and first, because Rails 8.0 and 8.1 need Ruby 3.2 while 7.1 runs on 2.7. Move to the latest 7.1 patch and clear every deprecation warning. Then repeat for 7.2, 8.0 and 8.1: change the Rails version in the `Gemfile`, run `bundle update rails`, run `bin/rails app:update` and review every conflict with its diff, run the tests, fix what that minor removed, and deploy on the old defaults. Next, uncomment the lines in that step's `new_framework_defaults_X_Y.rb` one at a time, then raise `config.load_defaults` and delete the file. One minor at a time matters because breaking changes are announced as deprecations in the release before, and because Rails 8.1's generator only writes `new_framework_defaults_8_1.rb`. Jump straight from 7.1 and the 7.2 and 8.0 defaults arrive with no staging file.

code

bash · 7 lines
bash
# One step of the loop: 7.2 to 8.0 (Gemfile already says ~> 8.0.0)
bundle update rails
bin/rails app:update
#     conflict  config/application.rb
# Overwrite .../config/application.rb? (enter "h" for help) [Ynaqdhm]
bin/rails db:migrate
bin/rails test

go deeper

for a junior

Recall the order: tests, Ruby, latest patch, then one Rails minor at a time with bin/rails app:update.

for a middle

Explain what app:update creates and asks, and why the defaults file is flipped line by line before load_defaults changes.

for a senior

Plan the deploys: old defaults first, one default per deploy, format-changing defaults after all servers run the new code, and the removals each minor brings.

for a principal

Decide how much of the path to batch, how to staff it, and how to keep feature work moving while the catalogue climbs three minors.

## The scenario A municipal library runs its catalogue, holds and fines on a Rails 7.1 app, on an older Ruby, with `config.load_defaults 7.1`. The goal is Rails 8.1 without a weekend of outages. The Rails upgrade guide gives the general order; the work is in applying it per step. ## Before touching Rails 1. **Test coverage.** The guide's first advice: make sure the tests exercise the bulk of the app before starting, because every behaviour may change. 2. **Ruby first, separately.** Rails 7.0 and 7.1 need Ruby 2.7+, 7.2 needs 3.1+, and 8.0 and 8.1 need 3.2+. Move the catalogue to a Ruby of 3.2 or newer while it is still on Rails 7.1, ship that, and only then start on Rails. (Ruby's own upgrade concerns belong to the Ruby topics.) 3. **Latest patch of the current minor.** Move to the newest 7.1.x patch and fix every deprecation warning. Generated apps log them in development and print them to stderr in test; making them fail the test run keeps them fixed. ## One minor at a time: 7.2, then 8.0, then 8.1 For each step the guide's loop is the same: 1. Change the Rails version in the `Gemfile` and run `bundle update rails`; resolve any gem that does not yet support the new minor. 2. Run `bin/rails app:update`. It regenerates config files, `bin/` stubs and `public/` files, copies any new Active Storage migrations, and creates `config/initializers/new_framework_defaults_X_Y.rb`. Every changed file stops at a prompt: `Y` overwrite, `n` skip, `a` overwrite all, `q` quit, `d` show a diff, `m` merge with the tool in `THOR_MERGE`. Read each diff; your edits to `config/environments/production.rb` must survive. 3. Run the tests and fix what that minor **removed**. Examples on this path: 7.2 removed calling `ActiveSupport::Deprecation` as a singleton (use a deprecator instance) and changed `alias_attribute` to bypass custom reader methods; 8.0 removed defining `enum` with keyword arguments; 8.1 removed `bin/rake stats` (use `bin/rails stats`) and the `:azure` Active Storage service. 4. **Deploy on the old defaults.** `app:update` keeps the existing `config.load_defaults` value, so this deploy changes the framework code but not its defaults. 5. Uncomment the lines of `new_framework_defaults_X_Y.rb` one at a time, deploying between them; then delete the file and raise `config.load_defaults` to the new version. ## Why not jump straight to 8.1 - **Deprecations only warn one step ahead.** Rails pairs breaking changes with deprecation notices in the previous minor. Running 7.2 shows what 8.0 will remove; jumping from 7.1 to 8.1 turns those warnings into `NoMethodError`s with no hint. - **Only the current defaults file exists.** Rails 8.1's generator carries a single template, `new_framework_defaults_8_1.rb`. An app that jumps from 7.1 gets no `_7_2` or `_8_0` file, so raising `load_defaults` to 8.1 flips three versions of defaults at once, unstaged. - **Smaller diffs.** Each step's `app:update` diff and test failures belong to one release, which makes them reviewable. ## What each step removes on this path | Step | Removed or changed | What to do | |---|---|---| | 7.1 to 7.2 | `ActiveSupport::Deprecation` no longer delegates to an instance | call `warn` on a deprecator instance | | 7.1 to 7.2 | `alias_attribute` bypasses custom methods on the original attribute | define the alias with `alias_method` where the custom reader matters | | 7.2 to 8.0 | `enum` with keyword arguments | `enum :status, { ... }` | | 8.0 to 8.1 | `bin/rake stats` | `bin/rails stats` | | 8.0 to 8.1 | the `:azure` Active Storage service | move to another storage service first | Each of these printed a deprecation warning in the minor before it was removed, which is the practical reason to stop on every minor: the app tells you what the next step will break. ## Rolling deploys and stored data Some defaults change formats written to shared storage: caches, cookies, encrypted or signed messages. During a rolling deploy, old and new servers run side by side. The upgrade guide's example is the 7.1 cache format: leave `config.active_support.cache_format_version` unchanged on the first deploy and enable 7.1 on a later one, so servers not yet upgraded can still read what upgraded servers write. Treat any format-changing default the same way: **new code first, new format second**. ## A checklist per step - Ruby supported by the target minor? - Latest patch of the current minor, deprecation-free? - Gemfile bumped, `bundle update rails` clean? - `app:update` diffs reviewed, migrations run? - Removals fixed, tests green, deployed on old defaults? - Defaults flipped one by one, `load_defaults` raised, defaults file deleted?

  • Why deploy each step before flipping any new default?
    It separates two kinds of change. The first deploy changes framework code with the old behaviour kept by `load_defaults`; if it breaks, the cause is code. Each later deploy flips one default from `new_framework_defaults_X_Y.rb`, so a regression points at one setting. It also lets every server run the new code before formats written to caches or cookies change.
  • What should you look for in the bin/rails app:update diff of config/environments/production.rb?
    Your own settings that the regenerated template would drop or reset, such as hosts, mailer URLs, log level or SSL options, next to genuinely new generated lines. Use `d` to see the diff and `m` to merge, or skip the file with `n` and copy the new lines by hand.
  • Which Rails version line governs how much each step can break?
    The minor. Rails treats minors like semver majors: 7.2, 8.0 and 8.1 may each remove what the release before deprecated. Patches inside a series only fix bugs, which is why reaching the latest patch first is cheap and useful.

  • Bump Ruby and Rails in the same deploy to save a release.
  • Go straight from 7.1 to 8.1; app:update generates every missing defaults file.
  • Accept every bin/rails app:update overwrite with a, it only touches generated files.
  • Raise config.load_defaults in the same commit as the Gemfile bump.
  • Deprecation warnings can wait until the target version is installed.

Assumes the path 7.1 to 7.2 to 8.0 to 8.1 at Rails 8.1.4, whose generator ships only the new_framework_defaults_8_1.rb template and whose gemspecs require Ruby 3.2.