2

I’m using Rails 6.1.4.4, Octoshark 0.3.0, and just upgraded to Ruby 3.0.2. Now when starting Rails console, I get

/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>'

I have this configured in 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

Not sure what other changes to the above or other upgrades I would need to make to get my connection pooling configured properly and things loaded correctly.

Edit: Per the suggestion given, I created a file, lib/core_extensions/octoshark/connection_handler.rb, with this

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

and then a file, config/initializers/ext.rb, with this

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

but am still getting the error.

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

That has to do with how keyword arguments are handled in ruby 3.0 and how octoshark is passing its arguments to .establish_connection.
So, I think you could monkey-patch the gem method or work with a fork, where the parameters are explicitely passed to the active-record method:

module Octoshark
  module ConnectionHandler
    def establish_connection(config, **parameters)
      Octoshark::ConnectionPoolsManager.reset_connection_managers!
      super(config, **parameters)
    end
  end
end
Alter Lagos
  • 12,090
  • 1
  • 70
  • 92
  • I'm a little unfamiliar with monkey patching. I wrapped your code in a "module CoreExtensions" and put it in a file with the path "lib/core_extensions/octoshark/connection_handler.rb", but I'm still getting the same error upon loading the console. What else do I need to do? – Dave Jan 17 '22 at 14:58
  • @Dave adding that code to a file in `config/initializers` is the most straightforward way to do it, using `core_extensions` folder is more about patching core ruby libraries. You could check anyway other options to monkey patch [in this link](https://dev.to/ayushn21/applying-monkey-patches-in-rails-1bj1). – Alter Lagos Jan 19 '22 at 01:07
  • I'm a little confused -- you say I shouldn't be using the core_extensions folder but then the link you included in your comment directly references core_extensions with regards to patching core Ruby libraries. – Dave Jan 19 '22 at 16:38
  • `core_extensions` is mostly a naming convention for ruby core changes (which isn't your case, because you're changing a gem) but, at the end of the day, it doesn't relly matter if you put your monkey-patch there or in a folder with another name, it's up to you. – Alter Lagos Jan 19 '22 at 23:56
  • I added the paths and contents of the files I tried to monkey patch it with as an edit. Still getting the error on server/console startup though. – Dave Jan 20 '22 at 17:21
  • Did you try forking the gem and making the changes in the fork? That's the definitive change. – Alter Lagos Jan 26 '22 at 20:25