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

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::BackfillMemberNamespaceForGroupMembers, :migration, schema: 20220314184009 do
  let(:migration) { described_class.new }
  let(:members_table) { table(:members) }
  let(:namespaces_table) { table(:namespaces) }

  let(:table_name) { 'members' }
  let(:batch_column) { :id }
  let(:sub_batch_size) { 100 }
  let(:pause_ms) { 0 }

  subject(:perform_migration) { migration.perform(1, 10, table_name, batch_column, sub_batch_size, pause_ms) }

  before do
    namespaces_table.create!(id: 100, name: 'test1', path: 'test1', type: 'Group')
    namespaces_table.create!(id: 101, name: 'test2', path: 'test2', type: 'Group')
    namespaces_table.create!(id: 102, name: 'test3', path: 'test3', type: 'Group')
    namespaces_table.create!(id: 201, name: 'test4', path: 'test4', type: 'Project')

    members_table.create!(id: 1, source_id: 100, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
    members_table.create!(id: 2, source_id: 101, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
    members_table.create!(id: 3, source_id: 102, source_type: 'Namespace', type: 'GroupMember', member_namespace_id: 102, access_level: 10, notification_level: 3)
    members_table.create!(id: 4, source_id: 103, source_type: 'Project', type: 'ProjectMember', member_namespace_id: nil, access_level: 10, notification_level: 3)
    members_table.create!(id: 5, source_id: 104, source_type: 'Project', type: 'ProjectMember', member_namespace_id: 201, access_level: 10, notification_level: 3)
  end

  it 'backfills `member_namespace_id` for the selected records', :aggregate_failures do
    expect(members_table.where(type: 'GroupMember', member_namespace_id: nil).count).to eq 2
    expect(members_table.where(type: 'ProjectMember', member_namespace_id: nil).count).to eq 1

    queries = ActiveRecord::QueryRecorder.new do
      perform_migration
    end

    expect(queries.count).to eq(3)
    expect(members_table.where(type: 'GroupMember', member_namespace_id: nil).count).to eq 0
    expect(members_table.where(type: 'GroupMember').pluck(:member_namespace_id)).to match_array([100, 101, 102])
    expect(members_table.where(type: 'ProjectMember', member_namespace_id: nil).count).to eq 1
    expect(members_table.where(type: 'ProjectMember').pluck(:member_namespace_id)).to match_array([nil, 201])
  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