summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/backfill_incident_issue_escalation_statuses.rb
blob: 2d46ff6b93347342557c2bbdb31931fc93d193f1 (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # BackfillIncidentIssueEscalationStatuses adds
    # IncidentManagement::IssuableEscalationStatus records for existing Incident issues.
    # They will be added with no policy, and escalations_started_at as nil.
    class BackfillIncidentIssueEscalationStatuses
      def perform(start_id, stop_id)
        ActiveRecord::Base.connection.execute <<~SQL
          INSERT INTO incident_management_issuable_escalation_statuses (issue_id, created_at, updated_at)
            SELECT issues.id, current_timestamp, current_timestamp
            FROM issues
            WHERE issues.issue_type = 1
            AND issues.id BETWEEN #{start_id} AND #{stop_id}
            ON CONFLICT (issue_id) DO NOTHING;
        SQL

        mark_job_as_succeeded(start_id, stop_id)
      end

      private

      def mark_job_as_succeeded(*arguments)
        ::Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
          self.class.name.demodulize,
          arguments
        )
      end
    end
  end
end