2018-05-16 02:06:55 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Database::Count do
|
|
|
|
before do
|
|
|
|
create_list(:project, 3)
|
2018-05-25 17:28:16 -04:00
|
|
|
create(:identity)
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
let(:models) { [Project, Identity] }
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
expect(Project).to receive(:count).and_call_original
|
|
|
|
expect(Identity).to receive(:count).and_call_original
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
describe 'no permission' do
|
|
|
|
it 'falls back to standard query' do
|
2018-10-28 10:50:44 -04:00
|
|
|
allow(ActiveRecord::Base).to receive(:transaction).and_raise(PG::InsufficientPrivilege)
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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 })
|
|
|
|
|
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
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-05-16 02:06:55 -04:00
|
|
|
|
2018-05-25 17:28:16 -04:00
|
|
|
expect(described_class.approximate_counts(models)).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|