Slide 1

Slide 1 text

Upgrading Rails at scale

Slide 2

Slide 2 text

gem ‘rails’, ‘5.2.0.rc2’

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

+

Slide 9

Slide 9 text

$ bundle-stats

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

gem 'rails', github: 'rails/rails', branch: 'master'

Slide 12

Slide 12 text

Ran 83.838 tests

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Fix all issues that might happen on boot

Slide 17

Slide 17 text

2868 errors

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Lessons learnt: Stop the bleeding first

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

gem 'rails', github: 'rails/rails', branch: 'master'

Slide 23

Slide 23 text

1. Prepare app for dual-booting 2. Fix issues happening on boot 3. Enable CI

Slide 24

Slide 24 text

Make CI fail only when new broken code is introduced Existing broken tests will be marked and allowed to fail

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

# 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

Slide 29

Slide 29 text

# 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

Slide 30

Slide 30 text

Minitest Reporter

Slide 31

Slide 31 text

ProgressReporter SummaryReporter

Slide 32

Slide 32 text

# Snippet taken from minitest.rb at_exit { exit exit_code || false } exit_code = Minitest.run ARGV

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Side advantage on stopping the bleeding: Getting help from other developers.

Slide 36

Slide 36 text

Workforce ()*+,(- *+-()- )*+*+-+- ()*+,(- *+-()- )*+*+-+-

Slide 37

Slide 37 text

Componentization

Slide 38

Slide 38 text

Componentization Before After

Slide 39

Slide 39 text

Natural way to identify which code belongs to which component.

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

⛵ * * Actually …

Slide 42

Slide 42 text

Enable deprecation logging in Production

Slide 43

Slide 43 text

Rails 5.0 ➡ Rails 5.1 0 new exceptions

Slide 44

Slide 44 text

Rails.configuration.active_support.deprecation = :log

Slide 45

Slide 45 text

Rails.configuration.active_support.deprecation = :notify

Slide 46

Slide 46 text

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”

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

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'

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

The less deprecations, the easier it will be to upgrade

Slide 54

Slide 54 text

You have introduced new deprecations in the codebase

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

— 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

Slide 58

Slide 58 text

Small but quick upgrades

Slide 59

Slide 59 text

Usable for any gem upgrade

Slide 60

Slide 60 text

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