From c532d7d1ae4c353f58557374fc6371065e1b7910 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 4 Feb 2016 11:06:02 +0800 Subject: [PATCH] Internally `AR::Base.establish_connection` removes the current connection. --- .../postgresql_fallback_adapter.rb | 1 - .../postgresql_fallback_adapter_spec.rb | 43 ++++++++----------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index a56f187b9..964955510 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -24,7 +24,6 @@ class PostgreSQLFallbackHandler if connection.active? logger.info "#{self.class}: Master server is active. Reconnecting..." - ActiveRecord::Base.remove_connection ActiveRecord::Base.establish_connection(config) Discourse.disable_readonly_mode @master = true diff --git a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb index 05013407d..c693fe469 100644 --- a/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb +++ b/spec/components/active_record/connection_adapters/postgresql_fallback_adapter_spec.rb @@ -27,40 +27,35 @@ describe ActiveRecord::ConnectionHandling do end it 'should failover to a replica server' do - begin - ActiveRecord::Base.expects(:postgresql_connection).with(config).raises(PG::ConnectionBad) - ActiveRecord::Base.expects(:verify_replica).with(@replica_connection) + ActiveRecord::Base.expects(:postgresql_connection).with(config).raises(PG::ConnectionBad) + ActiveRecord::Base.expects(:verify_replica).with(@replica_connection) - ActiveRecord::Base.expects(:postgresql_connection).with(config.merge({ - host: "localhost", port: "6432" - })).returns(@replica_connection) + ActiveRecord::Base.expects(:postgresql_connection).with(config.merge({ + host: "localhost", port: "6432" + })).returns(@replica_connection) - expect { ActiveRecord::Base.postgresql_fallback_connection(config) } - .to raise_error(PG::ConnectionBad) + expect { ActiveRecord::Base.postgresql_fallback_connection(config) } + .to raise_error(PG::ConnectionBad) - expect{ ActiveRecord::Base.postgresql_fallback_connection(config) } - .to change{ Discourse.readonly_mode? }.from(false).to(true) + expect{ ActiveRecord::Base.postgresql_fallback_connection(config) } + .to change{ Discourse.readonly_mode? }.from(false).to(true) - ActiveRecord::Base.unstub(:postgresql_connection) + ActiveRecord::Base.unstub(:postgresql_connection) - current_threads = Thread.list + current_threads = Thread.list - expect{ ActiveRecord::Base.connection_pool.checkout } - .to change{ Thread.list.size }.by(1) + expect{ ActiveRecord::Base.connection_pool.checkout } + .to change{ Thread.list.size }.by(1) - # Wait for the thread to finish execution - threads = (Thread.list - current_threads).each(&:join) + # Wait for the thread to finish execution + threads = (Thread.list - current_threads).each(&:join) - expect(Discourse.readonly_mode?).to eq(false) + expect(Discourse.readonly_mode?).to eq(false) - expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0) + expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0) - expect(ActiveRecord::Base.connection) - .to be_an_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) - ensure - # threads.each { |t| Thread.kill(t) } if threads - ActiveRecord::Base.establish_connection(:test) - end + expect(ActiveRecord::Base.connection) + .to be_an_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) end end