Welcome to mirror list, hosted at ThFree Co, Russian Federation.

00_rails_disable_joins_spec.rb « initializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 78e78b6810b14750e660282ae39038e2ca146887 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'DisableJoins' do
  let(:primary_model) do
    Class.new(ApplicationRecord) do
      self.table_name = '_test_primary_records'

      def self.name
        'TestPrimary'
      end
    end
  end

  let(:bridge_model) do
    Class.new(ApplicationRecord) do
      self.table_name = '_test_bridge_records'

      def self.name
        'TestBridge'
      end
    end
  end

  let(:secondary_model) do
    Class.new(ApplicationRecord) do
      self.table_name = '_test_secondary_records'

      def self.name
        'TestSecondary'
      end
    end
  end

  context 'passing disable_joins as an association option' do
    context 'when the association is a bare has_one' do
      it 'disallows the disable_joins option' do
        expect do
          primary_model.has_one :test_bridge, disable_joins: true
        end.to raise_error(ArgumentError, /Unknown key: :disable_joins/)
      end
    end

    context 'when the association is a belongs_to' do
      it 'disallows the disable_joins option' do
        expect do
          bridge_model.belongs_to :test_secondary, disable_joins: true
        end.to raise_error(ArgumentError, /Unknown key: :disable_joins/)
      end
    end

    context 'when the association is has_one :through' do
      it 'allows the disable_joins option' do
        primary_model.has_one :test_bridge
        bridge_model.belongs_to :test_secondary

        expect do
          primary_model.has_one :test_secondary, through: :test_bridge, disable_joins: true
        end.not_to raise_error
      end
    end

    context 'when the association is a bare has_many' do
      it 'disallows the disable_joins option' do
        expect do
          primary_model.has_many :test_bridges, disable_joins: true
        end.to raise_error(ArgumentError, /Unknown key: :disable_joins/)
      end
    end

    context 'when the association is a has_many :through' do
      it 'allows the disable_joins option' do
        primary_model.has_many :test_bridges
        bridge_model.belongs_to :test_secondary

        expect do
          primary_model.has_many :test_secondaries, through: :test_bridges, disable_joins: true
        end.not_to raise_error
      end
    end
  end

  context 'querying has_one :through when disable_joins is set' do
    before do
      create_tables(<<~SQL)
        CREATE TABLE _test_primary_records (
          id serial NOT NULL PRIMARY KEY);

        CREATE TABLE _test_bridge_records (
          id serial NOT NULL PRIMARY KEY,
          primary_record_id int NOT NULL,
          secondary_record_id int NOT NULL);

        CREATE TABLE _test_secondary_records (
          id serial NOT NULL PRIMARY KEY);
      SQL

      primary_model.has_one :test_bridge, anonymous_class: bridge_model, foreign_key: :primary_record_id
      bridge_model.belongs_to :test_secondary, anonymous_class: secondary_model, foreign_key: :secondary_record_id
      primary_model.has_one :test_secondary, through: :test_bridge, anonymous_class: secondary_model,
        disable_joins: -> { joins_disabled_flag }

      primary_record = primary_model.create!
      secondary_record = secondary_model.create!
      bridge_model.create!(primary_record_id: primary_record.id, secondary_record_id: secondary_record.id)
    end

    context 'when disable_joins evaluates to true' do
      let(:joins_disabled_flag) { true }

      it 'executes separate queries' do
        primary_record = primary_model.first

        query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondary }.count

        expect(query_count).to eq(2)
      end
    end

    context 'when disable_joins evalutes to false' do
      let(:joins_disabled_flag) { false }

      it 'executes a single query' do
        primary_record = primary_model.first

        query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondary }.count

        expect(query_count).to eq(1)
      end
    end
  end

  context 'querying has_many :through when disable_joins is set' do
    before do
      create_tables(<<~SQL)
        CREATE TABLE _test_primary_records (
          id serial NOT NULL PRIMARY KEY);

        CREATE TABLE _test_bridge_records (
          id serial NOT NULL PRIMARY KEY,
          primary_record_id int NOT NULL);

        CREATE TABLE _test_secondary_records (
          id serial NOT NULL PRIMARY KEY,
          bridge_record_id int NOT NULL);
      SQL

      primary_model.has_many :test_bridges, anonymous_class: bridge_model, foreign_key: :primary_record_id
      bridge_model.has_many :test_secondaries, anonymous_class: secondary_model, foreign_key: :bridge_record_id
      primary_model.has_many :test_secondaries, through: :test_bridges, anonymous_class: secondary_model,
        disable_joins: -> { disabled_join_flag }

      primary_record = primary_model.create!
      bridge_record = bridge_model.create!(primary_record_id: primary_record.id)
      secondary_model.create!(bridge_record_id: bridge_record.id)
    end

    context 'when disable_joins evaluates to true' do
      let(:disabled_join_flag) { true }

      it 'executes separate queries' do
        primary_record = primary_model.first

        query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondaries.first }.count

        expect(query_count).to eq(2)
      end
    end

    context 'when disable_joins evalutes to false' do
      let(:disabled_join_flag) { false }

      it 'executes a single query' do
        primary_record = primary_model.first

        query_count = ActiveRecord::QueryRecorder.new { primary_record.test_secondaries.first }.count

        expect(query_count).to eq(1)
      end
    end
  end

  context 'querying STI relationships' do
    let(:child_bridge_model) do
      Class.new(bridge_model) do
        def self.name
          'ChildBridge'
        end
      end
    end

    let(:child_secondary_model) do
      Class.new(secondary_model) do
        def self.name
          'ChildSecondary'
        end
      end
    end

    before do
      create_tables(<<~SQL)
        CREATE TABLE _test_primary_records (
          id serial NOT NULL PRIMARY KEY);

        CREATE TABLE _test_bridge_records (
          id serial NOT NULL PRIMARY KEY,
          primary_record_id int NOT NULL,
          type text);

        CREATE TABLE _test_secondary_records (
          id serial NOT NULL PRIMARY KEY,
          bridge_record_id int NOT NULL,
          type text);
      SQL

      primary_model.has_many :child_bridges, anonymous_class: child_bridge_model, foreign_key: :primary_record_id
      child_bridge_model.has_one :child_secondary, anonymous_class: child_secondary_model, foreign_key: :bridge_record_id
      primary_model.has_many :child_secondaries, through: :child_bridges, anonymous_class: child_secondary_model, disable_joins: true

      primary_record = primary_model.create!
      parent_bridge_record = bridge_model.create!(primary_record_id: primary_record.id)
      child_bridge_record = child_bridge_model.create!(primary_record_id: primary_record.id)

      secondary_model.create!(bridge_record_id: child_bridge_record.id)
      child_secondary_model.create!(bridge_record_id: parent_bridge_record.id)
      child_secondary_model.create!(bridge_record_id: child_bridge_record.id)
    end

    it 'filters correctly by the STI type across multiple queries' do
      primary_record = primary_model.first

      query_recorder = ActiveRecord::QueryRecorder.new do
        expect(primary_record.child_secondaries.count).to eq(1)
      end

      expect(query_recorder.count).to eq(2)
    end
  end

  context 'querying polymorphic relationships' do
    before do
      create_tables(<<~SQL)
        CREATE TABLE _test_primary_records (
          id serial NOT NULL PRIMARY KEY);

        CREATE TABLE _test_bridge_records (
          id serial NOT NULL PRIMARY KEY,
          primaryable_id int NOT NULL,
          primaryable_type text NOT NULL);

        CREATE TABLE _test_secondary_records (
          id serial NOT NULL PRIMARY KEY,
          bridgeable_id int NOT NULL,
          bridgeable_type text NOT NULL);
      SQL

      primary_model.has_many :test_bridges, anonymous_class: bridge_model, foreign_key: :primaryable_id, as: :primaryable
      bridge_model.has_one :test_secondaries, anonymous_class: secondary_model, foreign_key: :bridgeable_id, as: :bridgeable
      primary_model.has_many :test_secondaries, through: :test_bridges, anonymous_class: secondary_model, disable_joins: true

      primary_record = primary_model.create!
      primary_bridge_record = bridge_model.create!(primaryable_id: primary_record.id, primaryable_type: 'TestPrimary')
      nonprimary_bridge_record = bridge_model.create!(primaryable_id: primary_record.id, primaryable_type: 'NonPrimary')

      secondary_model.create!(bridgeable_id: primary_bridge_record.id, bridgeable_type: 'TestBridge')
      secondary_model.create!(bridgeable_id: nonprimary_bridge_record.id, bridgeable_type: 'TestBridge')
      secondary_model.create!(bridgeable_id: primary_bridge_record.id, bridgeable_type: 'NonBridge')
    end

    it 'filters correctly by the polymorphic type across multiple queries' do
      primary_record = primary_model.first

      query_recorder = ActiveRecord::QueryRecorder.new do
        expect(primary_record.test_secondaries.count).to eq(1)
      end

      expect(query_recorder.count).to eq(2)
    end
  end

  def create_tables(table_sql)
    ApplicationRecord.connection.execute(table_sql)

    bridge_model.reset_column_information
    secondary_model.reset_column_information
  end
end