Allow UsageData.count to use count_by:

This commit is contained in:
Ash McKenzie 2019-07-16 14:20:52 +10:00 committed by Ash McKenzie
parent 9246fd5707
commit d00d60a66d
No known key found for this signature in database
GPG Key ID: D6097705361D6DC9
2 changed files with 8 additions and 2 deletions

View File

@ -176,8 +176,8 @@ module Gitlab
{} # augmented in EE
end
def count(relation, fallback: -1)
relation.count
def count(relation, count_by: nil, fallback: -1)
count_by ? relation.count(count_by) : relation.count
rescue ActiveRecord::StatementInvalid
fallback
end

View File

@ -234,6 +234,12 @@ describe Gitlab::UsageData do
expect(described_class.count(relation)).to eq(1)
end
it 'returns the count for count_by when provided' do
allow(relation).to receive(:count).with(:creator_id).and_return(2)
expect(described_class.count(relation, count_by: :creator_id)).to eq(2)
end
it 'returns the fallback value when counting fails' do
allow(relation).to receive(:count).and_raise(ActiveRecord::StatementInvalid.new(''))