Merge pull request #5352 from tgxworld/method_for_replica_postgres_connection

Expose `replica_postgresql_connection` to `ActiveRecord::Base`.
This commit is contained in:
Guo Xiang Tan 2017-11-27 08:38:10 +08:00 committed by GitHub
commit 71942e4f62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 20 deletions

View File

@ -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

View File

@ -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