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

require 'spec_helper'

RSpec.describe Gitlab::BackgroundMigration::BackfillIntegrationsTypeNew, :migration, schema: 20220212120735 do
  let(:migration) { described_class.new }
  let(:integrations) { table(:integrations) }

  let(:namespaced_integrations) do
    Set.new(%w[
      Asana Assembla Bamboo Bugzilla Buildkite Campfire Confluence CustomIssueTracker Datadog
      Discord DroneCi EmailsOnPush Ewm ExternalWiki Flowdock HangoutsChat Harbor Irker Jenkins Jira Mattermost
      MattermostSlashCommands MicrosoftTeams MockCi MockMonitoring Packagist PipelinesEmail Pivotaltracker
      Prometheus Pushover Redmine Shimo Slack SlackSlashCommands Teamcity UnifyCircuit WebexTeams Youtrack Zentao
      Github GitlabSlackApplication
    ]).freeze
  end

  before do
    integrations.connection.execute 'ALTER TABLE integrations DISABLE TRIGGER "trigger_type_new_on_insert"'

    namespaced_integrations.each_with_index do |type, i|
      integrations.create!(id: i + 1, type: "#{type}Service")
    end

    integrations.create!(id: namespaced_integrations.size + 1, type: 'LegacyService')
  ensure
    integrations.connection.execute 'ALTER TABLE integrations ENABLE TRIGGER "trigger_type_new_on_insert"'
  end

  it 'backfills `type_new` for the selected records' do
    # We don't want to mock `Kernel.sleep`, so instead we mock it on the migration
    # class before it gets forwarded.
    expect(migration).to receive(:sleep).with(0.05).exactly(5).times

    queries = ActiveRecord::QueryRecorder.new do
      migration.perform(2, 10, :integrations, :id, 2, 50)
    end

    expect(queries.count).to be(16)
    expect(queries.log.grep(/^SELECT/).size).to be(11)
    expect(queries.log.grep(/^UPDATE/).size).to be(5)
    expect(queries.log.grep(/^UPDATE/).join.scan(/WHERE .*/)).to eq([
      'WHERE integrations.id BETWEEN 2 AND 3',
      'WHERE integrations.id BETWEEN 4 AND 5',
      'WHERE integrations.id BETWEEN 6 AND 7',
      'WHERE integrations.id BETWEEN 8 AND 9',
      'WHERE integrations.id BETWEEN 10 AND 10'
    ])

    expect(integrations.where(id: 2..10).pluck(:type, :type_new)).to contain_exactly(
      ['AssemblaService',           'Integrations::Assembla'],
      ['BambooService',             'Integrations::Bamboo'],
      ['BugzillaService',           'Integrations::Bugzilla'],
      ['BuildkiteService',          'Integrations::Buildkite'],
      ['CampfireService',           'Integrations::Campfire'],
      ['ConfluenceService',         'Integrations::Confluence'],
      ['CustomIssueTrackerService', 'Integrations::CustomIssueTracker'],
      ['DatadogService',            'Integrations::Datadog'],
      ['DiscordService',            'Integrations::Discord']
    )

    expect(integrations.where.not(id: 2..10)).to all(have_attributes(type_new: nil))
  end
end