mirror of
https://github.com/discourse/discourse.git
synced 2025-09-07 12:02:53 +08:00
Expose replica_postgresql_connection
to ActiveRecord::Base
.
This commit is contained in:
parent
82222e8d18
commit
a509f466a0
2 changed files with 24 additions and 20 deletions
|
@ -131,13 +131,7 @@ module ActiveRecord
|
||||||
if fallback_handler.master_down?
|
if fallback_handler.master_down?
|
||||||
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
|
||||||
fallback_handler.verify_master
|
fallback_handler.verify_master
|
||||||
|
connection = replica_postgresql_connection(config)
|
||||||
connection = postgresql_connection(config.dup.merge(
|
|
||||||
host: config[:replica_host],
|
|
||||||
port: config[:replica_port]
|
|
||||||
))
|
|
||||||
|
|
||||||
verify_replica(connection)
|
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
connection = postgresql_connection(config)
|
connection = postgresql_connection(config)
|
||||||
|
@ -158,10 +152,21 @@ module ActiveRecord
|
||||||
connection
|
connection
|
||||||
end
|
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
|
private
|
||||||
|
|
||||||
def verify_replica(connection)
|
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
|
raise "Replica database server is not in recovery mode." if !value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,11 +6,11 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
let(:replica_port) { 6432 }
|
let(:replica_port) { 6432 }
|
||||||
|
|
||||||
let(:config) do
|
let(:config) do
|
||||||
ActiveRecord::Base.configurations[Rails.env].merge(
|
ActiveRecord::Base.connection_config.merge(
|
||||||
"adapter" => "postgresql_fallback",
|
adapter: "postgresql_fallback",
|
||||||
"replica_host" => replica_host,
|
replica_host: replica_host,
|
||||||
"replica_port" => replica_port
|
replica_port: replica_port
|
||||||
).symbolize_keys!
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:multisite_db) { "database_2" }
|
let(:multisite_db) { "database_2" }
|
||||||
|
@ -27,7 +27,6 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
|
let(:postgresql_fallback_handler) { PostgreSQLFallbackHandler.instance }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
skip("Disable these tests until we figure out what is leaking connections")
|
|
||||||
postgresql_fallback_handler.initialized = true
|
postgresql_fallback_handler.initialized = true
|
||||||
|
|
||||||
['default', multisite_db].each do |db|
|
['default', multisite_db].each do |db|
|
||||||
|
@ -48,6 +47,7 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
|
|
||||||
context 'when master server is down' do
|
context 'when master server is down' do
|
||||||
before do
|
before do
|
||||||
|
skip("Figure out why this test leaks connections")
|
||||||
@replica_connection = mock('replica_connection')
|
@replica_connection = mock('replica_connection')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -70,8 +70,8 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad)
|
ActiveRecord::Base.expects(:postgresql_connection).with(configuration).raises(PG::ConnectionBad)
|
||||||
ActiveRecord::Base.expects(:verify_replica).with(@replica_connection)
|
ActiveRecord::Base.expects(:verify_replica).with(@replica_connection)
|
||||||
|
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(configuration.merge(
|
ActiveRecord::Base.expects(:postgresql_connection).with(
|
||||||
host: replica_host, port: replica_port)
|
configuration.dup.merge(host: replica_host, port: replica_port)
|
||||||
).returns(@replica_connection)
|
).returns(@replica_connection)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -129,10 +129,9 @@ describe ActiveRecord::ConnectionHandling do
|
||||||
it 'should raise the right error' 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).raises(PG::ConnectionBad)
|
||||||
|
|
||||||
ActiveRecord::Base.expects(:postgresql_connection).with(config.dup.merge(
|
ActiveRecord::Base.expects(:postgresql_connection).with(
|
||||||
host: replica_host,
|
config.dup.merge(host: replica_host, port: replica_port)
|
||||||
port: replica_port
|
).raises(PG::ConnectionBad).once
|
||||||
)).raises(PG::ConnectionBad).once
|
|
||||||
|
|
||||||
postgresql_fallback_handler.expects(:verify_master).twice
|
postgresql_fallback_handler.expects(:verify_master).twice
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue