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

require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210604070207_retry_backfill_traversal_ids.rb')

RSpec.describe RetryBackfillTraversalIds, :migration do
  include ReloadHelpers

  let_it_be(:namespaces_table) { table(:namespaces) }

  context 'when BackfillNamespaceTraversalIdsRoots jobs are pending' do
    before do
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsRoots',
        arguments: [1, 4, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['pending']
      )
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsRoots',
        arguments: [5, 9, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']
      )
    end

    it 'queues pending jobs' do
      migrate!

      expect(BackgroundMigrationWorker.jobs.length).to eq(1)
      expect(BackgroundMigrationWorker.jobs[0]['args']).to eq(['BackfillNamespaceTraversalIdsRoots', [1, 4, 100]])
      expect(BackgroundMigrationWorker.jobs[0]['at']).to be_nil
    end
  end

  context 'when BackfillNamespaceTraversalIdsChildren jobs are pending' do
    before do
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsChildren',
        arguments: [1, 4, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['pending']
      )
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsRoots',
        arguments: [5, 9, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']
      )
    end

    it 'queues pending jobs' do
      migrate!

      expect(BackgroundMigrationWorker.jobs.length).to eq(1)
      expect(BackgroundMigrationWorker.jobs[0]['args']).to eq(['BackfillNamespaceTraversalIdsChildren', [1, 4, 100]])
      expect(BackgroundMigrationWorker.jobs[0]['at']).to be_nil
    end
  end

  context 'when BackfillNamespaceTraversalIdsRoots and BackfillNamespaceTraversalIdsChildren jobs are pending' do
    before do
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsRoots',
        arguments: [1, 4, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['pending']
      )
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsChildren',
        arguments: [5, 9, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['pending']
      )
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsRoots',
        arguments: [11, 14, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']
      )
      table(:background_migration_jobs).create!(
        class_name: 'BackfillNamespaceTraversalIdsChildren',
        arguments: [15, 19, 100],
        status: Gitlab::Database::BackgroundMigrationJob.statuses['succeeded']
      )
    end

    it 'queues pending jobs' do
      freeze_time do
        migrate!

        expect(BackgroundMigrationWorker.jobs.length).to eq(2)
        expect(BackgroundMigrationWorker.jobs[0]['args']).to eq(['BackfillNamespaceTraversalIdsRoots', [1, 4, 100]])
        expect(BackgroundMigrationWorker.jobs[0]['at']).to be_nil
        expect(BackgroundMigrationWorker.jobs[1]['args']).to eq(['BackfillNamespaceTraversalIdsChildren', [5, 9, 100]])
        expect(BackgroundMigrationWorker.jobs[1]['at']).to eq(RetryBackfillTraversalIds::DELAY_INTERVAL.from_now.to_f)
      end
    end
  end
end