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: f747fd715364504dd98bc0d8b7faad2f58f6a6e6 (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.tr('?&,', '')
    label = target.project.labels.find_or_create_by(title: label_name, color: Label::DEFAULT_COLOR)

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