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

20140729152420_migrate_taggable_labels.rb « migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b844720ba13174bdbf2e4cd42743c7d8765b4d3 (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
class MigrateTaggableLabels < ActiveRecord::Migration
  def up
    taggings = ActsAsTaggableOn::Tagging.where(taggable_type: ['Issue', 'MergeRequest'], context: 'labels')
    taggings.find_each(batch_size: 500) do |tagging|
      create_label_from_tagging(tagging)
    end
  end

  def down
    Label.destroy_all
    LabelLink.destroy_all
  end

  private

  def create_label_from_tagging(tagging)
    target = tagging.taggable
    label_name = tagging.tag.name
    label = target.project.labels.find_or_create_by(title: label_name)

    if LabelLink.create(label: label, target: target)
      print '.'
    else
      print 'F'
    end
  end
end