如何从 rails 4.2.6 升级到 rails 5.0 [英] How to upgrade from rails 4.2.6 to rails 5.0

查看:49
本文介绍了如何从 rails 4.2.6 升级到 rails 5.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种控制台方式可以将我的 rails 升级到新的 5.0 版本?

还是我必须手动去替换升级所需的每个文件?Interne 在给我一个解决这个问题的方法时似乎含糊不清.

解决方案

我几天前刚刚经历过这个,这是我的(成功)过程:

首先,在升级之前,请确保您的应用在 Ruby 2.2+ 上运行.

然后将 Rails 更新到最新的 4.x 版本(现在是 4.2.7),并运行您的测试套件(或完全运行您的应用),观察弃用警告的日志.

如果您发现这些弃用情况,请修复它们.

仔细阅读 5.0 发行说明,并记下可能影响您的应用的事项.

使用 Ready4Rails 检查您的 Gemfile,这应该有助于告诉您哪些 gems可能还没有为 Rails 5 做好准备.[注意:Ready4Rails.net 似乎已于 2020 年 1 月 13 日下架]

您可能需要访问一些 gem 的主存储库,看看是否有可能有支持但尚未完全发布的分支.您可能希望将这些 gem 的 Gemfile 固定到预发布版本或分支(即使您不立即采用,这也可以帮助您在这些 gem 的最终版本发布时运行).

一般来说,它也可能有助于减少您的依赖项.例如,您的 development 块中可能有一堆尚未更新的 gem.没有这些,你可能还能活一段时间.

有些人会建议您删除 Gemfilebundle update 中的版本号.除了非常小的应用程序,我建议不要这样做.一次更改太多东西会导致很难追踪问题.

像这样更改 Gemfile 中列出的 Rails 版本:

gem 'rails', '= 5.0.0'

并运行bundle update rails.

这可能会失败,并为您提供捆绑程序无法解决的版本号比较列表.查找最终要求 rails 小于 5 的任何依赖项,然后查看是否可以更新.

更改该 gem 的版本,撤消对 rails 版本的更改.bundle update 该 gem,再次运行您的套件(或使用该 gem 功能的应用程序部分)并查找弃用.

提交您正在进行的小步骤并根据需要重复,直到您最终将 Rails 固定到 5.0.0.

一旦您解决了这些阻塞依赖项,并且 bundle update rails 成功完成,请提交并再次运行您的测试套件.

如果你的套件和我的一样,这里会有一堆弃用的墙,但通常只是每次测试重复几件事.现在并不是绝对有必要修复这些问题,但我会解决那些嘈杂的问题……它们可能会让您更难看到更重要的问题.我不得不将大量控制器规格从 post :foo, name: 'bar' 更改为 post :foo, params: { name: 'bar' } 以静音命名参数语法弃用警告.

您可能还需要更新一些代码以修复此处的一些失败规范.幸运的是,我没有必要这样做,但是您应该能够将根本原因追溯到 Rails 中的更改或您更新的 gem 之一.

现在,您应该运行 bin/rails rails:update,以更新您的配置文件.仔细区分每一个以寻找变化.我更喜欢从差异输出中复制/粘贴行并手动将它们复制到我的配置中,并在必要时进行调整并保持差异,直到我看到差异中的所有自定义设置为止.

在所有这些完成后,再次运行您的规范,并实际打开应用程序并确保它按预期工作.将其推送到暂存环境以确保它也能在类似生产的环境中运行可能会很有用.

查看由 bin/rails rails:update (config/initializers/new_framework_defaults.rb) 创建的文件,看看您是否可以禁用或注释掉这些文件而不影响您的应用的行为.

这些行为以及其他更改通常记录在升级 Ruby on Rails 指南

希望你现在已经完成了,但你可以带来一些额外的细节.我切换到一个新目录并使用 rails new rails5project 生成一个全新的 Rails 应用程序并复制您的应用程序中可能没有的新文件,例如 app/models/application_record.rbapp/mailers/application_mailer.rb(以及从它们继承的转换模型而不是 ActiveRecord::BaseActionMailer::Base).

此外,app/assets/javascripts/cable.jsapp/assets/javascripts/channelsapp/channels,如果你想使用 ActionCable,如果你想使用 ActiveJob`

app/jobs

然后查看新的 Rails 5 应用程序的 Gemfile,如果需要,可以带上其中列出的任何 gem.例如,Rails5 现在附带 Turbolinks 5,默认情况下为 Puma,listenspring-watcher-listen.

希望,毕竟你有一个工作应用程序,它仍然满足你在 Rails 5 上运行的所有需求.但是,如果你因为 gem 依赖而被阻塞,希望你可以保留这个分支,直到一切准备就绪并合并它!

Is there a console way i can upgrade my rails to the new 5.0 version?

Or do i have to manually go replace each file that's needed to upgrade? The Interne seems vague in giving me a solution to this problem.

解决方案

I just went through this a few days ago, and this was my (successful) process:

First, make sure your app runs on Ruby 2.2+ before upgrading.

Then update Rails to the latest 4.x version (4.2.7 right now), and run your test suite (or fully exercise your app) watching the log for Deprecation warnings.

Fix those deprecations if you find any.

Carefully read the 5.0 release notes and make note of things that might affect your app.

Check your Gemfile with Ready4Rails, which should help tell you what gems you have that may not be ready for Rails 5 yet. [Note: Ready4Rails.net appears to have been taken down as of 1/13/2020]

You'll probably need to visit the home repositories for some of your gems to see if there is a branch that might have support, but isn't quite released yet. You may want to pin your Gemfile for these gems to a pre-release version or fork (even if you don't adopt right away, this can help you get things running for when the final version of those gems are released).

It may also help to reduce your dependencies in general. For instance, you might have a bunch of gems in your development block, that aren't updated yet. You can probably live without these for awhile.

Some people will advise you to remove version numbers in your Gemfile and bundle update. I would recommend against that for anything but very small apps. Changing too many things at once can make it very hard to track down issues.

Change the version of Rails listed in your Gemfile like this:

gem 'rails', '= 5.0.0'

and run bundle update rails.

This will likely fail and give you a list of version number comparisons that bundler couldn't resolve. Look for any dependencies that ultimately require rails to be less than 5, and see if those can be updated.

Change your version of that gem, undo the change to the version for rails. bundle update that gem, run your suite again (or exercise parts of your app that use that gem's features) and look for deprecations.

Commit your small in-progress step and repeat as necessary until you can finally pin Rails to 5.0.0.

Once you get those blocking dependencies resolved, and bundle update rails completes successfully, commit that and run your test suite again.

If your suite was anything like mine, there will be a wall of deprecations here, but it's usually just a few things repeated for every test. It isn't strictly necessary to fix these right now, but I would tackle the noisy ones...they may be making it harder to see more important ones. I had to change a ton of controller specs from post :foo, name: 'bar' to post :foo, params: { name: 'bar' } to silence a named parameter syntax deprecation warning.

You may also have to update some code to fix some failing specs here. Luckily, I didn't have to, but you should be able to trace the root cause back to a change in Rails or one of your gems if you updated any.

Now, you should run bin/rails rails:update, to update your configuration files. Carefully diff each of these to look for changes. I prefer to copy/paste lines from the diff output and copy them manually to my configuration and adjust if necessary and keep diffing until all I see are my custom settings in the diff.

After all that is done, run your specs again, and actually open the app and make sure it works as expected. It may be useful to push this to a staging environment to make sure it behaves in a production-like setting, too.

Review the file created by bin/rails rails:update (config/initializers/new_framework_defaults.rb) and see if you can disable or comment these out without affecting your app's behavior.

The behavior of these, as well as other changes are usually documented in the Upgrading Ruby on Rails Guide

Hopefully you are done now, but there are some extra niceties you can bring over. I switch to a new directory and generate a brand new Rails app with rails new rails5project and copy over new files that you probably don't have in your app like app/models/application_record.rb and app/mailers/application_mailer.rb (and transition models to inherit from them instead of ActiveRecord::Base and ActionMailer::Base).

Also, app/assets/javascripts/cable.js, app/assets/javascripts/channels, and app/channels, if you want to use ActionCable, and app/jobs if you want to use ActiveJob`

Then look at the fresh Rails 5 app's Gemfile, and bring over any gems listed there if you want them. For instance, Rails5 now ships with Turbolinks 5, Puma by default, listen and spring-watcher-listen.

Hopefully, after all this you have a working application that still meets all your needs running on Rails 5. However, if you are blocked because of a gem dependency, hopefully you can keep this branch around until everything is ready and merge it!

这篇关于如何从 rails 4.2.6 升级到 rails 5.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆