Commit 72f0f338 authored by David Kim's avatar David Kim

Merge branch 'fix-connection-exists' into 'master'

Fix Connection#exists? when using the DB LB

See merge request gitlab-org/gitlab!68855
parents 07c6d7a3 abbc8b8f
...@@ -174,8 +174,11 @@ module Gitlab ...@@ -174,8 +174,11 @@ module Gitlab
end end
def exists? def exists?
connection # We can't _just_ check if `connection` raises an error, as it will
# point to a `ConnectionProxy`, and obtaining those doesn't involve any
# database queries. So instead we obtain the database version, which is
# cached after the first call.
connection.schema_cache.database_version
true true
rescue StandardError rescue StandardError
false false
......
...@@ -393,34 +393,28 @@ RSpec.describe Gitlab::Database::Connection do ...@@ -393,34 +393,28 @@ RSpec.describe Gitlab::Database::Connection do
end end
describe '#cached_column_exists?' do describe '#cached_column_exists?' do
it 'only retrieves data once' do it 'only retrieves the data from the schema cache' do
expect(connection.scope.connection) queries = ActiveRecord::QueryRecorder.new do
.to receive(:columns) 2.times do
.once.and_call_original expect(connection.cached_column_exists?(:projects, :id)).to be_truthy
expect(connection.cached_column_exists?(:projects, :bogus_column)).to be_falsey
2.times do end
expect(connection.cached_column_exists?(:projects, :id)).to be_truthy
expect(connection.cached_column_exists?(:projects, :bogus_column)).to be_falsey
end end
expect(queries.count).to eq(0)
end end
end end
describe '#cached_table_exists?' do describe '#cached_table_exists?' do
it 'only retrieves data once per table' do it 'only retrieves the data from the schema cache' do
expect(connection.scope.connection) queries = ActiveRecord::QueryRecorder.new do
.to receive(:data_source_exists?) 2.times do
.with(:projects) expect(connection.cached_table_exists?(:projects)).to be_truthy
.once.and_call_original expect(connection.cached_table_exists?(:bogus_table_name)).to be_falsey
end
expect(connection.scope.connection)
.to receive(:data_source_exists?)
.with(:bogus_table_name)
.once.and_call_original
2.times do
expect(connection.cached_table_exists?(:projects)).to be_truthy
expect(connection.cached_table_exists?(:bogus_table_name)).to be_falsey
end end
expect(queries.count).to eq(0)
end end
it 'returns false when database does not exist' do it 'returns false when database does not exist' do
...@@ -433,16 +427,14 @@ RSpec.describe Gitlab::Database::Connection do ...@@ -433,16 +427,14 @@ RSpec.describe Gitlab::Database::Connection do
end end
describe '#exists?' do describe '#exists?' do
it 'returns true if `ActiveRecord::Base.connection` succeeds' do it 'returns true if the database exists' do
expect(connection.scope).to receive(:connection)
expect(connection.exists?).to be(true) expect(connection.exists?).to be(true)
end end
it 'returns false if `ActiveRecord::Base.connection` fails' do it "returns false if the database doesn't exist" do
expect(connection.scope).to receive(:connection) do expect(connection.scope.connection.schema_cache)
raise ActiveRecord::NoDatabaseError, 'broken' .to receive(:database_version)
end .and_raise(ActiveRecord::NoDatabaseError)
expect(connection.exists?).to be(false) expect(connection.exists?).to be(false)
end end
......
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