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

connection_spec.rb « database « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e0e6039afceb5fb48c92403af981f3b8ee0f11c (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Connection do
  let(:connection) { described_class.new }

  describe '#default_pool_size' do
    before do
      allow(Gitlab::Runtime).to receive(:max_threads).and_return(7)
    end

    it 'returns the max thread size plus a fixed headroom of 10' do
      expect(connection.default_pool_size).to eq(17)
    end

    it 'returns the max thread size plus a DB_POOL_HEADROOM if this env var is present' do
      stub_env('DB_POOL_HEADROOM', '7')

      expect(connection.default_pool_size).to eq(14)
    end
  end

  describe '#config' do
    it 'returns a HashWithIndifferentAccess' do
      expect(connection.config).to be_an_instance_of(HashWithIndifferentAccess)
    end

    it 'returns a default pool size' do
      expect(connection.config).to include(pool: connection.default_pool_size)
    end

    it 'does not cache its results' do
      a = connection.config
      b = connection.config

      expect(a).not_to equal(b)
    end
  end

  describe '#pool_size' do
    context 'when no explicit size is configured' do
      it 'returns the default pool size' do
        expect(connection).to receive(:config).and_return({ pool: nil })

        expect(connection.pool_size).to eq(connection.default_pool_size)
      end
    end

    context 'when an explicit pool size is set' do
      it 'returns the pool size' do
        expect(connection).to receive(:config).and_return({ pool: 4 })

        expect(connection.pool_size).to eq(4)
      end
    end
  end

  describe '#username' do
    context 'when a username is set' do
      it 'returns the username' do
        allow(connection).to receive(:config).and_return(username: 'bob')

        expect(connection.username).to eq('bob')
      end
    end

    context 'when a username is not set' do
      it 'returns the value of the USER environment variable' do
        allow(connection).to receive(:config).and_return(username: nil)
        allow(ENV).to receive(:[]).with('USER').and_return('bob')

        expect(connection.username).to eq('bob')
      end
    end
  end

  describe '#database_name' do
    it 'returns the name of the database' do
      allow(connection).to receive(:config).and_return(database: 'test')

      expect(connection.database_name).to eq('test')
    end
  end

  describe '#adapter_name' do
    it 'returns the database adapter name' do
      allow(connection).to receive(:config).and_return(adapter: 'test')

      expect(connection.adapter_name).to eq('test')
    end
  end

  describe '#human_adapter_name' do
    context 'when the adapter is PostgreSQL' do
      it 'returns PostgreSQL' do
        allow(connection).to receive(:config).and_return(adapter: 'postgresql')

        expect(connection.human_adapter_name).to eq('PostgreSQL')
      end
    end

    context 'when the adapter is not PostgreSQL' do
      it 'returns Unknown' do
        allow(connection).to receive(:config).and_return(adapter: 'kittens')

        expect(connection.human_adapter_name).to eq('Unknown')
      end
    end
  end

  describe '#postgresql?' do
    context 'when using PostgreSQL' do
      it 'returns true' do
        allow(connection).to receive(:adapter_name).and_return('PostgreSQL')

        expect(connection.postgresql?).to eq(true)
      end
    end

    context 'when not using PostgreSQL' do
      it 'returns false' do
        allow(connection).to receive(:adapter_name).and_return('MySQL')

        expect(connection.postgresql?).to eq(false)
      end
    end
  end

  describe '#db_config_with_default_pool_size' do
    it 'returns db_config with our default pool size' do
      allow(connection).to receive(:default_pool_size).and_return(9)

      expect(connection.db_config_with_default_pool_size.pool).to eq(9)
    end

    it 'returns db_config with the correct database name' do
      db_name = connection.scope.connection.pool.db_config.name

      expect(connection.db_config_with_default_pool_size.name).to eq(db_name)
    end
  end

  describe '#disable_prepared_statements' do
    around do |example|
      original_config = ::Gitlab::Database.main.config

      example.run

      connection.scope.establish_connection(original_config)
    end

    it 'disables prepared statements' do
      connection.scope.establish_connection(
        ::Gitlab::Database.main.config.merge(prepared_statements: true)
      )

      expect(connection.scope.connection.prepared_statements).to eq(true)

      connection.disable_prepared_statements

      expect(connection.scope.connection.prepared_statements).to eq(false)
    end

    context 'with dynamic connection pool size' do
      before do
        connection.scope.establish_connection(connection.config.merge(pool: 7))
      end

      it 'retains the set pool size' do
        connection.disable_prepared_statements

        expect(connection.scope.connection.prepared_statements).to eq(false)
        expect(connection.scope.connection.pool.size).to eq(7)
      end
    end
  end

  describe '#db_read_only?' do
    it 'detects a read-only database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => "t" }])

      expect(connection.db_read_only?).to be_truthy
    end

    it 'detects a read-only database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => true }])

      expect(connection.db_read_only?).to be_truthy
    end

    it 'detects a read-write database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => "f" }])

      expect(connection.db_read_only?).to be_falsey
    end

    it 'detects a read-write database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => false }])

      expect(connection.db_read_only?).to be_falsey
    end
  end

  describe '#db_read_write?' do
    it 'detects a read-only database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => "t" }])

      expect(connection.db_read_write?).to eq(false)
    end

    it 'detects a read-only database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => true }])

      expect(connection.db_read_write?).to eq(false)
    end

    it 'detects a read-write database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => "f" }])

      expect(connection.db_read_write?).to eq(true)
    end

    it 'detects a read-write database' do
      allow(connection.scope.connection)
        .to receive(:execute)
        .with('SELECT pg_is_in_recovery()')
        .and_return([{ "pg_is_in_recovery" => false }])

      expect(connection.db_read_write?).to eq(true)
    end
  end

  describe '#version' do
    around do |example|
      connection.instance_variable_set(:@version, nil)
      example.run
      connection.instance_variable_set(:@version, nil)
    end

    context "on postgresql" do
      it "extracts the version number" do
        allow(connection)
          .to receive(:database_version)
          .and_return("PostgreSQL 9.4.4 on x86_64-apple-darwin14.3.0")

        expect(connection.version).to eq '9.4.4'
      end
    end

    it 'memoizes the result' do
      count = ActiveRecord::QueryRecorder
        .new { 2.times { connection.version } }
        .count

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

  describe '#postgresql_minimum_supported_version?' do
    it 'returns false when using PostgreSQL 10' do
      allow(connection).to receive(:version).and_return('10')

      expect(connection.postgresql_minimum_supported_version?).to eq(false)
    end

    it 'returns false when using PostgreSQL 11' do
      allow(connection).to receive(:version).and_return('11')

      expect(connection.postgresql_minimum_supported_version?).to eq(false)
    end

    it 'returns true when using PostgreSQL 12' do
      allow(connection).to receive(:version).and_return('12')

      expect(connection.postgresql_minimum_supported_version?).to eq(true)
    end
  end

  describe '#bulk_insert' do
    before do
      allow(connection).to receive(:connection).and_return(dummy_connection)
      allow(dummy_connection).to receive(:quote_column_name, &:itself)
      allow(dummy_connection).to receive(:quote, &:itself)
      allow(dummy_connection).to receive(:execute)
    end

    let(:dummy_connection) { double(:connection) }

    let(:rows) do
      [
        { a: 1, b: 2, c: 3 },
        { c: 6, a: 4, b: 5 }
      ]
    end

    it 'does nothing with empty rows' do
      expect(dummy_connection).not_to receive(:execute)

      connection.bulk_insert('test', [])
    end

    it 'uses the ordering from the first row' do
      expect(dummy_connection).to receive(:execute) do |sql|
        expect(sql).to include('(1, 2, 3)')
        expect(sql).to include('(4, 5, 6)')
      end

      connection.bulk_insert('test', rows)
    end

    it 'quotes column names' do
      expect(dummy_connection).to receive(:quote_column_name).with(:a)
      expect(dummy_connection).to receive(:quote_column_name).with(:b)
      expect(dummy_connection).to receive(:quote_column_name).with(:c)

      connection.bulk_insert('test', rows)
    end

    it 'quotes values' do
      1.upto(6) do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      connection.bulk_insert('test', rows)
    end

    it 'does not quote values of a column in the disable_quote option' do
      [1, 2, 4, 5].each do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      connection.bulk_insert('test', rows, disable_quote: :c)
    end

    it 'does not quote values of columns in the disable_quote option' do
      [2, 5].each do |i|
        expect(dummy_connection).to receive(:quote).with(i)
      end

      connection.bulk_insert('test', rows, disable_quote: [:a, :c])
    end

    it 'handles non-UTF-8 data' do
      expect { connection.bulk_insert('test', [{ a: "\255" }]) }.not_to raise_error
    end

    context 'when using PostgreSQL' do
      it 'allows the returning of the IDs of the inserted rows' do
        result = double(:result, values: [['10']])

        expect(dummy_connection)
          .to receive(:execute)
          .with(/RETURNING id/)
          .and_return(result)

        ids = connection
          .bulk_insert('test', [{ number: 10 }], return_ids: true)

        expect(ids).to eq([10])
      end

      it 'allows setting the upsert to do nothing' do
        expect(dummy_connection)
          .to receive(:execute)
          .with(/ON CONFLICT DO NOTHING/)

        connection
          .bulk_insert('test', [{ number: 10 }], on_conflict: :do_nothing)
      end
    end
  end

  describe '#cached_column_exists?' do
    it 'only retrieves data once' do
      expect(connection.scope.connection)
        .to receive(:columns)
        .once.and_call_original

      2.times do
        expect(connection.cached_column_exists?(:projects, :id)).to be_truthy
        expect(connection.cached_column_exists?(:projects, :bogus_column)).to be_falsey
      end
    end
  end

  describe '#cached_table_exists?' do
    it 'only retrieves data once per table' do
      expect(connection.scope.connection)
        .to receive(:data_source_exists?)
        .with(:projects)
        .once.and_call_original

      expect(connection.scope.connection)
        .to receive(:data_source_exists?)
        .with(:bogus_table_name)
        .once.and_call_original

      2.times do
        expect(connection.cached_table_exists?(:projects)).to be_truthy
        expect(connection.cached_table_exists?(:bogus_table_name)).to be_falsey
      end
    end

    it 'returns false when database does not exist' do
      expect(connection.scope).to receive(:connection) do
        raise ActiveRecord::NoDatabaseError, 'broken'
      end

      expect(connection.cached_table_exists?(:projects)).to be(false)
    end
  end

  describe '#exists?' do
    it 'returns true if `ActiveRecord::Base.connection` succeeds' do
      expect(connection.scope).to receive(:connection)

      expect(connection.exists?).to be(true)
    end

    it 'returns false if `ActiveRecord::Base.connection` fails' do
      expect(connection.scope).to receive(:connection) do
        raise ActiveRecord::NoDatabaseError, 'broken'
      end

      expect(connection.exists?).to be(false)
    end
  end

  describe '#system_id' do
    it 'returns the PostgreSQL system identifier' do
      expect(connection.system_id).to be_an_instance_of(Integer)
    end
  end

  describe '#get_write_location' do
    it 'returns a string' do
      expect(connection.get_write_location(connection.scope.connection))
        .to be_a(String)
    end

    it 'returns nil if there are no results' do
      expect(connection.get_write_location(double(select_all: []))).to be_nil
    end
  end
end