将 rails 从 4.2 升级到 5.2 依赖项问题

Upgrade rails from 4.2 to 5.2 dependencies issue

我是第一次进行此升级,我在第一步就遇到了问题:-(

基本上我想升级我的项目的 rails 版本,所以我将我的 Gemfile 更改为 rails 5.2 并尝试 运行:

bundle update rails

如给定 here 但我无法更新包并收到以下错误

Bundler could not find compatible versions for gem "activerecord":
  In Gemfile:
    rails (~> 5.2) was resolved to 5.2.1.rc1, which depends on
      activerecord (= 5.2.1.rc1)

    schema_plus_views was resolved to 0.3.1, which depends on
      activerecord (~> 4.2)

Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    rails (~> 5.2)

    gaffe was resolved to 1.2.0, which depends on
      rails (>= 4.0.0)

    rails_admin was resolved to 1.3.0, which depends on
      rails (< 6, >= 4.0)

    rails_admin_globalize_field was resolved to 0.4.0, which depends on
      rails (>= 4.2)

    sql-logging was resolved to 3.0.10, which depends on
      rails (>= 4.0)

Bundler could not find compatible versions for gem "rake":
  In Gemfile:
    rake (~> 11.2)

    capistrano (~> 3.6) was resolved to 3.11.0, which depends on
      rake (>= 10.0.0)

    derailed_benchmarks was resolved to 1.3.2, which depends on
      rake (< 13, > 10)

我的 Gemfile 是

source 'https://rubygems.org'

gem 'pg', platform: :ruby
gem 'schema_plus_views'
gem 'fix-db-schema-conflicts', github: 'davidlibrera/fix-db-schema-conflicts'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rake', '~> 11.2'
gem 'rails', '~> 4.2'
# gem 'rb-readline'
# Use postgresql as the database for Active Record
# Use SCSS for stylesheets
gem 'sass-rails'
gem "less-rails", platform: :ruby #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails'
gem "typescript-rails"
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem "bootstrap-sass"
gem "font-awesome-rails"
gem 'haml'

# Use jquery as the JavaScript library
gem 'jquery-rails'
gem "bower-rails"
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
# gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'oj'
gem 'jbuilder'
gem 'angular-rails-templates'
gem 'gaffe'
gem 'color-generator'

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1'

# we have keep redis client below version 4 until this issue is solved
# https://github.com/antirez/redis/issues/4272
# and we have to keep redis below 3.3.2 until this issue is solved
# https://github.com/redis/redis-rb/issues/650
gem 'redis', '3.3.5', require: %w(redis redis/connection/hiredis)
gem 'hiredis'
gem 'puma'
gem 'puma_worker_killer'
gem "dalli"
gem 'jbuilder_cache_multi'
gem 'globalize', '~> 5.0'
gem 'rack-attack'
gem 'ya2yaml'
gem 'cells-rails'
gem 'cells-erb'

# gem 'highline'
gem 'kaminari'
gem 'nokogiri'

gem 'zss', git: "https://github.com/ISEngineering/zmq-service-suite-ruby", platform: :ruby
gem 'ffi-rzmq', '2.0.4'

gem 'therubyracer'
gem "paperclip", "~> 4.3"
gem 'bootstrap-wysihtml5-rails'
gem 'rails-i18n', '~> 4.0'

# Excel depdendencies
gem 'xlsxtream', '>= 2'

# Excel legacy export
gem 'axlsx_rails', '0.5.1'
gem 'axlsx', git: 'https://github.com/randym/axlsx.git'
gem 'rubyzip', '>= 1.2.1'
gem 'zip-zip'

gem 'devise', '~> 4.2'
gem 'devise-i18n'
gem 'savon', '~> 2.10'

gem 'party_foul', git: 'https://github.com/alexanderadam/party_foul.git', branch: 'bugfix/132-dont-assume-that-exceptions-have-a-backtrace' #'>= 1.5.5'

gem 'ledermann-rails-settings'
gem 'dentaku'
gem 'delayed_job_active_record'
gem 'cancancan'
gem 'rolify'
gem 'rails_admin'
gem 'enumerize'
gem 'rails_admin_settings'
gem 'rails_admin_globalize_field'
gem 'trailblazer-rails'
gem 'premailer-rails'
gem 'bulk_insert'

# for assets that are referenced by libraries in vendor/assets
gem 'non-stupid-digest-assets'

gem 'actionpack-action_caching'
gem 'dotenv-rails'

group :development, :test do
  gem 'database_cleaner'
  gem 'rspec-rails'
  gem 'pry'
  gem 'pry-nav'
  gem 'awesome_print'
  gem 'mocha'
  gem 'webmock' # for vcr
  gem 'vcr'
  gem 'chromedriver-helper', '>= 2.1.0'
  gem 'selenium-webdriver', '3.9.0'
  gem 'capybara'
end

group :test do
  gem 'factory_bot_rails'
  gem 'db-query-matchers'
  gem 'capybara-screenshot'
  gem 'fakeredis',  require: 'fakeredis/rspec'
end

group :development do
  gem 'i18n-tasks'
  gem 'spring'
  gem 'letter_opener'
  gem 'derailed_benchmarks'
  gem 'sql-logging'
  gem 'rubocop'
  gem 'rubocop-rspec'
  gem 'fasterer'
  gem 'better_errors'
  gem 'binding_of_caller'
  gem 'activerecord-colored_log_subscriber' # this won't be required with rails 5 anymore

  # Use Capistrano for deployment
  gem 'capistrano',  '~> 3.6'
  gem 'capistrano-rbenv', '~> 2.0'
  gem 'capistrano-bundler', '~> 1.1.2'
  gem 'capistrano-rails'
  gem 'capistrano3-puma', github: "seuros/capistrano-puma"
  gem 'capistrano3-delayed-job', '~> 1.0'

end

group :profile do
  gem 'ruby-prof'
end

gem 'sdoc', group: :doc
gem 'sshkit' # is needed to load tasks on server

我该怎么办?

首先,正如 ThorTL67 在评论中指出的那样,逐步更新 Rails 版本是个好主意,而不是一次大跃进。这样,您的依赖问题就会不那么复杂。

通过 运行 bundle version 检查您使用的 Bundler 版本。如果那个版本是旧的,可​​能是某些依赖关系计算不正确,这可能有助于更新 Bundler (gem update bundler)。

然后是您可以采取的从 4.2 更新到 'some higher version' 的步骤。您得到的错误列表显示 gem 依赖项之间的冲突。您可以尝试逐一解决这些冲突。

Bundler could not find compatible versions for gem "activerecord":
  In Gemfile:
    rails (~> 5.2) was resolved to 5.2.1.rc1, which depends on
      activerecord (= 5.2.1.rc1)

    schema_plus_views was resolved to 0.3.1, which depends on
      activerecord (~> 4.2)

这个错误非常简单。它告诉您 gem rails 需要 activerecord v5.2.1,但是 gem schema_plus_views 需要 activerecord >= 4.2.0 但 < 5.0 (参见 RubyGems explanation of constraints). So how to resolve this? If you check the source page for schema_plus_views, you see that there is no compatible version yet for activerecord v5.2.1, and that it is not likely to be there soon: Any plan to update this gem to be compatible with rails 5.2?

所以在这种情况下,您有几个选择。将它从你的 Gemfile 中删除,并从你的代码中删除对这个 gem 的依赖;检查为您提供所需功能的替代 gem;帮助更新 gem 以支持 rails 5.

Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    rails (~> 5.2)

    gaffe was resolved to 1.2.0, which depends on
      rails (>= 4.0.0)

    rails_admin was resolved to 1.3.0, which depends on
      rails (< 6, >= 4.0)

    rails_admin_globalize_field was resolved to 0.4.0, which depends on
      rails (>= 4.2)

    sql-logging was resolved to 3.0.10, which depends on
      rails (>= 4.0)

我实际上不确定这里的问题是什么。这些对 rails gem 的依赖似乎并不相互矛盾,所以我会通过将它们一一注释然后尝试找出 gem 是什么实际问题再次更新。这可能会为您提供更多信息。

Bundler could not find compatible versions for gem "rake":
  In Gemfile:
    rake (~> 11.2)

    capistrano (~> 3.6) was resolved to 3.11.0, which depends on
      rake (>= 10.0.0)

    derailed_benchmarks was resolved to 1.3.2, which depends on
      rake (< 13, > 10)

这也好像应该不是问题。我会尝试将 Gemfile 中的 rake 依赖项从 gem 'rake', '~> 11.2' 更改为 gem 'rake' 并查看依赖项消息会发生什么。

完成所有这些步骤后,您可能会收到不同的 错误消息。你必须以同样的方式一个一个地解决它们。不幸的是,没有灵丹妙药,更新 Rails 可能是一个乏味的过程,尤其是当您的应用程序依赖于很多 gem 时。你必须逐个解决这个问题,通过删除你自己对 gem 的依赖,帮助 gem 社区 maintenance/updates,或者(最坏的情况)分叉 gem 和自己更改代码以使其再次适用于您的应用程序。祝你好运!

为了更新您的依赖项,您可以使用 bummr 等工具。请确保有一个良好的测试套件,如 README 指定的那样。之后,您可以开始修复兼容性问题、弃用等问题。