Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20200723040950_migrate_incident_issues_to_incident_type.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0f56263038113b4ab2f59e6be999a16ddf68ca3b (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
# frozen_string_literal: true

class MigrateIncidentIssuesToIncidentType < ActiveRecord::Migration[6.0]
  DOWNTIME = false
  BATCH_SIZE = 100

  disable_ddl_transaction!

  LABEL_PROPERTIES = {
    title: 'incident'
  }.freeze

  class Issue < ActiveRecord::Base
    include EachBatch

    self.table_name = 'issues'

    scope :incident_labelled, -> do
      joins("INNER JOIN label_links ON label_links.target_type = 'Issue' AND label_links.target_id = issues.id")
        .joins("INNER JOIN labels ON labels.id = label_links.label_id")
        .where(labels: LABEL_PROPERTIES)
    end

    enum issue_type: {
      issue: 0,
      incident: 1
    }

    scope :incident_typed, -> { where(issue_type: :incident) }
  end

  def up
    incident_issues = Issue.incident_labelled

    incident_issues.each_batch(of: BATCH_SIZE) do |batch|
      batch.update_all(issue_type: :incident)
    end
  end

  def down
    incident_issues = Issue.incident_typed

    incident_issues.each_batch(of: BATCH_SIZE) do |batch|
      batch.update_all(issue_type: :issue)
    end
  end
end