From 9540b952fd7bbf2f9bcc3afec8860e2f4859c52c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 15 Sep 2017 16:40:04 +0800 Subject: [PATCH] Fix broken condition in PostgresqlFallbackAdapter. --- .../postgresql_fallback_adapter.rb | 9 +++++++- .../postgresql_fallback_adapter_spec.rb | 21 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index ce2dfbb3e27..e16136bc1e0 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -119,7 +119,14 @@ module ActiveRecord def verify_replica(connection) value = connection.raw_connection.exec("SELECT pg_is_in_recovery()").values[0][0] - raise "Replica database server is not in recovery mode." if value == 'f' + + if !value + begin + raise "Replica database server is not in recovery mode." + ensure + connection.close + end + end end end end 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 607faaab394..c40eaf73dd0 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 @@ -96,8 +96,15 @@ describe ActiveRecord::ConnectionHandling do expect(ActiveRecord::Base.connection_pool.connections.count).to eq(0) - expect(ActiveRecord::Base.connection) - .to be_an_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + ActiveRecord::Base.connection_handler.clear_active_connections! + connection = ActiveRecord::Base.connection + + begin + expect(connection) + .to be_an_instance_of(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) + ensure + connection.close + end end end @@ -115,6 +122,16 @@ describe ActiveRecord::ConnectionHandling do end end + describe '.verify_replica' do + describe 'when database is not in recovery' do + it 'should raise the right error' do + expect do + ActiveRecord::Base.send(:verify_replica, ActiveRecord::Base.connection) + end.to raise_error(RuntimeError, "Replica database server is not in recovery mode.") + end + end + end + def with_multisite_db(dbname) RailsMultisite::ConnectionManagement.expects(:current_db).returns(dbname).at_least_once yield