$30 off During Our Annual Pro Sale. View Details »

Upgrade strategies for Ruby and Rails

Upgrade strategies for Ruby and Rails

Are you running Rails 5 already? How about ruby 2.3.X? I've been upgrading apps since Ruby 1.87 and Rails 2 and it's fun but there are always challenges. In this talk I'll describe the different methods that are available and which have worked better for me in an agile environment. This talk includes code examples, git graphs, venn diagrams, config files and it is suitable for all levels.

Enrique Carlos Mogollan

October 14, 2016
Tweet

More Decks by Enrique Carlos Mogollan

Other Decks in Programming

Transcript

  1. Upgrade strategies
    for Ruby and Rails
    Enrique Mogollán - @mogox
    Software engineer - Salesforce Desk.com

    View Slide

  2. Five years ago…

    View Slide

  3. Make small upgrade
    commits that you
    can test individually

    View Slide

  4. How many of you are running
    Ruby 1.9.x or lower?
    How many are running
    Rails 3.2 or lower?

    View Slide

  5. Complete survey!
    http://bit.ly/ror-versions

    View Slide

  6. Why do we
    upgrade?

    View Slide

  7. Why do we upgrade?
    Security
    • Security vulnerabilities
    • Bug fixes
    • EOL

    View Slide

  8. Why do we upgrade?
    Security New Features
    • Security vulnerabilities
    • Bug fixes
    • EOL
    • Named parameters Ruby 2
    • Generational garbage collector
    • Adequate record
    • Turbolinks 5

    View Slide

  9. Why do we upgrade?
    FEAR JOY
    • Security vulnerabilities
    • Bug fixes
    • EOL
    • Named parameters Ruby 2
    • Generational garbage collector
    • Adequate record
    • Turbolinks 5

    View Slide

  10. Test coverage for critical paths

    View Slide

  11. Tools

    View Slide

  12. Git and Git Workflow
    >  git  checkout  -­‐b  rails_upgrade  
    >  #  Add  some  changes  
    >  git  add  -­‐p  

    >  git  commit  -­‐m  "upgrading"

    >  git  push

    >  #  Open  pull-­‐request


    View Slide

  13. Ruby Version Manager
    • rvm (the classic)
    • chruby (unix-like)
    • rbenv (shims)
    More info:
    http://kgrz.io/2014/02/04/Programmers-guide-to-choosing-ruby-version-manager.html

    View Slide

  14. Bundler

    View Slide

  15. bundler install

    View Slide

  16. bundler install --path=vendor

    View Slide

  17. bundler open

    View Slide

  18. bundler update
    # use carefully

    View Slide

  19. bundler update rails
    # it takes multiple parameters
    bundler update rails active_record nokogiri rack thor mime-types

    View Slide

  20. The Gemfile is useful to manage
    dependencies on specific versions

    View Slide

  21. Upgrade
    Strategies

    View Slide

  22. The point of no return

    View Slide

  23. Rewrite/Extract [Upgrade strategies]
    • Rewrite - feature parity
    • Extraction - services or microservices

    View Slide

  24. Compatibility mode [Upgrade strategies]
    This strategy focuses on the ability of running different versions of Ruby or Rails
    with the same code base using configuration to determine which version is
    running.
    Advantages
    • Teams can keep building features while the upgrade is going in an agile
    environment.
    • Managing the point of no return is done within the code.

    View Slide

  25. Compatibility mode [Upgrade strategies]
    This strategy focuses on the ability of running different versions of Ruby or Rails
    with the same code base using configuration to determine which version is
    running.
    Disadvantages
    • Requires changes in the configuration, and configuration test.
    • When doing integration or manual testing you test all your changes at once.
    • Code duplication

    View Slide

  26. Compatibility mode - Ruby Upgrade [Upgrade strategies]

    View Slide

  27. Extra Steps
    Compatibility mode - Ruby Upgrade [Upgrade strategies]

    View Slide

  28. First commit - Gemfile in compatibility mode
    def  rails4?  
    ENV['RAILS_4']  =~  /true/  
    end  
    if  rails4?  
    gem  'rails',  '4.0.13'  
    gem  'handlebars_assets',  '0.6'  
    else  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.15'  
    end

    View Slide

  29. Focuses on doing small changes
    Advantages
    • Features can be build at the same time
    • Teams can work in multiple fixes in parallel
    • Easy to test in small parts
    Small iterations [Upgrade strategies]

    View Slide

  30. Small iterations [Upgrade strategies]
    Focuses on doing small changes.
    Disadvantages
    • The point of no return can be difficult to estimate, manage.
    • There is the risk of stop working in the upgrade.

    View Slide

  31. Small iterations - Ruby Upgrade [Upgrade strategies]

    View Slide

  32. First commit - Gemfile in Small Iterations
    #  BEFORE  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.15'  
    #  After  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.6'

    View Slide

  33. Upgrading Ruby

    View Slide

  34. Steps for upgrading Ruby - Small Project
    1. Change the Ruby version (Gemfile, .ruby-version)
    2. Bundle install
    3. Run test
    4. Ship it

    View Slide

  35. Steps for
    upgrading Ruby
    Small Project
    1.Change the Ruby version
    2.Bundle install
    3.Run test
    4.Upgrade production environment
    5.Ship it

    View Slide

  36. Steps for
    upgrading Ruby
    Medium
    Project
    1.Create a branch
    2.Change the Ruby version
    3.Bundle install
    4.Run test
    5.Commit changes
    6.Open a Pull Request
    7.Upgrade production environment
    8.Ship it

    View Slide

  37. Steps for
    upgrading Ruby
    Large
    Project
    1. Create a branch
    2. Change the Ruby version
    3. Bundle install
    4. Run test
    5. Fix one set of test or warnings
    6. Commit changes
    7. Open PR
    8. Ship it
    9. Repeat until done
    10. Upgrade production environment

    View Slide

  38. Upgrading Rails

    View Slide

  39. Upgrading Rails (according to the Rails guide)
    1. Write tests and make sure they pass.
    2. Move to the latest patch version after your current version.
    3. Fix tests and deprecated features.
    4. Move to the latest patch version of the next minor version.

    View Slide

  40. Upgrading
    Rails
    1. Upgrade Ruby
    2. Read the rails upgrade guide
    3. Move to the next patch/minor/major version
    4. Get a successful bundle update
    5. Adjust the configuration files and other changes
    suggested by the guide
    6. Run the test suite (and fix test)
    7. Start the app (and fix warnings)
    8. Smoke test the app
    9. Deploy

    View Slide

  41. Successful bundle update
    http://bundler.io/v1.13/man/bundle-update.1.html

    View Slide

  42. Finding the Gem versions compatible
    Gems
    compatible
    Current
    Rails
    Gems
    compatible
    Target
    Rails

    View Slide

  43. Successful bundler install
    Change the gemfile
    >  bundle  update  rails  
    #  error  no  handlebars_assets  compatible  
    Upgrade dependencies to a compatible version
    >  bundle  handlebars_assets  rails  
    Repeat

    View Slide

  44. Successful bundler install
    Change the gemfile
    >  bundle  update  rails  
    #  error  no  handlebars_assets  compatible  
    Upgrade dependencies to a compatible version
    >  bundle  update  handlebars_assets  money  
    sanitize  nokogiri  devise  railties  rails  

    View Slide

  45. Decide on a
    strategy pattern

    View Slide

  46. Bundle update in compatibility mode
    def  rails4?  
    ENV['RAILS_4']  =~  /true/  
    End  
    if  rails4?  
    gem  'rails',  '4.0.13'  
    gem  'handlebars_assets',  '0.6'  
    else  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.15'  
    end

    View Slide

  47. Bundle update in iterations
    #  BEFORE  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.15'  
    #  After  
    gem  'rails',  '3.2.22.5'  
    gem  'handlebars_assets',  '0.6'

    View Slide

  48. Gems
    compatible
    Current
    Rails
    Gems
    compatible
    Target
    Rails
    Incompatible Gems

    View Slide

  49. Upgrading
    Rails
    1. Upgrade Ruby
    2. Upgrade code in Rails App
    3. Test the app
    4. Deploy

    View Slide

  50. Upgrade
    code in
    Rails App
    • Decide on a strategy
    • Work on a branch
    ‣Bundle upgrade, config changes
    ‣Run the test and fix warnings
    ‣Merge back your changes
    (compatibility/small iterations)
    • Repeat

    View Slide

  51. Summary
    ● Find the right tools that work for your project that allow you
    to experiment.
    ● Decide on a strategy on how to upgrade, paying specially
    attention on how to manage the point of no return.
    ● Small iterations always help.

    View Slide

  52. Survey Results
    http://bit.ly/ror-versions

    View Slide

  53. “If Rails is getting better, by upgrading
    we’re also evolving and getting better”
    Jesse Wolgamott - Rails Conf 2013

    View Slide

  54. Change is
    inevitable

    View Slide

  55. @mogox

    View Slide

  56. Links
    Git Graphs: http://gitgraphjs.com/
    Jesse talk: https://www.youtube.com/watch?v=97fpzfRGTcs
    Rails Upgrade Guide: http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html
    Other Rails Upgrade links:
    http://www.justinweiss.com/articles/how-to-upgrade-to-rails-4-dot-2/
    http://railsapps.github.io/updating-rails.html
    http://mensfeld.pl/2013/06/upgrading-to-rails-4-0-from-rails-3-2-test-case-part-i-preparations-
    configuration-gems/

    View Slide

  57. View Slide