2

我正在使用 Rails 6.1.4.4、Octoshark 0.3.0,并且刚刚升级到 Ruby 3.0.2。现在启动 Rails 控制台时,我得到

/Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1043:in `establish_connection': wrong number of arguments (given 2, expected 1) (ArgumentError)
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/octoshark-0.3.0/lib/octoshark/active_record_extensions.rb:5:in `establish_connection'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_handling.rb:52:in `establish_connection'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:222:in `block (2 levels) in <class:Railtie>'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:217:in `block in <class:Railtie>'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `instance_exec'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `run'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:61:in `block in run_initializers'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `call'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
    from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:60:in `run_initializers'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:391:in `initialize!'
    from /Users/myuser/Documents/workspace/cfs/cfs-web/config/environment.rb:5:in `<top (required)>'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `block in require'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:299:in `load_dependency'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:367:in `require_environment!'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:28:in `require_environment!'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands/console/console_command.rb:101:in `perform'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/base.rb:69:in `perform'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command.rb:48:in `invoke'
    from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands.rb:18:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我在 config/initializers/octoshark.rb 中配置了这个

CONN_MANAGER = Octoshark::ConnectionPoolsManager.new({
  readonly: Rails.application.config.database_configuration['readonly'],
  third_party_readonly: {
    adapter: 'sqlserver',
    username: APP_CONFIG.third_party_read_secondary[Rails.env].username,
    password: APP_CONFIG.third_party_read_secondary[Rails.env].password,
    host: APP_CONFIG.third_party_read_secondary[Rails.env].host,
    database: APP_CONFIG.third_party_read_secondary[Rails.env].database
  }
})

class ActiveRecord::Base
  def self.connection
    CONN_MANAGER.current_or_default_connection
  end
end

不确定我需要对上述或其他升级进行哪些其他更改才能正确配置连接池并正确加载内容。

编辑:根据给出的建议,我用这个创建了一个文件 lib/core_extensions/octoshark/connection_handler.rb

module CoreExtensions
  module Octoshark
    module ConnectionHandler
      def establish_connection(config, **parameters)
        Octoshark::ConnectionPoolsManager.reset_connection_managers!
        super(config, **parameters)
      end
    end
  end
end

然后是一个文件 config/initializers/ext.rb

Dir.glob(Rails.root.join('lib/ext/**/*.rb')).sort.each do |filename|
  require filename
end

但我仍然收到错误。

4

1 回答 1

0

这与ruby​​ 3.0 中如何处理关键字参数以及如何octoshark其参数传递.establish_connection.
因此,我认为您可以对 gem 方法进行猴子修补或使用 fork,其中参数显式传递给 active-record 方法:

module Octoshark
  module ConnectionHandler
    def establish_connection(config, **parameters)
      Octoshark::ConnectionPoolsManager.reset_connection_managers!
      super(config, **parameters)
    end
  end
end
于 2022-01-16T22:53:13.947 回答