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

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::RenameTaskSystemNoteToChecklistItem do
  let(:notes) { table(:notes) }
  let(:projects) { table(:projects) }

  let(:namespace) { table(:namespaces).create!(name: 'batchtest1', type: 'Group', path: 'space1') }
  let(:project) { table(:projects).create!(name: 'proj1', path: 'proj1', namespace_id: namespace.id) }
  let(:issue) { table(:issues).create!(title: 'Test issue') }

  let!(:note1) do
    notes.create!(
      note: 'marked the task **Task 1** as complete', noteable_type: 'Issue', noteable_id: issue.id, system: true
    )
  end

  let!(:note2) do
    notes.create!(
      note: 'NO_MATCH marked the task **Task 2** as complete',
      noteable_type: 'Issue',
      noteable_id: issue.id,
      system: true
    )
  end

  let!(:note3) do
    notes.create!(
      note: 'marked the task **Task 3** as incomplete',
      noteable_type: 'Issue',
      noteable_id: issue.id,
      system: true
    )
  end

  let!(:note4) do
    notes.create!(
      note: 'marked the task **Task 4** as incomplete',
      noteable_type: 'Issue',
      noteable_id: issue.id,
      system: true
    )
  end

  let!(:metadata1) { table(:system_note_metadata).create!(note_id: note1.id, action: :task) }
  let!(:metadata2) { table(:system_note_metadata).create!(note_id: note2.id, action: :task) }
  let!(:metadata3) { table(:system_note_metadata).create!(note_id: note3.id, action: :task) }
  let!(:metadata4) { table(:system_note_metadata).create!(note_id: note4.id, action: :not_task) }

  let(:migration) do
    described_class.new(
      start_id: metadata1.id,
      end_id: metadata4.id,
      batch_table: :system_note_metadata,
      batch_column: :id,
      sub_batch_size: 2,
      pause_ms: 2,
      connection: ApplicationRecord.connection
    )
  end

  subject(:perform_migration) { migration.perform }

  it 'renames task to checklist item in task system notes that match', :aggregate_failures do
    expect do
      perform_migration

      note1.reload
      note2.reload
      note3.reload
      note4.reload
    end.to change(note1, :note).to('marked the checklist item **Task 1** as complete').and(
      not_change(note2, :note).from('NO_MATCH marked the task **Task 2** as complete')
    ).and(
      change(note3, :note).to('marked the checklist item **Task 3** as incomplete')
    ).and(
      not_change(note4, :note).from('marked the task **Task 4** as incomplete')
    )
  end

  it 'updates in batches' do
    expect { perform_migration }.to make_queries_matching(/UPDATE notes/, 2)
  end

  it 'tracks timings of queries' do
    expect(migration.batch_metrics.timings).to be_empty

    expect { perform_migration }.to change { migration.batch_metrics.timings }
  end
end