Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Railsアップグレード百景

r7kamura
March 22, 2019

 Railsアップグレード百景

2019年3月22日にRails Developers Meetup 2019で登壇した際の発表資料です。

r7kamura

March 22, 2019
Tweet

More Decks by r7kamura

Other Decks in Technology

Transcript

  1. RailsΞοϓάϨʔυඦܠ
    by r7kamura

    View full-size slide

  2. ࣗݾ঺հ
    • Ryo Nakamura
    • https://github.com/r7kamura
    • https://twitter.com/r7kamura
    • ϑϦʔϥϯεͰRailsͷ࢓ࣄΛ΍͍ͬͯ·͢
    • Rails 1͔Β6·ͰΞοϓάϨʔυͨ͜͠ͱ͋Γ

    View full-size slide

  3. ࠓ೔࿩͢͜ͱ
    ʮRailsͬͯ͜͏͍͏ײ͡ͰΞοϓάϨʔυ͍ͯ͘͠ͱ
    ྑ͍ͷͶʯͱ͍͏ͷ͕෼͔ͬͯ΋Β͑Δͱخ͍͠
    • ࣄલ४උ
    • ΞοϓάϨʔυͷྲྀΕ (Rails 4, 5, 6͕ର৅)
    • ࣄྫू

    View full-size slide

  4. ࠓ೔࿩͞ͳ͍͜ͱ
    • Πϯϑϥͷ࿩ (1୆͚ͩΞοϓάϨʔυ͢ΔɺϦΫΤετΛෳ
    ੡ͯ͠ݕূ؀ڥʹྲྀ͢౳)
    • ૊৫΍ίϛϡχέʔγϣϯͷ࿩ (ۀ຿ҕୗͰ୲౰͢Δ৔߹ͷ͋
    Ε͜ΕɺϨϏϡΞʔׂ౰౳ͷ͋Ε͜Ε)
    • QAख๏ͷ࿩ (ςετ߲໨ͷબఆํ๏ͳͲ)
    • ݩʑ͸ΞοϓάϨʔυͷ͸·ΓͲ͜Ζ100બʹ͢Δ༧ఆͩͬͨ

    View full-size slide

  5. ࣄલ४උ

    View full-size slide

  6. ࡞ۀલʹ΍͓ͬͯ͘ͱྑ͍͜ͱ
    • GitHub Issuesͷϥϕϧͷ੔ཧ ← ୔ࢁPRΛग़͢ͷͰ
    • ςετ΍ݕূ؀ڥͷ੔උ ← Կ౓΋σϓϩΠ͢ΔͷͰ
    • ։ൃ؀ڥͷ੔උ ← Կ౓΋ςετΛಈ͔͢ͷͰ
    • CIͷ੔උ ← Կ౓΋ಈ͔͢͜ͱʹͳΔͷͰ
    • RuboCopͷ੔උ ← ͍Ζ͍Ζॿ͔ΔͷͰ

    View full-size slide

  7. GitHub Issuesͷϥϕϧͷ੔ཧ

    View full-size slide

  8. ςετ΍ݕূ؀ڥͷ੔උ
    • RailsΞοϓάϨʔυ࣌ʹͦ͜ςετͷ͋Γ͕ͨ͞Λ
    ௧ײ͢Δ͜ͱʹͳΔ…
    • Pull Request͝ͱʹݕূ؀ڥ͕༻ҙ͞ΕΔ΍͕ͭ͋Δ
    ͱຊ౰ʹศར
    • μϛʔσʔλ͕༻ҙ͞Ε͍ͯΔݕূ؀ڥͩͱϕετ
    ʢ2ͭҎ্Ϩίʔυ͕͋Δͱࣦഊ͢Δ໰୊ͱ͔͋Δʣ

    View full-size slide

  9. CIͷ੔උ
    • ߴ଎Խ (ྫ: Performance Pricing PlanରԠ)
    • ςετͷछྨͷݟ௚͠ (ྫ: db:migration΋ςετ)
    • ϩάͷݟ௚͠ (ྫ: ςετதͷෆཁͳඪ४ग़ྗ)
    • ςετ݁Ռͷू໿ (ྫ: CircleCIͷςεταϚϦʔରԠ)

    View full-size slide

  10. RuboCopͷ੔උ
    • Bundler/Gemfileʹै͓ͬͯ͘ͱޙͷมߋ࡞ۀָ͕
    • RailsΧςΰϦͷCop΋༗ޮԽ͓ͯ͘͠ͱḿΔ
    • ηΩϡϦςΟ΍ύϑΥʔϚϯεͷ໰୊΋ݟͯ͘ΕΔ
    • ݹ͍APIͷݺͼग़͠Λauto-correctͰ͖Δͷ΋ศར

    View full-size slide

  11. GemfileͰͷఆٛॱংͷ᠘
    • গͳ͘ͱ΋rails͸ઌ಄ʹॻ͍͓͍ͯͨํ͕ྑ͍
    • ͜͏͍͏ίʔυؚ͕·ΕΔGem͕͋ΔͨΊ
    if defined?(Rails)

    end

    View full-size slide

  12. ΞοϓάϨʔυͷྲྀΕ

    View full-size slide

  13. ΞοϓάϨʔυͷྲྀΕ
    1. bundle update
    2. ςετͱमਖ਼
    3. master΁ͷऔΓࠐΈ (ਵ࣌σϓϩΠ)
    4. όʔδϣϯมߋΛσϓϩΠ
    5. ࣄޙॲཧ

    View full-size slide

  14. bundle update
    • جຊతʹ —conservative ΦϓγϣϯΛ෇͚Δ
    • bundle update --conservative actionmailer actionpack
    actionview activemodel activerecord activesupport rails railties
    Έ͍ͨʹ΍Δ
    • ґଘgemΛઌʹΞοϓάϨʔυ͠ͳ͍ͱ͍͚ͳ͍έʔε΋͋Δ
    • devise΍rails-i18nΈ͍ͨʹಉ͡όʔδϣϯͰRails 4 & 5྆ରԠ
    Ͱ͖ͳ͍gem΋͋ΔͷͰ஫ҙ (͜͏͍͏gem͕ຊ౰ʹ໽հ)

    View full-size slide

  15. ςετͱमਖ਼
    1. ςετ͕ਖ਼ৗʹ࣮ߦ͞ΕΔΑ͏ʹ͢Δ
    2. ςετͷࣦഊΛ0݅ʹ͢Δ
    3. ܯࠂͷ਺ΛݮΒ͍ͯ͘͠
    4. masterʹมߋ͕͋Ε͹औΓࠐΜͰ1ʹ໭Δ

    View full-size slide

  16. ςετҎ֎Ͱ͓͢͢Ίͷ֬ೝࣄ߲
    • rails console ͕ਖ਼ৗʹىಈ͢Δ͔?
    • Ruby΍CSSͷϑΝΠϧΛฤूͨ͠ͱ͖൓ө͞ΕΔ͔?
    • Rails.application.eager_load! ͕੒ޭ͢Δ͔?
    • rails db:rollback db:migration ͕੒ޭ͢Δ͔?
    • RAILE_ENV=production rails assets:precompile ͕੒ޭ͢Δ͔?

    View full-size slide

  17. master΁ͷऔΓࠐΈ
    • Ұ౓ʹ୔ࢁมߋ͢Δͱݕূ΋revert΋େมͳͷͰɺ
    όʔδϣϯมߋ࣌ͷࠩ෼͸࠷খԽ͍ͨ͠
    • มߋ͸৽چόʔδϣϯ྆ରԠͳܗͰՃ͑Δ
    • ςετͷमਖ਼ͱฒߦͯ͠masterʹมߋΛೖΕ͍ͯ͘
    (ͷͰ༧Ίcommit͸෼͚͓ͯ͜͏)

    View full-size slide

  18. ࠩ෼Λখ͘͢͞Δ޻෉
    • bundle update ͷ —conservative Φϓγϣϯ
    • rails_kwargs_testing
    • activerecord-compatible_legacy_migration

    View full-size slide

  19. rails_kwargs_testing
    • ςετͰͷϝιουҾ਺͕ get “/”, params: { page: “1” } ʹ
    • RSpecͷϩά͕ܯࠂͰຒΊਚ͘͞Εͯ͠·͏
    • 5.0Ҡߦͱಉ࣌ʹೖΕΔͱɺ࡞ۀதʹmasterଆͰςετ͕ॻ
    ͖׵ΘΔͱconflict͢Δ͠৭ʑେม
    • 4.2Ͱ΋৽ܗࣜͰॻ͚ΔΑ͏ʹͯ͘͠ΕΔͷ͕͜ͷgem

    View full-size slide

  20. Rails/HttpPositionalArguments
    • લड़ͷϝιουҾ਺Λݕࠪ͢ΔCop
    • લճ͸rails5-spec-converterΛ࢖͕ͬͨbuggy…
    • CopͰauto-correct͢Δͷ͕ྑͦ͞͏

    View full-size slide

  21. activerecord-compatible_legacy_migration
    • class Foo < ActiveRecord::Migration[4.2]; end ͷΑ͏ʹόʔδϣ
    ϯΛࢦఆ͠ͳ͍ͱ͍͚ͳ͘ͳͬͨ (5.0Ͱܯࠂɺ5.1Ͱྫ֎)
    • 4.2Ͱ͸ActiveRecord::Migration[4.2]ͱ͸ॻ͚ͳ͍ͷͰɺࣄલʹର
    Ԡ΋Ͱ͖ͣɺόʔδϣϯมߋ࣌ʹ͜ͷରԠ΋ಉ࣌ʹؚ·Εͯ͠·͏
    • 4.2Ͱ΋5.xͰ΋ಈ࡞͢ΔܧঝݩΛఏڙͯ͘͠ΕΔͷ͕͜ͷgem
    • ※·͋Կ΋ରԠͤͣ5.0ʹͯ͠΋db:migration࣌ʹͪΐͬͱܯࠂ͕
    ग़ΔΑ͏ʹͳΔ͚͔ͩͩΒɺͦΜͳʹॏཁͰ͸ͳ͍͔΋

    View full-size slide

  22. Rails 4.2.9ʹͨ͠ΒparamsपΓͰྫ֎͕…
    • get “/search/:query”, to: “items#search”
    • Rails 4.2.9ͩͱURIύε͕ASCII-8BITʹͳΔ
    • Rails 4.2.9ͰյΕͯ4.2.10Ͱ௚ͬͨ໰୊
    • Railsͷύονόʔδϣϯ͸Ұؾʹ࠷৽·Ͱ্͛Α͏

    View full-size slide

  23. Rails 5ʹͨ͠ΒINSERTͰྫ֎͕…
    • `created_on` DATE DEFAULT ‘0’
    • Rails 5͔ΒσϑΥϧτͰMySQLαʔόଆͷSQLϞʔυ
    ΋ߟྀ͢ΔΑ͏ʹͳͬͨ
    • ݁ՌతʹNO_ZERO_DATEͱSTRICT_TRANS_TABLES
    ͕༗ޮԽ͞ΕͯΤϥʔʹ
    • Rails 4ͷ಺͔Βݫ͓ͯ͘͘͠͠ͱ٢

    View full-size slide

  24. Rails 5ʹͨ͠Βىಈ࣌ʹྫ֎͕…
    • ActiveSupport.on_load(:action_controller) { include Foo }
    • APIϞʔυ࢖ͬͯͳ͍ͷʹ ActionController::API ͕ྫ֎Λ
    ग़͢Α͏ʹͳͬͨͧ???
    • ActiveSupport.on_load(:action_controller) ͸
    ActionController::API ͷಡΈࠐΈ࣌ʹ΋ൃՐ͢Δ
    • Rails 4 ࣌୅ʹ͸૝ఆ͍ͯ͠ͳ͔ͬͨ͜ͱ͕ى͖ͨ

    View full-size slide

  25. Rails 5ʹͨ͠Β404͕204ʹ…
    • ςϯϓϨʔτ͕ଘࡏ͠ͳ͍ͱ͖ͷڍಈ͕มΘͬͨ
    • Rails 4ͩͱྫ֎͕ൃੜͯ͠404
    • Rails 5ͩͱ204
    • ͜͜Ͱʮ͍ͭͰʹϦϑΝΫλϦϯάͯ͠͠·͓͏ʯ
    ͱ͔΍Γ࢝ΊΔͱຊ౰ʹྑ͘ͳ͍…

    View full-size slide

  26. Rails 5ʹͨ͠ΒControllerͷςετ͕…
    post “/login”, name: “…”, password: “…”
    post “/articles”, body: “…”
    • Rails 5.0 ʹROW_POST_DATA͕Ҿ͖ܧ͕ΕΔෆ۩߹͕
    • Rails 5.2 Ͱ͸मਖ਼͞Ε͍ͯΔ͕ 5.0 Ͱ͸ͦͷ··
    • ద౰ʹΞϓϦଆͰύον౰͙ͯͯ྇ͷ͕ྑͦ͞͏

    View full-size slide

  27. Rails 5ʹͨ͠ΒparamsपΓͰςετ͕…
    • get “/foo”, bar: nil, baz: Time.current
    • Rails 4 ·Ͱ͸ςετͰ౉ͨ͠paramsͱͯ͠౉ͨ͠ΦϒδΣΫ
    τ͕ControllerͰ௚઀ࢀর͞Ε͍ͯͨ
    • Rails 5͔Β͸ී௨ʹΤϯίʔυ/σίʔυ͞ΕΔ
    • ຊདྷ͋Γಘͳ͍৚݅ͷςετ͕େྔʹଘࡏ͍ͯͨ͠
    • Content-Type΍paramsͷ಺༰Λࡉ͔͘ݟ௚ͯ͠ରԠ͢Δ

    View full-size slide

  28. Rails 5ʹͨ͠ΒparamsपΓͰྫ֎͕…
    • ActionController::Parameters͕HashΛܧঝ͠ͳ͘ͳͬͨ
    (େม͗͢Δ)
    • ҰԠ5.0Ͱ͸ܯࠂ෇͖ͰHash૬౰ͷϝιου͕ݺͼग़ͤΔ
    Α͏ʹߟྀ͞Ε͍ͯΔ
    • ͔͠͠ params.is_a?(Hash) ʹഊ๺
    • ͜ͷܧঝݩมߋͷӨڹ͕Rails 5ԽͰҰ൪ා͍

    View full-size slide

  29. Rails 5.1ʹͨ͠ΒparamsपΓͰςετ͕…
    • get “/”, params: nil ʹͯ͠ΔͱRails 5.1͔Βྫ֎͕ग़Δͧ???
    • υΩϡϝϯτΛݟΔݶΓparams͸nilͰ΋ྑͦ͞͏
    • Ͳ͏΍ΒRails 5.1ʹ͢Δͱ͖ʹ࣮૷ΛϛεͬͨͬΆ͍
    • rails/rails#34737 Λग़ͯ͠mergeࡁΈ
    • Rails 6 ʹ͔͠ೖΒͳ͍ͷͰ 5 Ͱ͸݁ہ nil Λ౉ͤͳ͍

    View full-size slide

  30. Rails 5.1ʹͨ͠ΒARͷthrough͕…
    • Rails 5.1͔Β has_many :b, through: a ͕ޮ͔ͳ͍ͧ???
    • ௐࠪͨ͠ΒҰ࿈ͷincludeͷதͰ͜͏͍͏͜ͱ͕ى͖͍ͯͨ
    X.has_many :a
    X.has_many :b, through: :a
    X.has_many :a # ← ͜͜Ͱthrough: :aͷఆ͕ٛফ͑Δ

    View full-size slide

  31. Rails 6ʹͨ͠Βvalidation͕…
    • Rails 6͔Βhas-and-belongs-to-manyͳதؒϨίʔυͰ
    ఆ͍ٛͯ͠Δvalidation͕ಈ͔ͳ͘ͳͬͨ…?
    • ࠶ݱίʔυॻ͍ͯTwitterͰΰωͯͨΒkamipo͞Μ͕
    rails/rails#35116 Ͱ௚ͯ͘͠Εͨ
    • ͜ͷޙ΋ผ݅Ͱ࠶ݱίʔυॻ͍ͯ2ʙ3ճΰωͯͨΒ
    kamipo͞Μ͕શ෦௚ͯ͘͠Εͨ…

    View full-size slide

  32. Rails 6ʹͨ͠Βbelongs_toͷڍಈ͕…
    • ಉ͡Ϋϥε͔Βಉ͡Ϋϥεʹbelongs_toΛผ໊Ͱఆ
    ͍ٛͯ͠ΔϞσϧͰɺinverse_of͕ڞ༻͞Εͯ͠·͏
    • ࠶ݱՄೳ͕ͩະղܾ
    • rails/rails#35204 ͰIssueΛཱͯͯௐࠪத

    View full-size slide

  33. Tips1: Contributing to Rails͕ศར
    • rails/rails΁ͷIssueͷग़͠ํ
    • Pull Requestͷग़͠ํ
    • ςετͷಈ͔͠ํ
    • ࠷খͷ࠶ݱίʔυΛॻ͘ͱ͖ͷςϯϓϨʔτ

    View full-size slide

  34. Tips2: r7kamuraʹؙ౤͛͢Δબ୒ࢶ
    • ࣮ࡍRailsΞοϓάϨʔυΛ୭͕୲౰͢Δͷ͔ʁ
    • ਓࣄධՁ΍ݸʑਓͷϞνϕʔγϣϯͷ໰୊͕͋Δ
    • ୲౰ऀΛ෇͚ͯۀ຿ҕୗͷਓʹؙ౤͛͢Δͱ͍͏ͷ
    ͸৭ʑߟ͑ΔͱΘΓͱ༗ޮͩͱࢥ͍ͬͯΔ
    • RailsΞοϓάϨʔυͷ࢓ࣄΛืू͍ͯ͠·͢

    View full-size slide

  35. ͓͞Β͍
    • ࡞ۀͱฒߦͯ͠։ൃج൫Λ੔͍͑ͯ͜͏
    • ύονόʔδϣϯ͸Ұؾʹ্͛Α͏
    • όʔδϣϯมߋ࣌ͷࠩ෼Λ࠷খԽ͍ͯ͜͠͏
    • ᠘͸ଟ͍͕ਅ໘໨ʹςετॻ͍ͯͨΒݕ஌Ͱ͖Δ
    • Railsଆ͕͓͔͍͠ͳͱࢥͬͨΒ࠶ݱίʔυΛॻ͜͏

    View full-size slide