Skip to content

Commit

Permalink
DEV: Compatibility with Rails 6.1 (2) (#28)
Browse files Browse the repository at this point in the history
Follow-up to 5847f68.

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.
  • Loading branch information
OsamaSayegh committed Apr 15, 2021
1 parent 6c20bb1 commit 719a46f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 73 deletions.
3 changes: 1 addition & 2 deletions lib/rails_failover/active_record.rb
Expand Up @@ -27,8 +27,7 @@ def self.verify_primary_frequency_seconds
@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
Expand Down
13 changes: 10 additions & 3 deletions lib/rails_failover/active_record/handler.rb
Expand Up @@ -56,13 +56,20 @@ def initiate_fallback_to_primary

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}'"
Expand Down
7 changes: 6 additions & 1 deletion lib/rails_failover/active_record/middleware.rb
Expand Up @@ -80,7 +80,12 @@ def ensure_reading_connection_established!(writing_role:, reading_role:)
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
Expand Down
9 changes: 7 additions & 2 deletions lib/rails_failover/active_record/railtie.rb
Expand Up @@ -27,17 +27,22 @@ class Railtie < ::Rails::Railtie
::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

begin
::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
Expand Down
2 changes: 1 addition & 1 deletion spec/support/dummy_app/Gemfile
Expand Up @@ -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
Expand Down
131 changes: 67 additions & 64 deletions spec/support/dummy_app/Gemfile.lock
@@ -1,69 +1,73 @@
PATH
remote: ../../..
specs:
rails_failover (0.7.1)
rails_failover (0.7.2)
activerecord (~> 6.0)
concurrent-ruby
railties (~> 6.0)

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)
marcel (~> 1.0.0)
activesupport (6.0.3.6)
mini_mime (~> 1.0.2)
activesupport (6.1.3.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2, >= 2.2.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
builder (3.2.4)
byebug (11.1.3)
concurrent-ruby (1.1.8)
Expand All @@ -82,7 +86,7 @@ GEM
mini_mime (>= 0.1.1)
marcel (1.0.1)
method_source (1.0.0)
mini_mime (1.1.0)
mini_mime (1.0.3)
mini_portile2 (2.5.0)
minitest (5.14.4)
nio4r (2.5.7)
Expand All @@ -96,32 +100,32 @@ GEM
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.0.3.6)
actioncable (= 6.0.3.6)
actionmailbox (= 6.0.3.6)
actionmailer (= 6.0.3.6)
actionpack (= 6.0.3.6)
actiontext (= 6.0.3.6)
actionview (= 6.0.3.6)
activejob (= 6.0.3.6)
activemodel (= 6.0.3.6)
activerecord (= 6.0.3.6)
activestorage (= 6.0.3.6)
activesupport (= 6.0.3.6)
bundler (>= 1.3.0)
railties (= 6.0.3.6)
rails (6.1.3.1)
actioncable (= 6.1.3.1)
actionmailbox (= 6.1.3.1)
actionmailer (= 6.1.3.1)
actionpack (= 6.1.3.1)
actiontext (= 6.1.3.1)
actionview (= 6.1.3.1)
activejob (= 6.1.3.1)
activemodel (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
bundler (>= 1.15.0)
railties (= 6.1.3.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.0.3.6)
actionpack (= 6.0.3.6)
activesupport (= 6.0.3.6)
railties (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
method_source
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
thor (~> 1.0)
raindrops (0.19.1)
rake (13.0.3)
sass-rails (6.0.0)
Expand All @@ -142,10 +146,9 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (1.1.0)
thread_safe (0.3.6)
tilt (2.0.10)
tzinfo (1.2.9)
thread_safe (~> 0.1)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicorn (6.0.0)
kgio (~> 2.6)
raindrops (~> 0.7)
Expand All @@ -164,11 +167,11 @@ PLATFORMS
DEPENDENCIES
byebug
pg (~> 1.2)
rails (~> 6.0.3, >= 6.0.3.1)
rails (~> 6.1.3, >= 6.1.3.1)
rails_failover!
sass-rails (>= 6)
unicorn
webpacker (~> 4.0)

BUNDLED WITH
2.2.14
2.2.16

0 comments on commit 719a46f

Please sign in to comment.