I took an abandoned, multi-tenant Rails SaaS app from Ruby 2.4 / Rails 5.0, both end-of-life for years, to Ruby 3.4 / Rails 8.1, one version at a time. Each step had green tests and a git tag. Along the way I found five real bugs, including one that let any user wipe an account's data with a single URL. The slowest page went from 38 seconds to 1.7.
- 167 → 0known gem vulnerabilities
- 21.9×faster CSV export
- −57%process memory
- 5app bugs found and fixed
All code, notes and raw benchmark data are public: github.com/Hadayxinchao/hours-rails-upgrade. Every claim below links back to a tag or file you can check.
The app, and why I picked it
Hours is an open-source time-tracking SaaS by Defacto Software (MIT licence). Accounts live on subdomains, each in its own Postgres schema. It has clients, projects, hours, mileage, reports, CSV export, billing and invitations. Its last commit was in June 2020.
That makes it a realistic "please upgrade our Rails app" codebase. It has real business logic, multi-tenancy, a jQuery and Sass front end, and dependencies that have since died. It also has one thing many client apps don't: a real safety net of 280 specs with 99.3% line coverage.
Here is what I measured before touching anything:
| Runtime | Ruby 2.4.2 (EOL 2020), Rails 5.0.7 (EOL 2018) |
|---|---|
| Known gem vulnerabilities | 167 advisories in 26 gems (4 critical, 43 high) |
| Brakeman | 8 warnings, including SQL injection ×3 and remote code execution ×1 |
| Fresh install | Failed: mimemagic 0.3.2 had been yanked from RubyGems |
| Production boot | Crashed: newrelic_rpm 3.9 rejects Rails 5.0 |
| Framework defaults | Still Rails 4.2 behaviour: every 5.0 default switched off |
| CI | Travis CI, no longer usable |
Approach: measure first, then one hop at a time
- Reproduce the original environment. I froze Ruby 2.4 and the 2020 gem set in Docker, so the "before" numbers are measured, not guessed.
- Build a benchmark that works on every version. It uses the same seed data (20,000 hour entries) and the same 9 pages, timed through the full Rack stack in production mode. The same script runs on Rails 5.0 and Rails 8.1.
- Upgrade one minor version at a time. For each hop: bump the version, run
rails app:update, keep the app-specific config, and fix every deprecation. Then get the specs green and tag the commit. Ruby moves alongside Rails only when a gem forces it. - Replace dead dependencies with maintained ones instead of pinning them forever.
- Optimise last, on the new stack, using the same benchmark.
The path was 5.0 → 5.1 → 5.2 → 6.0 → 6.1 → 7.0 → 7.1 → 7.2 → 8.0 → 8.1, with Ruby going 2.4 → 2.6 → 2.7 → 3.0 → 3.3 → 3.4. That made seven tagged hops, plus a performance pass. Because each tag is a working app, a client can stop at any hop and still have something deployable.
What each hop actually found
Most upgrade work is not dramatic. It is update_attributes → update, uniq → distinct, and config flags. The interesting part is the failures that tests or a quick click-through would not have caught. A few examples:
Rails 5.2: a security patch that broke audit logging
The Rails 5.2.8.1 security release (CVE-2022-32224) switched YAML columns to safe loading. The audited gem stores changes as YAML, so every audited write raised Psych::DisallowedClass: Date and 89 specs failed. In production, nobody could have created or edited hours. The fix is an allow-list. But that allow-list is silently ignored by the Psych version on Ruby 2.4, so the Ruby upgrade had to come in the same hop.
Rails 6.0: a dependency that changed API under the resolver
Bundler quietly moved rails-assets-chartjs from 1.0 to 4.5, which has a completely different API. Every page with a chart raised an error. I pinned it to 1.x on purpose, and that pin is why I'll close the Dependabot PR that tries to bump it.
Rails 7: a CSS regression with zero failing tests
Ruby 3.3 can't run Ruby Sass anymore, so I moved the styles to Dart Sass. Everything compiled and every spec passed. But the Billables, Reports and Projects pages had silently lost their column layouts. The old Neat grid library relied on Sass 3.2 behaviour, where assigning a variable inside a mixin overwrote the global one. I only found it by diffing the compiled CSS rule by rule against a build from the 2020 environment, using a small semantic CSS diff script. After a !global patch, 1,474 declarations were identical, and the rest were formatting differences.
Rails 7.2: a production cache that only broke in production
connection_pool 3.0 switched to keyword arguments, but Rails 7.2's MemCacheStore still passes a hash. The app raised ArgumentError as soon as caching was turned on. The specs run with caching off, so they couldn't see it. I found it by running the store against a real memcached container, and added a spec that builds the production cache store.
Rails 8: two crashes that only the production image showed
First, Apartment connects to the database at boot, which breaks assets:precompile in a Docker build with no database. Second, Sprockets 3 can't read its own precompiled manifest with json 3. Development and tests never load that manifest, so only production crashed. Both were found by building and running the real image, not by the test suite.
Bugs that were there all along
An upgrade forces you to read code nobody has looked at in years. These five bugs were in the 2020 code, not caused by the upgrade:
🔴 Any signed-in user could delete an account's data with one GET request
The report filter took each key from the URL and called it as a method on the query:
# EntryQuery#filter (2020)
params[:entry_filter].each do |filter, value|
relation = relation.public_send(filter, value)
end
The keys came straight from the URL, so this worked:
GET /reports?entry_filter[delete_by]=1=1 # → Hour.delete_by("1=1")
GET /reports?entry_filter[update_all]=value=0 # → Hour.update_all("value=0")
Brakeman did not flag it. I confirmed it with a failing spec (the hours count went from 2 to 0) and fixed it in three layers:
- An allow-list of the 8 real filters.
- Permitted params in the controller.
- Arel conditions instead of string SQL.
On the running app, the exploit URL now leaves all 20,000 hours untouched.
The other four
- "Download CSV" ignored the report filters and always exported every entry. For a tool people use to invoice, that's a business bug: with a client and "not billed" filter applied, the export should have had 1,840 rows, not 23,000.
- Report filters crashed or forgot their values because of a
params.permitcalled on the wrong level. - A hardcoded Devise
secret_keyhad been public on GitHub since 2014. Devise now derives its keys from the environment. There is a transition variable, because rotating the key invalidates outstanding password-reset and invitation links. - The production cache was broken (the
connection_poolissue above).
Each one has a regression spec that fails without the fix.
Replacing dead dependencies
| Before | After | Note |
|---|---|---|
| Paperclip | Active Storage | A tenant-aware, idempotent data migration that keeps the old files for rollback |
| Ruby Sass, CoffeeScript, Uglifier | Dart Sass, plain JS, Terser | Compiled CSS proven equivalent by diff |
| Unicorn | Puma 7 | After fixing module-level state that was a race condition under threads |
| Delayed Job (installed, never used) | Solid Queue | Emails no longer go over SMTP inside the web request |
| memcached | Solid Cache | One less service to run |
gravatar_image_tag (2014) | A 15-line helper | The gem called URI.escape, which Ruby 3 removed, so every page with an avatar raised |
| Travis CI | GitHub Actions | Specs, Brakeman, bundler-audit and a production image build |
One trap is worth calling out. In a schema-per-tenant app, background jobs run outside a request, so they don't know which account they belong to. A Devise email job would look up user #1 in the wrong schema. It would either fail or, worse, email another account's user #1. I wrote a TenantAwareJob concern that stores the tenant when the job is enqueued and wraps the whole job in it, with a spec that reproduces the bug without it.
The performance pass
The upgrade alone made the app faster: Ruby 3.4 and the newer Rails cut the CSV export from 38 s to 20 s. But 20 seconds is still broken. The question was why.
The benchmark said the CSV export ran 96 queries, which looked harmless. It was wrong, because it didn't count query-cache hits. A profiler that counts every statement showed the real numbers:
| Page | SQL statements | Cause | After fix |
|---|---|---|---|
| CSV export | 89,006 | 4 lookups per row × 23k rows | 13 |
| Dashboard | 1,769 | 2 SUMs per category, recomputed 3× per project | 66 |
| Billables | 1,405 | The same EXISTS check in the controller and the view | 13 |
Cache hits are cheap in SQL time, but each one still builds an Active Record object. That's where the export's 38 million allocations came from. The fixes are ordinary: preloading, one GROUP BY instead of a query per category, memoization, and HAML 6, which cut view rendering time by about 40%.
Every optimisation was checked against the old code's output. The CSV exports are byte-identical, the Billables HTML is byte-identical, and the DOM of 18 pages matches.
Final numbers
To avoid comparing numbers from different days, I rebuilt all three versions from their own commits and benchmarked them back to back. They used the same data, Postgres 9.6, production mode and a single process.
| Page (p50) | Rails 5.0 / Ruby 2.4 | Upgrade only | Upgrade + perf |
|---|---|---|---|
| Reports CSV export (23k rows) | 37,997 ms | 20,002 ms | 1,735 ms (21.9×) |
| Billables (~10k rows) | 3,318 ms | 2,114 ms | 540 ms (6.1×) |
| Dashboard | 655 ms | 543 ms | 165 ms (4.0×) |
| Client detail | 374 ms | 240 ms | 120 ms (3.1×) |
| Project detail | 184 ms | 138 ms | 87 ms (2.1×) |
| Process memory | 833 MB | 453 MB | 358 MB (−57%) |
The one mistake I made, and how it was caught
One regression introduced by the upgrade did reach the code. On HAML 5.1 with the newer Rails output buffer, the "New/Edit project" form printed its whole content a second time, as escaped HTML text, under the billable switch. The specs passed because the real inputs were still on the page.
I caught it during the performance pass while diffing rendered pages between HAML versions. Rendering the same form on the original 2020 stack confirmed the bug wasn't there before. HAML 6 fixes it, and a new spec asserts that the form has no escaped markup and each input appears once. That spec fails on HAML 5.1.
The lesson I'm keeping: "specs are green" doesn't mean "the page looks the same." For an upgrade, I now diff the rendered output of key pages before and after each hop, not just at the end.
What I deliberately didn't do
Some changes are product decisions, not engineering ones. They're documented for the owner instead of done quietly:
- devise_invitable 3.x. It requires an irreversible data migration and changes when invited users are created, which would change what the Team page shows.
- Paginating Billables. It would make the page instant, but it changes how people review entries before invoicing.
- Switching cookies to JSON-only. This should wait one session lifetime after deploy, so existing sessions aren't logged out.
- Propshaft and importmap. Sprockets is still supported on Rails 8. Moving would mean replacing six jQuery plugins, with no benefit to users.
Result
| Before | After | |
|---|---|---|
| Stack | Ruby 2.4 / Rails 5.0 | Ruby 3.4 / Rails 8.1 |
| Known vulnerabilities | 167 | 0 |
| Brakeman warnings | 8 | 0 |
| Specs | 280 | 298, 0 deprecation warnings |
| Locked gems | 290 | 175 |
| Infrastructure | Postgres, memcached, idle job worker | Postgres only |
| Deploy | Broken | Multi-stage Docker image, GitHub Actions CI |
The full hop-by-hop log is in UPGRADE_LOG.md, and the benchmark method and raw JSON are in docs/case-study/final.
Have an app like this?
If your Rails app is stuck on an old version, I can help with:
- An audit and an upgrade plan.
- A version-by-version upgrade, with green tests at every step.
- Performance fixes backed by measurements.
- A Docker and CI setup your team can keep running.
Every step is tagged, so you can stop at any point and still have a working app. Get in touch.