Upgrading Rails at scale. RailsConf 2018

Upgrading Rails at scale. RailsConf 2018

Journey of a Rails upgrade in one of the biggest ruby on rails application running in production.


Edouard Chin

April 18, 2018


  1. Upgrading Rails at scale

  2. gem ‘rails’, ‘5.2.0.rc2’

  3. Edouard Chin https://github.com/Edouard-chin @DaroudeDudek

  4. None
  5. None
  6. None
  7. None
  8. +

  9. $ bundle-stats

  10. None
  11. gem 'rails', github: 'rails/rails', branch: 'master'

  12. Ran 83.838 tests

  13. source 'https://rubygems.org' gem 'rails', '~> 5.1.0'

  14. source 'https://rubygems.org' if ENV[‘SHOPIFY_NEXT’] bundler_monkey_patch end if ENV['SHOPIFY_NEXT'] gem 'rails',

    '~> 5.2.0' else gem 'rails', '~> 5.1.0' end
  15. None
  16. Fix all issues that might happen on boot

  17. 2868 errors

  18. None
  19. Lessons learnt: Stop the bleeding first

  20. None
  21. None
  22. gem 'rails', github: 'rails/rails', branch: 'master'

  23. 1. Prepare app for dual-booting 2. Fix issues happening on

    boot 3. Enable CI
  24. Make CI fail only when new broken code is introduced

    Existing broken tests will be marked and allowed to fail
  25. mark_as :failing_on_rails_next test 'example test' do raise StandardError, 'This test

    fails but will be considered as passing' end
  26. mark_as :failing_on_rails_next test 'example test' do raise StandardError, 'This test

    fails but will be considered as passing' end # What's the difference between both test 'example test' do skip('This test is failing lets not run it') if running_on_rails_next? raise StandardError, 'This test fails but will be skipped' end
  27. mark_as :failing_on_rails_next test 'example test' do raise StandardError, 'This test

    fails but will be considered as passing' end desc 'This task is great' task :great_task do # ... end desc 'Another cool task' task :cool_task do # ... end
  28. # frozen_string_literal: true included do class_attribute :metadata, instance_accessor: false self.metadata

    = Hash.new { |hash, key| hash[key] = [] } end tags.each do |tag| next if method_defined?("#{tag}?") define_method "#{tag}?" do self.class.metadata[tag].include?(name.to_sym) end define_singleton_method :method_added do |name| tags.each do |tag| metadatas[tag] << name end singleton_class.send(:remove_method, :method_added) end module MarkingModule extend ActiveSupport::Concern module ClassMethods def mark_as(*tags) end end end
  29. # frozen_string_literal: true class MyTest < MiniTest::Test end mark_as :failing_on_rails_next

    def test_example end mark_as :slow def another_example end include MarkingModule # The `metadata` hash will look like this { failing_on_rails_next: ['test_example'], slow: ['another_example'] } puts MyTest.new('test_example').failing_on_rails_next? # true
  30. Minitest Reporter

  31. ProgressReporter SummaryReporter

  32. # Snippet taken from minitest.rb at_exit { exit exit_code ||

    false } exit_code = Minitest.run ARGV
  33. module Minitest module Reporters class RailsNextReporter < BaseReporter ALLOWED =

    :allowed FIXED = :fixed attr_writer :state def after_test(test) return unless test_will_fail?(test) @state = else ALLOWED end end def test_will_fail?(test) return false unless options[:rails_next] test.respond_to?(:failing_on_rails_next?) && test.failing_on_rails_next? end if test.failures.none? FIXED def record(result) case @state when ALLOWED result.failures.clear end end when FIXED make_ci_fail end end end
  34. None
  35. Side advantage on stopping the bleeding: Getting help from other

  36. Workforce ()*+,(- *+-()- )*+*+-+- ()*+,(- *+-()- )*+*+-+-

  37. Componentization

  38. Componentization Before After

  39. Natural way to identify which code belongs to which component.

  40. None
  41. ⛵ * * Actually …

  42. Enable deprecation logging in Production

  43. Rails 5.0 ➡ Rails 5.1 0 new exceptions

  44. Rails.configuration.active_support.deprecation = :log

  45. Rails.configuration.active_support.deprecation = :notify

  46. class Subscriber < ActiveSupport::Subscriber cattr_accessor(:deprecations) { [] } def deprecation(event)

    message = event.payload[:message] deprecations << message unless exists?(message) end private def exists?(message) message && deprecations.any? { |deprecation| deprecation == message } end end Subscriber.attach_to :rails Notification name: “rails.deprecation”
  47. class Output < ActiveSupport::Subscriber def process_action(*) Subscriber.deprecations.each do |deprecation| Rails.logger.warn

    "[DEPRECATION WARNING] #{deprecation}" end Subscriber.deprecations.clear end end Output.attach_to :action_controller
  48. None
  49. None
  50. source 'https://rubygems.org' if ENV['SHOPIFY_NEXT'] && File.exist?("#{Bundler::SharedHelpers.default_gemfile}_next.lock") module Bundler::SharedHelpers def default_lockfile=(path)

    @default_lockfile = path end def default_lockfile @default_lockfile ||= Pathname.new("#{default_gemfile}.lock") end end Bundler::SharedHelpers.default_lockfile = Pathname.new("#{Bundler::SharedHelpers.default_gemfile}_next.lock") class Bundler::Dsl unless self.method_defined? :to_definition_unpatched alias_method :to_definition_unpatched, :to_definition end def to_definition(bad_lockfile, unlock) to_definition_unpatched(Bundler::SharedHelpers.default_lockfile, unlock) end end end if ENV['SHOPIFY_NEXT'] else gem 'rails', '~> 5.1.0' end gem 'rails', '~> 5.2.0'
  51. None
  52. The less deprecations, the easier it will be to upgrade

  53. You have introduced new deprecations in the codebase

  54. None
  55. None
  56. — test_foo: - DEPRECATION WARNING: This code is deprecated -

    DEPRECATION WARNING: Another deprecation test_bar - DEPRECATION WARNING: This code is deprecated class MyTest < MiniTest::Test def test_foo ActiveSupport::Deprecation.warn(‘This code is deprecated') ActiveSupport::Deprecation.warn(‘Another deprecation’) end def test_bar ActiveSupport::Deprecation.warn(‘This code is deprecated’) end end
  57. Small but quick upgrades

  58. Usable for any gem upgrade

  59. Edouard Chin https://github.com/Edouard-chin @DaroudeDudek Thanks!