summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/lib/gitlab/database/cte_materialized_shared_examples.rb
blob: df79572387400e14562d8a5b0c35a1322c5f3cef (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
# frozen_string_literal: true

RSpec.shared_examples 'CTE with MATERIALIZED keyword examples' do
  describe 'adding MATERIALIZE to the CTE' do
    let(:options) { {} }

    before do
      # Clear the cached value before the test
      Gitlab::Database::AsWithMaterialized.clear_memoization(:materialized_supported)
    end

    context 'when PG version is <12' do
      it 'does not add MATERIALIZE keyword' do
        allow(ApplicationRecord.database).to receive(:version).and_return('11.1')

        expect(query).to include(expected_query_block_without_materialized)
      end
    end

    context 'when PG version is >=12' do
      it 'adds MATERIALIZE keyword' do
        allow(ApplicationRecord.database).to receive(:version).and_return('12.1')

        expect(query).to include(expected_query_block_with_materialized)
      end

      context 'when version is higher than 12' do
        it 'adds MATERIALIZE keyword' do
          allow(ApplicationRecord.database).to receive(:version).and_return('15.1')

          expect(query).to include(expected_query_block_with_materialized)
        end
      end

      context 'when materialized is disabled' do
        let(:options) { { materialized: false } }

        it 'does not add MATERIALIZE keyword' do
          expect(query).to include(expected_query_block_without_materialized)
        end
      end
    end
  end
end