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

20181010133639_backfill_store_project_full_path_in_repo.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e2e4527d2b1def041882e2df92e3e3a6b4c4263 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# frozen_string_literal: true

class BackfillStoreProjectFullPathInRepo < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  class Repository
    attr_reader :storage

    def initialize(storage, relative_path)
      @storage = storage
      @relative_path = relative_path
    end

    def gitaly_repository
      Gitaly::Repository.new(storage_name: @storage, relative_path: @relative_path)
    end
  end

  module Storage
    class HashedProject
      attr_accessor :project

      ROOT_PATH_PREFIX = '@hashed'.freeze

      def initialize(project)
        @project = project
      end

      def disk_path
        "#{ROOT_PATH_PREFIX}/#{disk_hash[0..1]}/#{disk_hash[2..3]}/#{disk_hash}"
      end

      def disk_hash
        @disk_hash ||= Digest::SHA2.hexdigest(project.id.to_s) if project.id
      end
    end

    class LegacyProject
      attr_accessor :project

      def initialize(project)
        @project = project
      end

      def disk_path
        project.full_path
      end
    end
  end

  module Routable
    extend ActiveSupport::Concern

    def full_path
      @full_path ||= build_full_path
    end

    def build_full_path
      if parent && path
        parent.full_path + '/' + path
      else
        path
      end
    end
  end

  class Namespace < ActiveRecord::Base
    self.table_name = 'namespaces'

    include Routable

    belongs_to :parent, class_name: "Namespace"
  end

  class Project < ActiveRecord::Base
    self.table_name = 'projects'

    include Routable
    include EachBatch

    FULLPATH_CONFIG_KEY = 'gitlab.fullpath'

    belongs_to :namespace
    delegate :disk_path, to: :storage
    alias_method :parent, :namespace

    def add_fullpath_config
      entries = { FULLPATH_CONFIG_KEY => full_path }

      repository_service.set_config(entries)
    end

    def remove_fullpath_config
      repository_service.delete_config([FULLPATH_CONFIG_KEY])
    end

    def storage
      @storage ||=
        if hashed_storage?
          Storage::HashedProject.new(self)
        else
          Storage::LegacyProject.new(self)
        end
    end

    def hashed_storage?
      self.storage_version && self.storage_version >= 1
    end

    def repository
      @repository ||= Repository.new(repository_storage, disk_path + '.git')
    end

    def repository_service
      @repository_service ||= Gitlab::GitalyClient::RepositoryService.new(repository)
    end
  end

  def up
    Project.each_batch do |batch|
      batch.each do |project|
        project.add_fullpath_config
      end
    end
  end

  def down
    Project.each_batch do |batch|
      batch.each do |project|
        project.remove_fullpath_config
      end
    end
  end
end