summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/count/reltuples_count_strategy_spec.rb
blob: bd3c66d0548fe035b83a74aa562d2a5b395940aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'spec_helper'

describe Gitlab::Database::Count::ReltuplesCountStrategy do
  before do
    create_list(:project, 3)
    create(:identity)
  end

  subject { described_class.new(models).count }

  describe '#count', :postgresql do
    let(:models) { [Project, Identity] }

    context 'when reltuples is up to date' do
      before do
        ActiveRecord::Base.connection.execute('ANALYZE projects')
        ActiveRecord::Base.connection.execute('ANALYZE identities')
      end

      it 'uses statistics to do the count' do
        models.each { |model| expect(model).not_to receive(:count) }

        expect(subject).to eq({ Project => 3, Identity => 1 })
      end
    end

    context 'when models using single-type inheritance are used' do
      let(:models) { [Group, CiService, Namespace] }

      before do
        models.each do |model|
          ActiveRecord::Base.connection.execute("ANALYZE #{model.table_name}")
        end
      end

      it 'returns nil counts for inherited tables' do
        models.each { |model| expect(model).not_to receive(:count) }

        expect(subject).to eq({ Namespace => 3 })
      end
    end

    context 'insufficient permissions' do
      it 'returns an empty hash' do
        allow(ActiveRecord::Base).to receive(:transaction).and_raise(PG::InsufficientPrivilege)

        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)

      expect(described_class.enabled?).to be_falsey
    end
  end
end