Skip to content

Instantly share code, notes, and snippets.

@tanish-kr
Created January 18, 2024 01:08
Show Gist options
  • Save tanish-kr/2ebc68a563c0a7be47f64123ec4435b9 to your computer and use it in GitHub Desktop.
Save tanish-kr/2ebc68a563c0a7be47f64123ec4435b9 to your computer and use it in GitHub Desktop.
Rails7 upgrade

Rails6 -> 7 Update

参考

事前準備

  • 使用しているGemでRails7サポートがまだのものがあるかを調べる
  • Rails以外のgemをUpdateする
    • Rails以外でのUpdateでの阻害をここで潰しておく
  • Rubyのバージョンは2.7.0以降が必須とのことなので、特に問題は無いはず

手順

  1. Gemfileファイル内のRailsバージョン番号を変更し、bundle updateを実行する。
  2. [- package.jsonファイル内のRails JavaScriptパッケージのバージョンを変更する。] Rails側ではJSを使用していない模様で、pakcage.jsonは無いのでこのステップはskip
  3. アップデートタスクを実行する
	$ bin/rails app:update

対話形式で新しいバージョンでのファイル作成や変更を聞かれてくるので、コンフリクトが発生したら、基本的に手動で変更(m)する。(mergetoolが起動)

一回やり直したい場合、quitで途中で中断できる

config/initializers/new_framework_defaults_X.Y.rbが作成されたことを確認する

  1. Railsが起動することを確認する
 $ bin/rails server

エラーになっている箇所を修正する

  1. テストが通ることを確認する
 $ bundle exec rspec

アップデートにより失敗しているテストを修正する

  1. 動作確認をする

  2. 問題なければPRを出す

つまづきそうなポイント

事前の調査で恐らくつまづきそうなポイントを以下に上げます

config.autolader でclassicを使用しているが、7.0から設定そのものが無くなっているので、一旦zeitwerkに設定して問題ないことを確認する必要がありそう

以下を参考に対応を行う Classic から Zeitwerk への移行

対応後

フレームワークのデフォルトを設定する

  1. config/initializers/new_framework_defaults_X.Y.rbの内容を1個ずつコメントアウトして確認する
  2. すべてコメントアウトして問題なければ、new_framework_defaults_X.Y.rbは削除する
  3. load_defaultsを7.0に上げる
 config.load_defaults 7.0

bundle update rails でエラーになる

  • 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

zeitwerkではまったこと

独自の内部ライブラリの構造がルールに則っていないためエラーになる

  • ライブラリのエントリーポイントですべてのライブラリがrequireされていた
  • autloader.ignoreにルールに沿っていないファイルパスを追加していく

rspec が動かない

テストでSessionを使用する振る舞いが変わった https://qiita.com/uichi/items/5c31f70b99582446ba00#%E3%83%86%E3%82%B9%E3%83%88%E3%81%A7%E3%82%BB%E3%83%83%E3%82%B7%E3%83%A7%E3%83%B3%E3%81%AE%E5%80%A4%E3%82%92%E3%82%BB%E3%83%83%E3%83%88%E3%81%99%E3%82%8B%E6%99%82%E3%81%AFactioncontrollertestsession%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment