summaryrefslogtreecommitdiff
path: root/db/migrate/20190527135836_fill_array_column_for_labels.rb
diff options
context:
space:
mode:
Diffstat (limited to 'db/migrate/20190527135836_fill_array_column_for_labels.rb')
-rw-r--r--db/migrate/20190527135836_fill_array_column_for_labels.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/db/migrate/20190527135836_fill_array_column_for_labels.rb b/db/migrate/20190527135836_fill_array_column_for_labels.rb
new file mode 100644
index 00000000000..d40bd1c4e85
--- /dev/null
+++ b/db/migrate/20190527135836_fill_array_column_for_labels.rb
@@ -0,0 +1,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