Madogiwa Blog

主に技術系の学習メモに使っていきます。

Ruby on Rails: 個人のサービスをRails7にアップグレードしたのでやったこととかメモ📝

2021/12/15 Rails 7がリリースされました🎉

rubyonrails.org

さっそく個人のサービスをRails 6.1.4.4 -> Rails 7にアップグレードしたのでやったこととかメモ📝

Rails 7の主要な対応

While most Rails applications won’t need a dependency on Node given these defaults, we’ve still managed to also dramatically improve the integration story for those who do in Rails 7.

import_mapによりnode依存を解消!

madogiwa0124.hatenablog.com

また以下の大きな新機能 ※ActiveRecordで属性の暗号化、SQLコメントで実行元をトレース、非同期のeagar_loading

  • At-Work Encryption With Active Record
  • Trace Query Origins With Marginalia-Style Tagging
  • Asynchronous Query Loading
  • Zeitwerk Exclusively

新しいブランドサイトもカッコいいですね👍

rubyonrails.org

引用元:https://rubyonrails.org/2021/12/15/Rails-7-fulfilling-a-vision

Rails 7へのアップグレード手順

Rails以外のgemのバージョンアップ

まだRails 7に対応してない可能性があるので、まずはRails以外のgemを最新に上げときます。

Railsのバージョンアップ

上記でrails以外のgemが最新になっているはずなのでbundle update railsでバージョンをあげます。

bundle update時にRails 7に対応してないgemが見つかった場合は、masterに向ける OR 対応のPRが出ていれば、そのbranchに向けたりすることも検討します。

Upgrade Guideを読む

edgeguides.rubyonrails.org

上記を読んで自身のサービスに影響がありそうなところを把握します。

自分のサービスだと以下の変更はキャッシュ及び暗号化された値の読み込みに影響がありそうでしたが、 今回のサービスではキャッシュは無くなってもいいのと暗号化された値も暗号化されたクッキーが読めず、 ログインセッションが切れる等が発生しても問題ないので一旦そのまま入れることにしました。

  • 3.10 Key generator digest class changing to use SHA256
  • 3.12 New ActiveSupport::Cache serialization format

リリースノートにも目を通しておきます。

edgeguides.rubyonrails.org

RailsDiffで設定ファイルまわりの更新を取り込む

RailsDiff is about what you'd have to change about your app's configuration when upgrading Rails versions, not about what Rails has changed internally. https://railsdiff.org/

バージョンごとの設定ファイルの差分を閲覧出来るRailsDiffというサービスを使って差分を確認しつつ、反映が必要そうな箇所を修正していきます。

https://railsdiff.org/6.1.4.4/7.0.0

bin/rails app:updateを使っても良いですが、オーバーライドの良し悪しとかの判断がCLIの対話形式だとやりにくいと感じたので上記方法を選択しています。

正常に動作することを確認する

最後に既存のテストコードが通ることや、実際に動かしてみて動作することを確認できればOKです🎉

アップグレード時に発生したエラーとか

missing require leading to uninitialized constant ActiveSupport::XmlMini::IsolatedExecutionState

テスト実行時にmissing require leading to uninitialized constant ActiveSupport::XmlMini::IsolatedExecutionStateが発生したため、 ActiveSupportをrequireするようにしました。

# config/application.rb

require 'active_support'

module MyApp
  class Application < Rails::Application

github.com

その他気づいたこと

redirect_toでopen redirectの検証がされるようになった

redirect_toでopen redirectの検証がデフォルトで入るようになったようです。

By default, Rails protects against redirecting to external hosts for your app's safety, so called open redirects. Note: this was a new default in Rails 7.0, after upgrading opt-in by uncommenting the line with raise_on_open_redirects in config/initializers/new_framework_defaults_7_0.rb https://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to

従来どおりの挙動にする場合は引数allow_other_hostにtrueを渡す。

redirect_to "https://rubyonrails.org", allow_other_host: true

ActiveRecord::Core#strict_loading!が追加されてた

今までGlobalで設定されていたsrict_loadingを無効化したいケースのためにmasterから実装を持ってきてパッチを当ててたのですが、publicなメソッドとして追加されてました。

Sets the record to strict_loading mode. This will raise an error if the record tries to lazily load an association. Parameters:

  • value - Boolean specifying whether to enable or disable strict loading.
  • mode - Symbol specifying strict loading mode. Defaults to :all. Using

https://api.rubyonrails.org/classes/ActiveRecord/Core.html#method-i-strict_loading-21

modeも指定出来るようになっていて、念願のn_plus_one_onlyも指定出来るのですが、globalでn_plus_one_onlyに指定する方法が分からなかった。。。(strict_loading_modeはdefaultでallで、read_attributesになっていた。。。)

madogiwa0124.hatenablog.com

参考

inside.pixiv.blog