summaryrefslogtreecommitdiff
path: root/db/migrate/20190527135836_fill_array_column_for_labels.rb
blob: d40bd1c4e8557a2718c534b3bea8d1ec335f0160 (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
class FillArrayColumnForLabels < ActiveRecord::Migration[5.1]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  def up
    update!('issues')
    update!('merge_requests')
  end

  private

  def update!(table)
    execute <<-SQL
      UPDATE #{table}
      SET label_titles = subquery.label_titles
      FROM
        (
          SELECT #{table}.id AS id, array_agg(labels.title) AS label_titles
          FROM #{table}
          LEFT JOIN label_links ON target_id = #{table}.id AND label_links.target_type = 'Issue'
          LEFT JOIN labels ON label_id = labels.id
          GROUP BY #{table}.id
        ) AS subquery
      WHERE #{table}.id = subquery.id;
    SQL
  end
end