DEV: Compatibility with Rails 6.1 (2) (#28)

DEV: Compatibility with Rails 6.1 (2) (#28)

Follow-up to DEV: Compatibility with Rails 6.1 (#27) · discourse/rails_failover@5847f68 · GitHub.

The old commit doesn’t fully fix the compatibility problems; there are a few more spots that need updating to call db_config instead of spec and rails now raises ActiveRecord::ConnectionNotEstablished when it can’t connect to a database so we need to kickoff the failover process when that exception occurs. Integration tests are now updated to use rails 6.1.3.1.

diff --git a/lib/rails_failover/active_record.rb b/lib/rails_failover/active_record.rb
index 34cce8a..0347eaa 100644
--- a/lib/rails_failover/active_record.rb
+++ b/lib/rails_failover/active_record.rb
@@ -27,8 +27,7 @@ module RailsFailover
       @verify_primary_frequency_seconds || 5
     end
 
-    def self.establish_reading_connection(handler, connection_spec)
-      config = connection_spec.config
+    def self.establish_reading_connection(handler, config)
 
       if config[:replica_host] && config[:replica_port]
         replica_config = config.dup
diff --git a/lib/rails_failover/active_record/handler.rb b/lib/rails_failover/active_record/handler.rb
index 8451f62..ea5a9b4 100644
--- a/lib/rails_failover/active_record/handler.rb
+++ b/lib/rails_failover/active_record/handler.rb
@@ -56,13 +56,20 @@ module RailsFailover
 
         primaries_down.keys.each do |handler_key|
           connection_handler = ::ActiveRecord::Base.connection_handlers[handler_key]
-          spec = connection_handler.retrieve_connection_pool(spec_name).spec
-          config = spec.config
+
+          connection_pool = connection_handler.retrieve_connection_pool(spec_name)
+          if connection_pool.respond_to?(:db_config)
+            config = connection_pool.db_config.configuration_hash
+            adapter_method = connection_pool.db_config.adapter_method
+          else
+            config = connection_pool.spec.config
+            adapter_method = connection_pool.spec.adapter_method
+          end
           logger.debug "#{Process.pid} Checking server for '#{handler_key} #{spec_name}'..."
           connection_active = false
 
           begin
-            connection = ::ActiveRecord::Base.public_send(spec.adapter_method, config)
+            connection = ::ActiveRecord::Base.public_send(adapter_method, config)
             connection_active = connection.active?
           rescue => e
             logger.debug "#{Process.pid} Connection to server for '#{handler_key} #{spec_name}' failed with '#{e.message}'"
diff --git a/lib/rails_failover/active_record/middleware.rb b/lib/rails_failover/active_record/middleware.rb
index 4f7bebb..2302066 100644
--- a/lib/rails_failover/active_record/middleware.rb
+++ b/lib/rails_failover/active_record/middleware.rb
@@ -80,7 +80,12 @@ module RailsFailover
           handler = ::ActiveRecord::ConnectionAdapters::ConnectionHandler.new
 
           ::ActiveRecord::Base.connection_handlers[writing_role].connection_pools.each do |pool|
-            ::RailsFailover::ActiveRecord.establish_reading_connection(handler, pool.spec)
+            if pool.respond_to?(:db_config)
+              config = pool.db_config.configuration_hash
+            else
+              config = pool.spec.config
+            end
+            ::RailsFailover::ActiveRecord.establish_reading_connection(handler, config)
           end
 
           handler
diff --git a/lib/rails_failover/active_record/railtie.rb b/lib/rails_failover/active_record/railtie.rb
index 7442ad4..c9f2f73 100644
--- a/lib/rails_failover/active_record/railtie.rb
+++ b/lib/rails_failover/active_record/railtie.rb
@@ -27,9 +27,14 @@ module RailsFailover
               ::ActiveRecord::ConnectionAdapters::ConnectionHandler.new
 
             ::ActiveRecord::Base.connection_handlers[::ActiveRecord::Base.writing_role].connection_pools.each do |connection_pool|
+              if connection_pool.respond_to?(:db_config)
+                config = connection_pool.db_config.configuration_hash
+              else
+                config = connection_pool.spec.config
+              end
               RailsFailover::ActiveRecord.establish_reading_connection(
                 ::ActiveRecord::Base.connection_handlers[::ActiveRecord::Base.reading_role],
-                connection_pool.spec
+                config
               )
             end
 
@@ -37,7 +42,7 @@ module RailsFailover
               ::ActiveRecord::Base.connection
             rescue ::ActiveRecord::NoDatabaseError
               # Do nothing since database hasn't been created
-            rescue ::PG::Error
+            rescue ::PG::Error, ::ActiveRecord::ConnectionNotEstablished
               Handler.instance.verify_primary(::ActiveRecord::Base.writing_role)
               ::ActiveRecord::Base.connection_handler = ::ActiveRecord::Base.lookup_connection_handler(:reading)
             end
diff --git a/spec/support/dummy_app/Gemfile b/spec/support/dummy_app/Gemfile
index 312bc47..e8eb6fe 100644
--- a/spec/support/dummy_app/Gemfile
+++ b/spec/support/dummy_app/Gemfile
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
 git_source(:github) { |repo| "https://github.com/#{repo}.git" }
 
 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
-gem 'rails', '~> 6.0.3', '>= 6.0.3.1'
+gem 'rails', '~> 6.1.3', '>= 6.1.3.1'
 # Use SCSS for stylesheets
 gem 'sass-rails', '>= 6'
 # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
diff --git a/spec/support/dummy_app/Gemfile.lock b/spec/support/dummy_app/Gemfile.lock
index 7956d15..2e7d687 100644
--- a/spec/support/dummy_app/Gemfile.lock
+++ b/spec/support/dummy_app/Gemfile.lock
@@ -1,7 +1,7 @@
 PATH
   remote: ../../..
   specs:
-    rails_failover (0.7.1)
+    rails_failover (0.7.2)
       activerecord (~> 6.0)
       concurrent-ruby
       railties (~> 6.0)
@@ -9,61 +9,65 @@ PATH
 GEM
   remote: https://rubygems.org/
   specs:
-    actioncable (6.0.3.6)
-      actionpack (= 6.0.3.6)
+    actioncable (6.1.3.1)
+      actionpack (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
       nio4r (~> 2.0)
       websocket-driver (>= 0.6.1)
-    actionmailbox (6.0.3.6)
-      actionpack (= 6.0.3.6)
-      activejob (= 6.0.3.6)
-      activerecord (= 6.0.3.6)
-      activestorage (= 6.0.3.6)
-      activesupport (= 6.0.3.6)
+    actionmailbox (6.1.3.1)
+      actionpack (= 6.1.3.1)
+      activejob (= 6.1.3.1)
+      activerecord (= 6.1.3.1)
+      activestorage (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
       mail (>= 2.7.1)
-    actionmailer (6.0.3.6)
-      actionpack (= 6.0.3.6)
-      actionview (= 6.0.3.6)
-      activejob (= 6.0.3.6)
+    actionmailer (6.1.3.1)
+      actionpack (= 6.1.3.1)
+      actionview (= 6.1.3.1)
+      activejob (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
       mail (~> 2.5, >= 2.5.4)
       rails-dom-testing (~> 2.0)
-    actionpack (6.0.3.6)
-      actionview (= 6.0.3.6)
-      activesupport (= 6.0.3.6)
-      rack (~> 2.0, >= 2.0.8)
+    actionpack (6.1.3.1)
+      actionview (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
+      rack (~> 2.0, >= 2.0.9)
       rack-test (>= 0.6.3)
       rails-dom-testing (~> 2.0)
       rails-html-sanitizer (~> 1.0, >= 1.2.0)
-    actiontext (6.0.3.6)
-      actionpack (= 6.0.3.6)
-      activerecord (= 6.0.3.6)
-      activestorage (= 6.0.3.6)
-      activesupport (= 6.0.3.6)
+    actiontext (6.1.3.1)
+      actionpack (= 6.1.3.1)
+      activerecord (= 6.1.3.1)
+      activestorage (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
       nokogiri (>= 1.8.5)
-    actionview (6.0.3.6)
-      activesupport (= 6.0.3.6)
+    actionview (6.1.3.1)
+      activesupport (= 6.1.3.1)
       builder (~> 3.1)
       erubi (~> 1.4)
       rails-dom-testing (~> 2.0)
       rails-html-sanitizer (~> 1.1, >= 1.2.0)
-    activejob (6.0.3.6)
-      activesupport (= 6.0.3.6)
+    activejob (6.1.3.1)
+      activesupport (= 6.1.3.1)
       globalid (>= 0.3.6)
-    activemodel (6.0.3.6)
-      activesupport (= 6.0.3.6)
-    activerecord (6.0.3.6)
-      activemodel (= 6.0.3.6)
-      activesupport (= 6.0.3.6)
-    activestorage (6.0.3.6)
-      actionpack (= 6.0.3.6)
-      activejob (= 6.0.3.6)
-      activerecord (= 6.0.3.6)
+    activemodel (6.1.3.1)
+      activesupport (= 6.1.3.1)
+    activerecord (6.1.3.1)
+      activemodel (= 6.1.3.1)
+      activesupport (= 6.1.3.1)
+    activestorage (6.1.3.1)
+      actionpack (= 6.1.3.1)
+      activejob (= 6.1.3.1)
+      activerecord (= 6.1.3.1)
+      activesupport (= 6.1.3.1)

[... diff too long, it was truncated ...]

GitHub sha: 719a46fca669da8d4df30dd838862077e663be32

This commit appears in #28 which was merged by OsamaSayegh.