summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/relation_rename_service.rb
blob: 03aaa6aefc335eaa7803beca2e085dc74ad1a03d (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
42
43
44
45
46
47
48
# frozen_string_literal: true

# This class is intended to help with relation renames within Gitlab versions
# and allow compatibility between versions.
# If you have to change one relationship name that is imported/exported,
# you should add it to the RENAMES constant indicating the old name and the
# new one.
# The behavior of these renamed relationships should be transient and it should
# only last one release until you completely remove the renaming from the list.
#
# When importing, this class will check the hash and:
# - if only the old relationship name is found, it will rename it with the new one
# - if only the new relationship name is found, it will do nothing
# - if it finds both, it will use the new relationship data
#
# When exporting, this class will duplicate the keys in the resulting file.
# This way, if we open the file in an old version of the exporter it will work
# and also it will with the newer versions.
module Gitlab
  module ImportExport
    class RelationRenameService
      RENAMES = {
        'pipelines' => 'ci_pipelines' # Added in 11.6, remove in 11.7
      }.freeze

      def self.rename(tree_hash)
        return unless tree_hash&.present?

        RENAMES.each do |old_name, new_name|
          old_entry = tree_hash.delete(old_name)

          next if tree_hash[new_name]
          next unless old_entry

          tree_hash[new_name] = old_entry
        end
      end

      def self.add_new_associations(tree_hash)
        RENAMES.each do |old_name, new_name|
          next if tree_hash.key?(old_name)

          tree_hash[old_name] = tree_hash[new_name]
        end
      end
    end
  end
end