- Rails アップグレードガイド
- Railsを6.1系から7.0系へアップグレードした時に調査したこと
- Rails7.0アップグレード手順とハマりどころのTips
- 永久保存版!?伊藤さん式・Railsアプリのアップグレード手順
- Classic から Zeitwerk への移行
- 使用しているGemでRails7サポートがまだのものがあるかを調べる
- Rails以外のgemをUpdateする
- Rails以外でのUpdateでの阻害をここで潰しておく
- Rubyのバージョンは2.7.0以降が必須とのことなので、特に問題は無いはず
- Gemfileファイル内のRailsバージョン番号を変更し、bundle updateを実行する。
- [- package.jsonファイル内のRails JavaScriptパッケージのバージョンを変更する。] Rails側ではJSを使用していない模様で、pakcage.jsonは無いのでこのステップはskip
- アップデートタスクを実行する
$ bin/rails app:update
対話形式で新しいバージョンでのファイル作成や変更を聞かれてくるので、コンフリクトが発生したら、基本的に手動で変更(m)する。(mergetoolが起動)
一回やり直したい場合、quitで途中で中断できる
config/initializers/new_framework_defaults_X.Y.rbが作成されたことを確認する
- Railsが起動することを確認する
$ bin/rails server
エラーになっている箇所を修正する
- テストが通ることを確認する
$ bundle exec rspec
アップデートにより失敗しているテストを修正する
-
動作確認をする
-
問題なければPRを出す
事前の調査で恐らくつまづきそうなポイントを以下に上げます
config.autolader
でclassicを使用しているが、7.0から設定そのものが無くなっているので、一旦zeitwerk
に設定して問題ないことを確認する必要がありそう
以下を参考に対応を行う Classic から Zeitwerk への移行
フレームワークのデフォルトを設定する
- config/initializers/new_framework_defaults_X.Y.rbの内容を1個ずつコメントアウトして確認する
- すべてコメントアウトして問題なければ、new_framework_defaults_X.Y.rbは削除する
- load_defaultsを7.0に上げる
config.load_defaults 7.0
-
Gemfile.lockを削除して bundle install
-
その場合、関係ないGemの更新が入ってしまう...
-
updateにエラーになったGemを一つずつ追加していくと、Gemfile.lockを削除してinstallをやり直さなくてもよい
-
config/application.rb
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w(assets tasks))
# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
- environments/development.rb
# since you don't have to restart the web server when you make code changes.
config.enable_reloading = true # cache_classes の反対?
# Enable server timing
config.server_timing = true # https://techracho.bpsinc.jp/hachi8833/2021_10_11/112364#1-1
# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise
- environment/production.rb
# Full error reports are disabled and caching is turned on.
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Don't log any deprecations.
config.active_support.report_deprecations = false
- environment/test.rb
# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true
# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true
https://tech-book.precena.co.jp/software/backend/ruby-on-rails/upgrade-6-1-to-7-0
独自の内部ライブラリの構造がルールに則っていないためエラーになる
- ライブラリのエントリーポイントですべてのライブラリがrequireされていた
- autloader.ignoreにルールに沿っていないファイルパスを追加していく