summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/background_migration/batched_migration_job_spec.rb
blob: 7280ca0b58e43f910f8a45a8c688c9aa62277af1 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::BatchedMigrationJob do
  let(:connection) { Gitlab::Database.database_base_models[:main].connection }

  describe '.generic_instance' do
    it 'defines generic instance with only some of the attributes set' do
      generic_instance = described_class.generic_instance(
        batch_table: 'projects', batch_column: 'id',
        job_arguments: %w(x y), connection: connection
      )

      expect(generic_instance.send(:batch_table)).to eq('projects')
      expect(generic_instance.send(:batch_column)).to eq('id')
      expect(generic_instance.instance_variable_get('@job_arguments')).to eq(%w(x y))
      expect(generic_instance.send(:connection)).to eq(connection)

      %i(start_id end_id sub_batch_size pause_ms).each do |attr|
        expect(generic_instance.send(attr)).to eq(0)
      end
    end
  end

  describe '.job_arguments' do
    let(:job_class) do
      Class.new(described_class) do
        job_arguments :value_a, :value_b
      end
    end

    subject(:job_instance) do
      job_class.new(start_id: 1, end_id: 10,
                    batch_table: '_test_table',
                    batch_column: 'id',
                    sub_batch_size: 2,
                    pause_ms: 1000,
                    job_arguments: %w(a b),
                    connection: connection)
    end

    it 'defines methods' do
      expect(job_instance.value_a).to eq('a')
      expect(job_instance.value_b).to eq('b')
      expect(job_class.job_arguments_count).to eq(2)
    end

    context 'when no job arguments are defined' do
      let(:job_class) do
        Class.new(described_class)
      end

      it 'job_arguments_count is 0' do
        expect(job_class.job_arguments_count).to eq(0)
      end
    end
  end

  describe '.operation_name' do
    subject(:perform_job) { job_instance.perform }

    let(:job_instance) do
      job_class.new(start_id: 1, end_id: 10,
                    batch_table: '_test_table',
                    batch_column: 'id',
                    sub_batch_size: 2,
                    pause_ms: 1000,
                    job_arguments: %w(a b),
                    connection: connection)
    end

    let(:job_class) do
      Class.new(described_class) do
        operation_name :update_all
      end
    end

    it 'defines method' do
      expect(job_instance.operation_name).to eq(:update_all)
    end

    context 'when `operation_name` is not defined' do
      let(:job_class) do
        Class.new(described_class) do
          def perform
            each_sub_batch do |sub_batch|
              sub_batch.update_all('to_column = from_column')
            end
          end
        end
      end

      let(:test_table) { table(:_test_table) }
      let(:test_insert_table) { table(:_test_insert_table) }

      before do
        allow(job_instance).to receive(:sleep)

        connection.create_table :_test_table do |t|
          t.timestamps_with_timezone null: false
          t.integer :from_column, null: false
        end

        connection.create_table :_test_insert_table, id: false do |t|
          t.integer :to_column
          t.index :to_column, unique: true
        end

        test_table.create!(id: 1, from_column: 5)
        test_table.create!(id: 2, from_column: 10)
      end

      after do
        connection.drop_table(:_test_table)
        connection.drop_table(:_test_insert_table)
      end

      it 'raises an exception' do
        expect { perform_job }.to raise_error(RuntimeError, /Operation name is required/)
      end
    end
  end

  describe '.scope_to' do
    subject(:job_instance) do
      job_class.new(start_id: 1, end_id: 10,
                    batch_table: '_test_table',
                    batch_column: 'id',
                    sub_batch_size: 2,
                    pause_ms: 1000,
                    job_arguments: %w(a b),
                    connection: connection)
    end

    context 'when additional scoping is defined' do
      let(:job_class) do
        Class.new(described_class) do
          job_arguments :value_a, :value_b
          scope_to ->(r) { "#{r}-#{value_a}-#{value_b}".upcase }
        end
      end

      it 'applies additional scope to the provided relation' do
        expect(job_instance.filter_batch('relation')).to eq('RELATION-A-B')
      end
    end

    context 'when there is no additional scoping defined' do
      let(:job_class) do
        Class.new(described_class) do
        end
      end

      it 'returns provided relation as is' do
        expect(job_instance.filter_batch('relation')).to eq('relation')
      end
    end
  end

  describe '.feature_category' do
    context 'when jobs does not have feature_category attribute set' do
      let(:job_class) { Class.new(described_class) }

      it 'returns :database as default' do
        expect(job_class.feature_category).to eq(:database)
      end
    end

    context 'when jobs have feature_category attribute set' do
      let(:job_class) do
        Class.new(described_class) do
          feature_category :delivery
        end
      end

      it 'returns the provided value' do
        expect(job_class.feature_category).to eq(:delivery)
      end
    end
  end

  describe 'descendants', :eager_load do
    it 'have the same method signature for #perform' do
      expected_arity = described_class.instance_method(:perform).arity
      offences = described_class.descendants.select { |klass| klass.instance_method(:perform).arity != expected_arity }

      expect(offences).to be_empty, "expected no descendants of #{described_class} to accept arguments for " \
        "'#perform', but some do: #{offences.join(", ")}"
    end

    it 'do not use .batching_scope' do
      offences = described_class.descendants.select { |klass| klass.respond_to?(:batching_scope) }

      expect(offences).to be_empty, "expected no descendants of #{described_class} to define '.batching_scope', " \
        "but some do: #{offences.join(", ")}"
    end
  end

  describe '#perform' do
    let(:connection) { Gitlab::Database.database_base_models[:main].connection }

    let(:job_class) { Class.new(described_class) }

    let(:job_instance) do
      job_class.new(start_id: 1, end_id: 10,
                    batch_table: '_test_table',
                    batch_column: 'id',
                    sub_batch_size: 2,
                    pause_ms: 1000,
                    connection: connection)
    end

    subject(:perform_job) { job_instance.perform }

    it 'raises an error if not overridden' do
      expect { perform_job }.to raise_error(NotImplementedError, /must implement perform/)
    end

    context 'when the subclass uses sub-batching' do
      let(:job_class) do
        Class.new(described_class) do
          operation_name :update

          def perform(*job_arguments)
            each_sub_batch(
              batching_arguments: { order_hint: :updated_at },
              batching_scope: -> (relation) { relation.where.not(bar: nil) }
            ) do |sub_batch|
              sub_batch.update_all('to_column = from_column')
            end
          end
        end
      end

      let(:test_table) { table(:_test_table) }

      before do
        allow(job_instance).to receive(:sleep)

        connection.create_table :_test_table do |t|
          t.timestamps_with_timezone null: false
          t.integer :from_column, null: false
          t.text :bar
          t.integer :to_column
        end

        test_table.create!(id: 1, from_column: 5, bar: 'value')
        test_table.create!(id: 2, from_column: 10, bar: 'value')
        test_table.create!(id: 3, from_column: 15)
        test_table.create!(id: 4, from_column: 20, bar: 'value')
      end

      after do
        connection.drop_table(:_test_table)
      end

      it 'calls the operation for each sub-batch' do
        expect { perform_job }.to change { test_table.where(to_column: nil).count }.from(4).to(1)

        expect(test_table.order(:id).pluck(:to_column)).to contain_exactly(5, 10, nil, 20)
      end

      context 'with additional scoping' do
        let(:job_class) do
          Class.new(described_class) do
            scope_to ->(r) { r.where('mod(id, 2) = 0') }
            operation_name :update

            def perform(*job_arguments)
              each_sub_batch(
                batching_arguments: { order_hint: :updated_at },
                batching_scope: -> (relation) { relation.where.not(bar: nil) }
              ) do |sub_batch|
                sub_batch.update_all('to_column = from_column')
              end
            end
          end
        end

        it 'respects #filter_batch' do
          expect { perform_job }.to change { test_table.where(to_column: nil).count }.from(4).to(2)

          expect(test_table.order(:id).pluck(:to_column)).to contain_exactly(nil, 10, nil, 20)
        end
      end

      it 'instruments the batch operation' do
        expect(job_instance.batch_metrics.affected_rows).to be_empty

        expect(job_instance.batch_metrics).to receive(:instrument_operation).with(:update).twice.and_call_original

        perform_job

        expect(job_instance.batch_metrics.affected_rows[:update]).to contain_exactly(2, 1)
      end

      it 'pauses after each sub-batch' do
        expect(job_instance).to receive(:sleep).with(1.0).twice

        perform_job
      end

      context 'when batching_arguments are given' do
        it 'forwards them for batching' do
          expect(job_instance).to receive(:base_relation).and_return(test_table)

          expect(test_table).to receive(:each_batch).with(column: 'id', of: 2, order_hint: :updated_at)

          perform_job
        end
      end
    end

    context 'when the subclass uses distinct each batch' do
      let(:job_instance) do
        job_class.new(start_id: 1,
                      end_id: 100,
                      batch_table: '_test_table',
                      batch_column: 'from_column',
                      sub_batch_size: 2,
                      pause_ms: 10,
                      connection: connection)
      end

      let(:job_class) do
        Class.new(described_class) do
          operation_name :insert

          def perform(*job_arguments)
            distinct_each_batch do |sub_batch|
              sub_batch.pluck(:from_column).each do |value|
                connection.execute("INSERT INTO _test_insert_table VALUES (#{value})")
              end

              sub_batch.size
            end
          end
        end
      end

      let(:test_table) { table(:_test_table) }
      let(:test_insert_table) { table(:_test_insert_table) }

      before do
        allow(job_instance).to receive(:sleep)

        connection.create_table :_test_table do |t|
          t.timestamps_with_timezone null: false
          t.integer :from_column, null: false
        end

        connection.create_table :_test_insert_table, id: false do |t|
          t.integer :to_column
          t.index :to_column, unique: true
        end

        test_table.create!(id: 1, from_column: 5)
        test_table.create!(id: 2, from_column: 10)
        test_table.create!(id: 3, from_column: 10)
        test_table.create!(id: 4, from_column: 5)
        test_table.create!(id: 5, from_column: 15)
      end

      after do
        connection.drop_table(:_test_table)
        connection.drop_table(:_test_insert_table)
      end

      it 'calls the operation for each distinct batch' do
        expect { perform_job }.to change { test_insert_table.pluck(:to_column) }.from([]).to([5, 10, 15])
      end

      it 'stores the affected rows' do
        perform_job

        expect(job_instance.batch_metrics.affected_rows[:insert]).to contain_exactly(2, 1)
      end

      context 'when used in combination with scope_to' do
        let(:job_class) do
          Class.new(described_class) do
            scope_to ->(r) { r.where.not(from_column: 10) }
            operation_name :insert

            def perform(*job_arguments)
              distinct_each_batch do |sub_batch|
              end
            end
          end
        end

        it 'raises an error' do
          expect { perform_job }.to raise_error RuntimeError,
            /distinct_each_batch can not be used when additional filters are defined with scope_to/
        end
      end
    end
  end
end