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

tags.rb « migrate « ci « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e4872f07165d65f5d806a0a380c6929289f0059 (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
require 'yaml'

module Ci
  module Migrate
    class Tags
      def restore
        ActiveRecord::Base.transaction do
          puts 'Inserting tags...'
          connection.execute(
            'INSERT INTO tags (name) ' +
              'SELECT ci_tags.name FROM ci_tags ' +
              'WHERE (SELECT COUNT(*) FROM tags WHERE tags.name = ci_tags.name)=0'
          )

          puts 'Deleting old taggings...'
          connection.execute "DELETE FROM taggings WHERE context = 'tags' AND taggable_type LIKE 'Ci::%'"

          puts 'Inserting taggings...'
          connection.execute(
            'INSERT INTO taggings (taggable_type, taggable_id, tag_id, context) ' +
              "SELECT CONCAT('Ci::', ci_taggings.taggable_type), ci_taggings.taggable_id, tags.id, 'tags' FROM ci_taggings " +
              'JOIN ci_tags ON ci_tags.id = ci_taggings.tag_id ' +
              'JOIN tags ON tags.name = ci_tags.name '
          )

          puts 'Resetting counters... '
          connection.execute(
            'UPDATE tags SET ' +
              'taggings_count = (SELECT COUNT(*) FROM taggings WHERE tags.id = taggings.tag_id)'
          )
        end
      end

      protected

      def connection
        ActiveRecord::Base.connection
      end
    end
  end
end