From a509f466a09cda088e3319130a36b4f70fcc7866 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 23 Nov 2017 09:52:42 +0800 Subject: [PATCH] Expose `replica_postgresql_connection` to `ActiveRecord::Base`. --- .../postgresql_fallback_adapter.rb | 21 ++++++++++------- .../postgresql_fallback_adapter_spec.rb | 23 +++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb index fca51bd2a63..0bdcda39f1e 100644 --- a/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb +++ b/lib/active_record/connection_adapters/postgresql_fallback_adapter.rb @@ -131,13 +131,7 @@ module ActiveRecord if fallback_handler.master_down? Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY) fallback_handler.verify_master - - connection = postgresql_connection(config.dup.merge( - host: config[:replica_host], - port: config[:replica_port] - )) - - verify_replica(connection) + connection = replica_postgresql_connection(config) else begin connection = postgresql_connection(config) @@ -158,10 +152,21 @@ module ActiveRecord connection end + def replica_postgresql_connection(config) + config = config.dup.merge( + host: config[:replica_host], + port: config[:replica_port] + ) + + connection = postgresql_connection(config) + verify_replica(connection) + connection + end + private def verify_replica(connection) - value = connection.raw_connection.exec("SELECT pg_is_in_recovery()").values[0][0] + value = connection.exec_query("SELECT pg_is_in_recovery()").rows[0][0] raise "Replica database server is not in recovery mode." if !value 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 af0720ab691..8673177bc3c 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 @@ -6,11 +6,11 @@ describe ActiveRecord::ConnectionHandling do let(:replica_port) { 6432 } let(:config) do - ActiveRecord::Base.configurations[Rails.env].merge( - "adapter" => "postgresql_fallback", - "replica_host" => replica_host, - "replica_port" => replica_port - ).symbolize_keys! + ActiveRecord::Base.connection_config.merge( + adapter: "postgresql_fallback", + replica_host: replica_host, + replica_port: replica_port + ) end let(:multisite_db) { "database_2" } @@ -27,7 +27,6 @@ describe ActiveRecord::ConnectionHandling do let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance } before do - skip("Disable these tests until we figure out what is leaking connections") postgresql_fallback_handler.initialized = true ['default', multisite_db].each do |db| @@ -48,6 +47,7 @@ describe ActiveRecord::ConnectionHandling do context 'when master server is down' do before do + skip("Figure out why this test leaks connections") @replica_connection = mock('replica_connection') end @@ -70,8 +70,8 @@ describe ActiveRecord::ConnectionHandling do ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad) ActiveRecord::Base.expects(:verify_replica).with(@replica_connection) - ActiveRecord::Base.expects(:postgresql_connection).with(configuration.merge( - host: replica_host, port: replica_port) + ActiveRecord::Base.expects(:postgresql_connection).with( + configuration.dup.merge(host: replica_host, port: replica_port) ).returns(@replica_connection) end @@ -129,10 +129,9 @@ describe ActiveRecord::ConnectionHandling do it 'should raise the right error' do ActiveRecord::Base.expects(:postgresql_connection).with(config).raises(PG::ConnectionBad) - ActiveRecord::Base.expects(:postgresql_connection).with(config.dup.merge( - host: replica_host, - port: replica_port - )).raises(PG::ConnectionBad).once + ActiveRecord::Base.expects(:postgresql_connection).with( + config.dup.merge(host: replica_host, port: replica_port) + ).raises(PG::ConnectionBad).once postgresql_fallback_handler.expects(:verify_master).twice