How to upgrade Rails 6.1 to 7.0
Migrating from Rails 6.1 to 7.0 demands strategic execution due to new features and changes affecting performance, security, and functionality. Thoroughly understanding these modifications is crucial for a successful upgrade.
Before upgrading to Rails 7.0, make sure to upgrade your Ruby and Rails version. If you are using Rails 5.2 or above, please refer to our blog post on upgrading Rails 5.2 to 6.0.
Why you should upgrade to Rails 7
Here are some compelling reasons why you might consider upgrading to Rails 7:
-
Performance and Security:
-
Speed Boost: Rails 7 introduces several optimizations, like Hotwire and improved asset management, leading to faster loading times and smoother user experiences.
-
Enhanced Security: Rails 7 prioritizes security with updates like automatic vulnerability patching and stricter API access controls, keeping your application safe and sound.
-
-
Modernization and Functionality:
-
Fresh Features: Rails 7 offers exciting new features like Action Cable improvements, Hotwire Turbo for interactive UIs, and Active Storage Backgrounding, enhancing your app's capabilities.
-
Improved Developer Experience: Rails 7 simplifies development with features like Zeitwerk autoloading and improved test helper macros, making your team more efficient.
-
Future-Proofing: Staying up-to-date with the latest Rails version ensures compatibility with future libraries and gems, safeguarding your application's longevity.
-
How to prepare your application for Rails 7
-
Ensure you're using Ruby version 2.7.0 or higher. However, Ruby 3.0+ is preferred, as it offers better performance and integration with the new features in Rails 7.
-
Ensure your Rails 6 app is on the latest patch before the upgrade.
-
Aim for at least 80% test coverage to facilitate smoother migration.
-
Follow a Git flow workflow with dedicated staging and production environments.
Rails 7 upgrade steps
1. Update Ruby version
Using a version of Ruby below 2.7.0 will not work with Rails 7. While technically possible to use 2.7.0, Ruby 3.x versions offer additional benefits like improved performance and access to new language features introduced in Ruby 3.0 and later. Some features of Rails 7 are specifically designed for use with Ruby 3.x versions.
The Ruby Releases provides the history of all the versions, where you can explore the release notes and identify what can possibly go wrong with your upgrade process.
2. Update Gemfile Rails version to 7
Replace the existing Rails version with '~> 7.0.0' in your Gemfile.
gem "rails", "~> 7.0.0"
In your terminal, run:
bundle update
The command will update all dependent gems to their latest versions compatible with Rails 7.
3. Fix Gemfile dependencies
Like the Rails 5.2 to 6.0 upgrade,
you might face
Gemfile dependencies
issues on running bundle update
command.
4. Add sprockets to Gemfile
When using the asset pipeline, assets-related errors can pop up when running a rails command.
# - Don't know how to build task 'assets:precompile'
# - 'method_missing': undefined method `assets'
Sprockets is no longer a default dependency in Rails 7. You need to explicitly add it to your Gemfile if you still require its functionality for asset management. Here's how to do it:
- Add sprockets-rails to your Gemfile:
gem "sprockets-rails"
-
Run
bundle install
to install the gem. -
Adjust your application configuration. No further configuration is needed if you use the default Rails asset precompilation.
-
Verify your assets are compiling and working correctly. It would help if you ran
rails assets:precompile
to precompile your assets. Then, check your application and ensure all styles and scripts are loading as expected.
5. Run update rails task
Next, execute the below command in your terminal:
bin/rails app:update
The command analyzes your configuration files and prompts you if it detects any conflicts with default Rails 7 settings. You must carefully review each conflict and choose whether to overwrite existing files, create new ones, or manually merge changes. Pay close attention to file paths and configuration options to ensure compatibility with Rails 7.
Once the command completes, thoroughly review the updated files, especially:
-
config/application.rb
-
config/environments/*.rb
-
config/initializers/*
Ensure everything aligns with your application's needs and Rails 7 conventions.
6. Uncomment defaults in new_framework_defaults_7_0.rb
To ease the transition to Rails 7's new defaults,
running bin/rails app:update
generates a new_framework_defaults_7.0.rb file.
This allows you to gradually adopt the new settings across multiple deployments,
making the upgrade process more manageable.
The load_defaults
setting in the application.rb
acts as a time machine for
your Rails app's configuration.
Set it to 6.1,
and
you can harness Rails 7's power while still using familiar settings from Rails 6.1.
# config/application.rb
config.load_defaults 6.1
Steadily activate each setting in new_framework_defaults_7.0.rb,
checking your application's functionality after each change.
Once fully enabled,
remove the file
and
embrace Rails 7's defaults by setting load_defaults
to 7.0 in the application.rb
.
# config/application.rb
config.load_defaults 7.0
7. Remove deprecations or warnings and fix specs
Rails 7 introduces various deprecations. Consult the official Rails 7 release notes and update your code accordingly.
8. Testing your application
Even if your application has healthy test coverage, manual testing is required to ensure you get all crucial flow.
Thorough local testing is essential, but don't stop there. Deploy to a staging environment for a final reality check before production. This extra step can uncover issues that might slip through in development.
9. Release Rails 7 to production
Deploy your Rails 7 upgrade with confidence! Monitor for errors, but remember: thorough testing and a rollback plan pave the way for a smooth transition to improved stability, security, and all the benefits Rails 7 offers.
Need guidance on your Rails 7 adventure? We're your map and compass! Check out our resources, seek our expertise, and let's navigate the upgrade smoothly.