kikeda1104's blog

備忘録・技術に関することを書いています。(webエンジニア)

Rails 5.0からRails 5.1へのアップデート(1/?)

こんにちは。時間の合間に作成しているサービスのRailsを5.0から5.1へアップデートしていまして その内容を書いています。rails upgrade guideが、まだ作成中で、issueを読んで対応していました。

テスト, jsの移行は、まだ終わっていなくて順次移行中です。

環境

Gemfile

Gemのversionをあげます。(環境によって異なります)

# Gemfile
gem 'rails', '5.1.0' # Use sqlite3 as the database for Active Record
gem 'puma', '~> 3.7'
gem 'webpacker'
gem 'coffee-rails', '~> 4.2'
  
group :development, :test do
 # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13.0'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

参考: https://github.com/kikeda1104/9syoku/blob/master/Gemfile

bundle install or bundle update

./bin/rails app:update

rails 5.0でも使われていた、binファイルや設定ファイルを更新してくれる./bin/rails app:updateがありますのでこれを利用します。 config、routeが上書きされるので、差分をとって編集しましょう。

./bin/rails app:update

secret.ymlの暗号化

./bin/rails secrets:setup

f:id:kikeda1104:20170504161357p:plain

secrets.yml.keyが作成され、gitignoreに追加されます。 secrets.yml.encを編集したい場合は、`./bin/rails secrets:edit'です。

f:id:kikeda1104:20170504161633p:plain 上のメッセージが出たら、EDITOR=vi bin/rails secrets:edit EDITORは、好みで変えてください。

productionのsecret_key_baseをこちらに設定して保存します。

webpacker

これは、新規だった場合に、rails new rail_app --webpack –webpackオプションを指定すると、自動でwebpacker gemがGemfile bundle installによりgemがインストールされます。

./bin/rails -T 一部省略

f:id:kikeda1104:20170504162456p:plain

namespaceにwebpackerとついているのは、webpacker gemが導入されることにより追加されるタスクです。 vueを含めてみます。

./bin/rails webpacker:install:vue

f:id:kikeda1104:20170504163358p:plain

migrationファイル

ActiveRecord::Migration => ActiveRecord::Migration[5.0 or 5.1]に修正

./bin/rails db:drop RAILS_ENV=production && ./bin/rails db:create RAILS_ENV=production && ./bin/rails db:migrate RAILS_ENV=production

RAILS_ENV=production ./bin/rails assets:precompile

puma起動

RAILS_ENV=production ./bin/rails s

まとめ

気が向いたら、細かい改善点を更新していきます。

近況

前職の会社を辞めてから、フリーランス(個人事業主)として働き始めて コードを書く機会が増えてきたので、知見を少しずつブログに書きためようかと思います。 友人の紹介による仕事ですが、色々と新しい経験を積める環境にあって感謝しています。

参考

A Guide for Upgrading Ruby on Rails — Ruby on Rails Guides

rails/5_1_release_notes.md at 39a2e1465e15d1da56ba1f4ed14fd38740bd86d4 · rails/rails · GitHub