Railsをv6.0.4.7からv7.0.2.3へアップデートする仕事をしました。
バージョンを一気にあげたのでどの対応がどのバージョンの変更によるものなのかはちょっとわかっていないけど対応したものを書いておきます。
やったこと
activerecord-multi-tenantをmasterを参照するようにする
Rails v7に対応した変更が入ったバージョンがまだリリースされていなかったのでmasterに向けた。
リダイレクトをしている箇所に allow_other_host: true
を指定する
対象のサービスはマルチテナントアプリでテナントをサブドメインで判別しているアプリなので別のホスト(別のサブドメイン)へリダイレクトする処理が結構あった。
Rails v7に上げると別のホストへのリダイレクト処理は例外が発生するようになっていたので別ホストへリダイレクトしている箇所は全て allow_other_host: true
を指定するようにすることで対応した。
Time.zone.tzinfo.ical_timezone(args)
のBreaking Changes対応
Railsのアップデートと同時にtzinfo gemもアップデートされたのでBreaking Changeがあり、timezoneの取得が変わった。
Before
Time.zone.tzinfo.ical_timezone(Time.zone.name)
After
tz = TZInfo::Timezone.get(Time.zone.name)
timezone = tz.ical_timezone(start_at)
ActiveRecord::Associations::Preloader#preload
のBreaking Changes対応
N+1を潰すためにActiveRecord::Associations::Preloader#preload
を使用している箇所があるがBreaking Changeがあったので以下のように書き換えた。
Before
ActiveRecord::Associations::Preloader.new.preload(records, @association_name)
After
ActiveRecord::Associations::Preloader.new(records: records, associations: @association_name).call
ActiveModel::Errors
のBreaking Changes対応
ActiveModelのエラーを扱うところを以下のように書き換えた。
Before
error.model.errors.keys.each do |attribute|
error.model.errors.full_messages_for(attribute)
end
After
error.model.errors.each do |model_error|
error.model.errors.full_messages_for(model_error.attribute)
end
add_template_helper
を helper
に書き換える
add_template_helper
はdeprecatedになっており今回methodが削除されたので全て helper
に書き換えた。
Before
add_template_helper(ApplicationHelper)
After
helper(ApplicationHelper)
render(partial: ...)
のファイル指定を変えた
どこの変更が影響してかはわかっていないんだけど今までのファイル指定だとファイルが見つからないと怒られるようになったのでファイル指定を少し変えた。
ちなみに該当ファイルは layouts/current_user_context.json.jb
というファイル名。
Before
%meta{ id: "currentUserContext", data: render(partial: "layouts/current_user_context.json", formats: :json) }
After
%meta{ id: "currentUserContext", data: render(partial: "layouts/current_user_context", formats: :json) }
config.action_view.raise_on_missing_translations
のBreaking Change対応
指定の仕方が変わったので書き換えた。
Before
config.action_view.raise_on_missing_translations = true
After
config.i18n.raise_on_missing_translations = true
bin/rails active_storage:update
の実行
ActiveStorageのアップデートにはDBのmigrationが必要で、このコマンドを実行するとアップデートに必要なmigrationファイルを生成してくれる。
ActiveJob::Logging::LogSubscriber
のBreaking Change対応
モジュールのネームスペースが変更になったので以下のように書き換えが必要だった。
Before
ActiveJob::Logging::LogSubscriber
After
ActiveJob::LogSubscriber
fixture_file_upload
にエラーが発生するようになったので Rack::Test::UploadedFile
を使うようにする
fixture_file_upload
実行時に以下のエラーが発生するようになった。
undefined method `file_fixture_path' for FactoryBot::SyntaxRunner:Class
このエラー自体は以下の記事の通り include ActiveSupport::Testing::FileFixtures
を rails_helper.rb
へ追記することで修正可能。
だが今回はよりシンプルに Rack::Test::UploadedFile
を代わりに使うようにすることで修正した。
Before
fixture_file_upload(Rails.root.join("spec/fixtures/avatar.png"), "image/png")
After
Rack::Test::UploadedFile.new(Rails.root.join("spec/fixtures/avatar.png"), "image/png")
以上!
そんなに大きなアプリでもないのでそんなに難しくなくバージョンを上げられてよかった。