ActiveRecord is not responding `connected?` with true
This is probably broken since the migration from Rails 5 to Rails 6. And the reason is probably due to their introduction to multiple database support.
@smcgivern reported here !31476 (comment 341250130) that Geo::TrackingBase.connected?
is not returning true when it normally should.
That code specifically depends on ::Gitlab::Geo.geo_database_configured?
, and when that is true, it delegates to super
. The problem is that in the end it boils down to activerecord internal implementation which is also returning false.
Here is a way to reproduce the problem without requiring Geo:
Project.connected?
=> false
Project.connection_handler.connected?(Project.connection_specification_name)
=> false
Project.connection_specification_name
=> "primary"
This suggests that there is a bug in Rails 6.x implementation when "falling back" from the multiple database definition to the previous one (like we are using right now).
More documentation here: https://guides.rubyonrails.org/active_record_multiple_databases.html
After some investigation, it seems that ActiveRecord only connects when it first needs to.
Here is a different version of the example above tested in 05/2021
:
Loading production environment (Rails 6.0.3.6)
irb(main):001:0> Project.connected?
=> false
irb(main):002:0> Project.first
=> #<Project id:1 gitlab-instance-4e07e4c4/Monitoring>>
irb(main):003:0> Project.connected?
=> true