2018-10-15 14:42:47 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Database::Count do
|
|
|
|
before do
|
|
|
|
create_list(:project, 3)
|
2018-11-08 19:23:39 +05:30
|
|
|
create(:identity)
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
let(:models) { [Project, Identity] }
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
describe '.approximate_counts' do
|
|
|
|
context 'with MySQL' do
|
|
|
|
context 'when reltuples have not been updated' do
|
|
|
|
it 'counts all models the normal way' do
|
|
|
|
expect(Gitlab::Database).to receive(:postgresql?).and_return(false)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(Project).to receive(:count).and_call_original
|
|
|
|
expect(Identity).to receive(:count).and_call_original
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
context 'with PostgreSQL', :postgresql do
|
|
|
|
describe 'when reltuples have not been updated' do
|
|
|
|
it 'counts all models the normal way' do
|
|
|
|
expect(described_class).to receive(:reltuples_from_recently_updated).with(%w(projects identities)).and_return({})
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(Project).to receive(:count).and_call_original
|
|
|
|
expect(Identity).to receive(:count).and_call_original
|
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
describe 'no permission' do
|
|
|
|
it 'falls back to standard query' do
|
|
|
|
allow(described_class).to receive(:postgresql_estimate_query).and_raise(PG::InsufficientPrivilege)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(Project).to receive(:count).and_call_original
|
|
|
|
expect(Identity).to receive(:count).and_call_original
|
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
describe 'when some reltuples have been updated' do
|
|
|
|
it 'counts projects in the fast way' do
|
|
|
|
expect(described_class).to receive(:reltuples_from_recently_updated).with(%w(projects identities)).and_return({ 'projects' => 3 })
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(Project).not_to receive(:count).and_call_original
|
|
|
|
expect(Identity).to receive(:count).and_call_original
|
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
describe 'when all reltuples have been updated' do
|
|
|
|
before do
|
|
|
|
ActiveRecord::Base.connection.execute('ANALYZE projects')
|
|
|
|
ActiveRecord::Base.connection.execute('ANALYZE identities')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'counts models with the standard way' do
|
|
|
|
expect(Project).not_to receive(:count)
|
|
|
|
expect(Identity).not_to receive(:count)
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2019-01-03 12:48:30 +05:30
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-10-15 14:42:47 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|