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-10-28 12:50:04 -04:00
|
|
|
let(:reltuples_strategy) { double('reltuples_strategy', count: {}) }
|
|
|
|
let(:exact_strategy) { double('exact_strategy', count: {}) }
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
before do
|
|
|
|
allow(Gitlab::Database::Count::ReltuplesCountStrategy).to receive(:new).with(models).and_return(reltuples_strategy)
|
|
|
|
end
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
context '.approximate_counts' do
|
|
|
|
context 'selecting strategies' do
|
|
|
|
let(:strategies) { [double('s1', :enabled? => true), double('s2', :enabled? => false)] }
|
2018-05-16 02:06:55 -04:00
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
it 'uses only enabled strategies' do
|
|
|
|
expect(strategies[0]).to receive(:new).and_return(double('strategy1', count: {}))
|
|
|
|
expect(strategies[1]).not_to receive(:new)
|
|
|
|
|
|
|
|
described_class.approximate_counts(models, strategies: strategies)
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
context 'fallbacks' do
|
2018-10-28 13:01:17 -04:00
|
|
|
subject { described_class.approximate_counts(models, strategies: strategies) }
|
|
|
|
|
|
|
|
let(:strategies) do
|
|
|
|
[
|
|
|
|
double('s1', :enabled? => true, new: first_strategy),
|
|
|
|
double('s2', :enabled? => true, new: second_strategy)
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:first_strategy) { double('first strategy', count: {}) }
|
|
|
|
let(:second_strategy) { double('second strategy', count: {}) }
|
|
|
|
|
|
|
|
it 'gets results from first strategy' do
|
|
|
|
expect(strategies[0]).to receive(:new).with(models).and_return(first_strategy)
|
|
|
|
expect(first_strategy).to receive(:count)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'gets more results from second strategy if some counts are missing' do
|
|
|
|
expect(first_strategy).to receive(:count).and_return({ Project => 3 })
|
|
|
|
expect(strategies[1]).to receive(:new).with([Identity]).and_return(second_strategy)
|
|
|
|
expect(second_strategy).to receive(:count).and_return({ Identity => 1 })
|
|
|
|
|
|
|
|
expect(subject).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not get more results as soon as all counts are present' do
|
|
|
|
expect(first_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 })
|
|
|
|
expect(strategies[1]).not_to receive(:new)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2018-10-28 12:50:04 -04:00
|
|
|
end
|
2018-10-28 12:07:05 -04:00
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
context 'with PostgreSQL', :postgresql do
|
2018-05-25 17:28:16 -04:00
|
|
|
describe 'when reltuples have not been updated' do
|
|
|
|
it 'counts all models the normal way' do
|
|
|
|
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
|
2018-10-28 12:07:05 -04:00
|
|
|
expect(reltuples_strategy).to receive(:count).and_return({ Project => 3 })
|
2018-05-25 17:28:16 -04:00
|
|
|
|
|
|
|
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-10-28 12:07:05 -04:00
|
|
|
# TODO: This covers two parts: reltuple strategy itself and the fallback
|
|
|
|
# TODO: Add spec that covers strategy details for reltuple strategy
|
2018-05-25 17:28:16 -04:00
|
|
|
describe 'when all reltuples have been updated' do
|
2018-10-28 12:07:05 -04:00
|
|
|
#before do
|
|
|
|
#ActiveRecord::Base.connection.execute('ANALYZE projects')
|
|
|
|
#ActiveRecord::Base.connection.execute('ANALYZE identities')
|
|
|
|
#end
|
2018-05-25 17:28:16 -04:00
|
|
|
|
|
|
|
it 'counts models with the standard way' do
|
2018-10-28 12:07:05 -04:00
|
|
|
allow(reltuples_strategy).to receive(:count).and_return({ Project => 3, Identity => 1 })
|
2018-05-25 17:28:16 -04:00
|
|
|
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
|
2018-10-28 11:08:37 -04:00
|
|
|
|
|
|
|
describe Gitlab::Database::Count::ExactCountStrategy do
|
|
|
|
subject { described_class.new(models).count }
|
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
describe '#count' do
|
|
|
|
it 'counts all models' do
|
|
|
|
models.each { |model| expect(model).to receive(:count).and_call_original }
|
|
|
|
|
|
|
|
expect(subject).to eq({ Project => 3, Identity => 1 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.enabled?' do
|
|
|
|
it 'is enabled for PostgreSQL' do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(true)
|
|
|
|
|
|
|
|
expect(described_class.enabled?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is enabled for MySQL' do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(false)
|
|
|
|
|
|
|
|
expect(described_class.enabled?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Gitlab::Database::Count::ReltuplesCountStrategy do
|
|
|
|
subject { described_class.new(models).count }
|
|
|
|
|
|
|
|
describe '#count' do
|
|
|
|
context 'when reltuples is not up to date' do
|
|
|
|
it 'returns an empty hash' do
|
|
|
|
models.each { |model| expect(model).not_to receive(:count) }
|
|
|
|
|
|
|
|
expect(subject).to eq({})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.enabled?' do
|
|
|
|
it 'is enabled for PostgreSQL' do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(true)
|
|
|
|
|
|
|
|
expect(described_class.enabled?).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is disabled for MySQL' do
|
|
|
|
allow(Gitlab::Database).to receive(:postgresql?).and_return(false)
|
2018-10-28 11:08:37 -04:00
|
|
|
|
2018-10-28 12:50:04 -04:00
|
|
|
expect(described_class.enabled?).to be_falsey
|
|
|
|
end
|
2018-10-28 11:08:37 -04:00
|
|
|
end
|
|
|
|
end
|
2018-05-16 02:06:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|