summaryrefslogtreecommitdiff
path: root/spec/migrations/20200706035141_adjust_unique_index_alert_management_alerts_spec.rb
blob: 0068571ad0da080dc245e9d3022003b2cc60e4f0 (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
# frozen_string_literal: true

require 'spec_helper'
require Rails.root.join('db', 'migrate', '20200706035141_adjust_unique_index_alert_management_alerts.rb')

RSpec.describe AdjustUniqueIndexAlertManagementAlerts, :migration do
  let(:migration) { described_class.new }
  let(:alerts) { AlertManagement::Alert }
  let(:project) { create_project }
  let(:other_project) { create_project }
  let(:resolved_state) { 2 }
  let(:triggered_state) { 1 }
  let!(:existing_alert) { create_alert(project, resolved_state, '1234', 1) }
  let!(:p2_alert) { create_alert(other_project, resolved_state, '1234', 1) }
  let!(:p2_alert_diff_fingerprint) { create_alert(other_project, resolved_state, '4567', 2) }

  it 'can reverse the migration' do
    expect(existing_alert.fingerprint).not_to eq(nil)
    expect(p2_alert.fingerprint).not_to eq(nil)
    expect(p2_alert_diff_fingerprint.fingerprint).not_to eq(nil)

    migrate!

    # Adding a second alert with the same fingerprint now that we can
    second_alert = create_alert(project, triggered_state, '1234', 2)
    expect(alerts.count).to eq(4)

    schema_migrate_down!

    # We keep the alerts, but the oldest ones fingerprint is removed
    expect(alerts.count).to eq(4)
    expect(second_alert.reload.fingerprint).not_to eq(nil)
    expect(p2_alert.fingerprint).not_to eq(nil)
    expect(p2_alert_diff_fingerprint.fingerprint).not_to eq(nil)
    expect(existing_alert.reload.fingerprint).to eq(nil)
  end

  def namespace
    @namespace ||= table(:namespaces).create!(name: 'foo', path: 'foo')
  end

  def create_project
    table(:projects).create!(namespace_id: namespace.id)
  end

  def create_alert(project, status, fingerprint, iid)
    params = {
      title: 'test',
      started_at: Time.current,
      iid: iid,
      project_id: project.id,
      status: status,
      fingerprint: fingerprint
    }
    table(:alert_management_alerts).create!(params)
  end
end