summaryrefslogtreecommitdiff
path: root/spec/migrations/replace_work_item_type_backfill_next_batch_strategy_spec.rb
blob: 5e22fc069736201e2dfae3d5d0550b3c09159aa2 (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
# frozen_string_literal: true

require 'spec_helper'
require_migration!

RSpec.describe ReplaceWorkItemTypeBackfillNextBatchStrategy, :migration do
  describe '#up' do
    it 'sets the new strategy for existing migrations' do
      migrations = create_migrations(described_class::OLD_STRATEGY_CLASS, 2)

      expect do
        migrate!

        migrations.each(&:reload)
      end.to change { migrations.pluck(:batch_class_name).uniq }.from([described_class::OLD_STRATEGY_CLASS])
                                                                .to([described_class::NEW_STRATEGY_CLASS])
    end
  end

  describe '#down' do
    it 'sets the old strategy for existing migrations' do
      migrations = create_migrations(described_class::NEW_STRATEGY_CLASS, 2)

      expect do
        migrate!
        schema_migrate_down!

        migrations.each(&:reload)
      end.to change { migrations.pluck(:batch_class_name).uniq }.from([described_class::NEW_STRATEGY_CLASS])
                                                                .to([described_class::OLD_STRATEGY_CLASS])
    end
  end

  def create_migrations(batch_class_name, count)
    Array.new(2) { |index| create_background_migration(batch_class_name, [index]) }
  end

  def create_background_migration(batch_class_name, job_arguments)
    migrations_table = table(:batched_background_migrations)

    migrations_table.create!(
      batch_class_name: batch_class_name,
      job_class_name: described_class::JOB_CLASS_NAME,
      max_value: 10,
      batch_size: 5,
      sub_batch_size: 1,
      interval: 2.minutes,
      table_name: :issues,
      column_name: :id,
      total_tuple_count: 10_000,
      pause_ms: 100,
      job_arguments: job_arguments
    )
  end
end