summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200421195234_backfill_status_page_published_incidents.rb
blob: fa7a5a9d924d991a561d3c8df86c86dd78e20456 (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
# frozen_string_literal: true

class BackfillStatusPagePublishedIncidents < ActiveRecord::Migration[6.0]
  DOWNTIME = false

  disable_ddl_transaction!

  class Incident < ActiveRecord::Base
    self.table_name = 'status_page_published_incidents'
  end

  class StatusPageIssue < ActiveRecord::Base
    include ::EachBatch

    self.table_name = 'issues'

    scope :published_only, -> do
      joins('INNER JOIN status_page_settings ON status_page_settings.project_id = issues.project_id')
        .where('status_page_settings.enabled = true')
        .where(confidential: false)
    end
  end

  def up
    current_time = Time.current

    StatusPageIssue.published_only.each_batch do |batch|
      incidents = batch.map do |status_page_issue|
        {
          issue_id: status_page_issue.id,
          created_at: current_time,
          updated_at: current_time
        }
      end

      Incident.insert_all(incidents, unique_by: :issue_id)
    end
  end

  def down
    # no op

    # While we expect this table to be empty at the point of
    # the up migration, there is no reliable way to determine
    # whether records were added as a part of the migration
    # or after it has run.
  end
end