2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rails6から7にアップデートしたときに遭遇した`deprecation warning`について

Posted at

読んで欲しい人

  • Rails6から7系にアップデートする際deprecation warningにぶつかった人
  • 過去の自分

取り組んでいたこと

あるプロジェクトのRailsバージョンを6から7系にアップデートする作業を行っていました
正確には6.1.7.8から7.0.8.7です

その中でdeprecation warningに関しての設定が6と7で差分がありました

deprecation warningとはなにか?

現在、そのプロジェクトで使用しているメソッドや機能が、将来的には非推奨になって削除されますよ、気をつけてね

という警告です

例えば、

DEPRECATION WARNING: `old_method_name` is deprecated and will be removed in Rails 7.1. Use `new_method_name` instead.

上記の例ではold_method_nameはRails7.1では削除されるよ気をつけてね、という警告をしてくれています

deprecation warningに関するRails6から7の差分

Diffはこんな感じ

-  # Send deprecation notices to registered listeners.
-  config.active_support.deprecation = :notify
-
-  # Log disallowed deprecations.
-  config.active_support.disallowed_deprecation = :log
-
-  # Tell Active Support which deprecation messages to disallow.
-  config.active_support.disallowed_deprecation_warnings = []
+  # Don't log any deprecations.
+  config.active_support.report_deprecations = false

Rails6: config.active_support.deprecation = :notify

config.active_support.deprecation = :notifyとは、非推奨のメソッドや機能がプロジェクトに含まれるときに:notifyしてくれる設定です。
:notifyなので通知を送ってくれる設定です

:notify以外だと下記を設定できます

  • :log: ログファイルに記録
  • :raise: 例外を発生
  • :silence: 通知を無視
  • :stderr: 標準エラー出力にメッセージを出力

Rails6: config.active_support.disallowed_deprecation = :log

config.active_support.disallowed_deprecation = :logとは、利用が許されない非推奨警告メッセージの設定です
logの場合は、ロガーに出力をしてくれます

config.active_support.deprecationと同様のオプションを設定することができます

紛らわしいポイント

disallowed_deprecationdeprecationは違います。

  • deprecationは非推奨
  • disallowed_deprecationは特に許可されていない非推奨

Rails6: config.active_support.disallowed_deprecation_warnings = []

config.active_support.disallowed_deprecation_warnings = []とは、どの「利用が許されない非推奨警告メッセージ」を設定するのか?を指定するコードです。

紛らわしいポイント

  • config.active_support.disallowed_deprecation_warnings = []はメッセージを設定せずに空配列を渡しているので、警告メッセージは設定されません。

設定をしたければちゃんと

config.active_support.disallowed_deprecation_warnings = ['特定の非推奨メッセージ']

みたいにメッセージを渡してあげる必要があります

また全ての非推奨メッセージを確認したい場合は

config.active_support.disallowed_deprecation_warnings = :all

と記述すると、メッセージを全て確認することができます

Rails7: config.active_support.report_deprecations = false

config.active_support.report_deprecations = falseを設定するとdeprecation warningからのメッセージを停止することができます。
これはdeprecationsdisallowed_deprecationの両方を含めます

基本的な設定について

基本は下記の構成が推奨されています

  • production: config.active_support.report_deprecations = falseを有効にする
  • test/development: config.active_support.report_deprecations = falseは記述せず、deprecation_warningsについては:raiseでエラーを発生させて、非推奨のものに気付けるようにしておく

test/developmentではconfig.active_support.disallowed_deprecation_warnings = :allだけ設定して、全てに気づくようにするというパワープレイもなしではないと思います

感想

  • この記述をすると設定が効くのか?falseにすると設定が消えるのか?など紛らわしい設定だなと思いました
  • 今後のアップデートでもうまく生かせそう

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?