Windows環境でRails 5.1.5のアプリケーションを5.2.0にアップデートしたのでその手順とトラブルシューティングを共有します。Windows環境と銘打っていますが多分他の環境でも同じ手順でアップデートできると思います。
なお rails new [APP_NAME] --api で生成したAPIサーバーの話なのでWEBサーバーの場合は別途作業が必要かもしれません。
なお、 GitHub上に アップデートPR を作成しているのでこちらを見ていただければ差分の概要がつかみやすいかもしれません。
アップデート前の環境
- Windows 10
- ターミナル作業は
Git Bashで実行
- ターミナル作業は
- Ruby 2.4.1p111
- Rails 5.1.5
- APIサーバー
-
bundle install --path vendor/bundleでgemはインストール
アップデート手順
事前準備
- Rubyのバージョンを2.5以上にする
-
bundle updateの実行
1. Railsのバージョンを5.2.0に変更する
Gemfileを以下のように変更します。
gem 'rails', '~> 5.2.0'
gemの更新。
bundle install --path vendor/bundle
2. 5.2.0での設定、基本構成の反映
公式ドキュメント に従って以下のコマンドを実行します、
rails app:update
すると以下のような上書き確認が表示されます。
Y で上書き、 n でスキップ、 d で事前の差分確認ができます。
基本的には d で差分を確認してから Y か n を決めた方がよいかと。
$ rails app:update
conflict config/boot.rb
Overwrite C:/coding/firebase-auth-api/config/boot.rb? (enter "h" for help) [Ynaqdh] h
Y - yes, overwrite
n - no, do not overwrite
a - all, overwrite this and all others
q - quit, abort
d - diff, show the differences between the old and the new
h - help, show this help
ちなみにここで変更が発生するファイルは以下になります。
歴史のあるアプリケーションだと config/application.rb config/environments 以下は慎重に反映した方がいいですね。
- bin/bundle
- bin/setup
- bin/update
- config/application.rb
- config/boot.rb
- config/cable.yml
- config/environments/development.rb
- config/environments/production.rb
- config/environments/test.rb
- config/puma.rb
要注意なのが config/environments に Active Storage の設定が追加されることです。
Active Storage が不要ならコメントアウトするか削除すると作業手順が減ります。
+ config.active_storage.service = :local
3. gem bootsnapの追加
ここで rails s をすると以下のエラーが発生します。
これはRails 5.2から bootsnap を利用して起動するようになったからです。
$ rails s
Traceback (most recent call last):
3: from bin/rails:3:in `<main>'
2: from bin/rails:3:in `require_relative'
1: from C:/coding/firebase-auth-api/config/boot.rb:4:in `<top (required)>'
C:/coding/firebase-auth-api/config/boot.rb:4:in `require': cannot load such file -- bootsnap/setup (LoadError)
config/boot.rb を見ると以下の行が追加されています。
+ require 'bootsnap/setup' # Speed up boot time by caching expensive operations.
gem bootsnapが必要なのでGemfileに以下を追加しましょう。
gem 'bootsnap', require: false
Active Storage を導入していないならばここでアップデート作業は終了です。
rails s が実行できるようになっています。
4. Active Storageの設定
2 で Active Storage を導入したならば追加の作業が必要です。
この時点で rails s を実行すると以下のエラーが発生します。
3: from C:/coding/firebase-auth-api/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/railtie.rb:216:in `configure'
2: from C:/coding/firebase-auth-api/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/railtie.rb:216:in `instance_eval'
1: from C:/coding/firebase-auth-api/config/environments/development.rb:31:in `block in <top (required)>'
C:/coding/firebase-auth-api/vendor/bundle/ruby/2.5.0/gems/railties-5.2.0/lib/rails/railtie/configuration.rb:97:in `method_missing': undefined method `active_storage' for #<Rails::Application::Configuration:0x00000000061932b8> (NoMethodError)
Gemfileに以下を追加して bundle install を実行しましょう。
gem 'activestorage', '~> 5.2.0'
config/application.rb に以下を追加して rails active_storage:install を実行します。
require 'active_storage/engine'
db/migrate 以下に Active Storage 用のテーブルのマイグレーションスクリプトが追加されるので db:migrate を実行します。
- active_storage_attachments
- active_storage_blobs
これで rails s で起動するようになります。