Commit 256709fd authored by Yorick Peterse's avatar Yorick Peterse

Fix DB LB errors when escaping input

The Host class uses the pattern `connection.quote(input)` to quote
certain forms of input before passing these to queries. Rails will
unfortunately try to acquire a database connection when running such
code, resulting in errors should the database not be available.

This commit fixes this by simply catching the connection errors that may
be produced.
parent 51125bfb
---
title: Fix DB LB errors when escaping input
merge_request:
author:
type: fixed
...@@ -115,6 +115,8 @@ module Gitlab ...@@ -115,6 +115,8 @@ module Gitlab
SQL SQL
row['diff'].to_i if row.any? row['diff'].to_i if row.any?
rescue *CONNECTION_ERRORS
nil
end end
def primary_write_location def primary_write_location
...@@ -142,6 +144,8 @@ module Gitlab ...@@ -142,6 +144,8 @@ module Gitlab
row = query_and_release(query) row = query_and_release(query)
row['result'] == 't' row['result'] == 't'
rescue *CONNECTION_ERRORS
false
end end
def query_and_release(sql) def query_and_release(sql)
......
...@@ -239,6 +239,14 @@ describe Gitlab::Database::LoadBalancing::Host, :postgresql do ...@@ -239,6 +239,14 @@ describe Gitlab::Database::LoadBalancing::Host, :postgresql do
expect(host.replication_lag_size).to be_nil expect(host.replication_lag_size).to be_nil
end end
it 'returns nil when the database connection fails' do
allow(host)
.to receive(:connection)
.and_raise(ActionView::Template::Error.new('boom', StandardError.new))
expect(host.replication_lag_size).to be_nil
end
end end
describe '#primary_write_location' do describe '#primary_write_location' do
...@@ -268,6 +276,14 @@ describe Gitlab::Database::LoadBalancing::Host, :postgresql do ...@@ -268,6 +276,14 @@ describe Gitlab::Database::LoadBalancing::Host, :postgresql do
expect(host.caught_up?('foo')).to eq(false) expect(host.caught_up?('foo')).to eq(false)
end end
it 'returns false when the connection fails' do
allow(host)
.to receive(:connection)
.and_raise(ActionView::Template::Error.new('boom', StandardError.new))
expect(host.caught_up?('foo')).to eq(false)
end
end end
describe '#query_and_release' do describe '#query_and_release' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment