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
end
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
rescue StandardError
false
......
......@@ -393,34 +393,28 @@ RSpec.describe Gitlab::Database::Connection do
end
describe '#cached_column_exists?' do
it 'only retrieves data once' do
expect(connection.scope.connection)
.to receive(:columns)
.once.and_call_original
2.times do
expect(connection.cached_column_exists?(:projects, :id)).to be_truthy
expect(connection.cached_column_exists?(:projects, :bogus_column)).to be_falsey
it 'only retrieves the data from the schema cache' do
queries = ActiveRecord::QueryRecorder.new do
2.times do
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
describe '#cached_table_exists?' do
it 'only retrieves data once per table' do
expect(connection.scope.connection)
.to receive(:data_source_exists?)
.with(:projects)
.once.and_call_original
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
it 'only retrieves the data from the schema cache' do
queries = ActiveRecord::QueryRecorder.new do
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
it 'returns false when database does not exist' do
......@@ -433,16 +427,14 @@ RSpec.describe Gitlab::Database::Connection do
end
describe '#exists?' do
it 'returns true if `ActiveRecord::Base.connection` succeeds' do
expect(connection.scope).to receive(:connection)
it 'returns true if the database exists' do
expect(connection.exists?).to be(true)
end
it 'returns false if `ActiveRecord::Base.connection` fails' do
expect(connection.scope).to receive(:connection) do
raise ActiveRecord::NoDatabaseError, 'broken'
end
it "returns false if the database doesn't exist" do
expect(connection.scope.connection.schema_cache)
.to receive(:database_version)
.and_raise(ActiveRecord::NoDatabaseError)
expect(connection.exists?).to be(false)
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