summaryrefslogtreecommitdiff
path: root/db/post_migrate/20220629184402_unset_escalation_policies_for_alert_incidents.rb
blob: 89adc4b2703ade7a1e32376da4bd2046e4d5e25b (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
# frozen_string_literal: true

class UnsetEscalationPoliciesForAlertIncidents < Gitlab::Database::Migration[2.0]
  disable_ddl_transaction!

  restrict_gitlab_migration gitlab_schema: :gitlab_main

  class EscalationStatus < MigrationRecord
    include EachBatch

    self.table_name = 'incident_management_issuable_escalation_statuses'

    scope :having_alert_policy, -> do
      joins(
        'INNER JOIN alert_management_alerts ' \
        'ON alert_management_alerts.issue_id ' \
        '= incident_management_issuable_escalation_statuses.issue_id'
      )
    end
  end

  def up
    EscalationStatus.each_batch do |escalation_statuses|
      escalation_statuses
        .where.not(policy_id: nil)
        .having_alert_policy
        .update_all(policy_id: nil, escalations_started_at: nil)
    end
  end

  def down
    # no-op
    #
    # We cannot retrieve the exact nullified values. We could
    # approximately guess what the values are via the alert's
    # escalation policy. However, that may not be accurate
    # in all cases, as an alert's escalation policy is implictly
    # inferred from the project rather than explicit, like an incident.
    # So we won't backfill potentially incorrect data.
    #
    # This data is functionally safe to delete, as the relevant
    # fields are read-only, and exclusively informational.
    #
    # Re-running the migration will have no effect.
  end
end