summaryrefslogtreecommitdiff
path: root/db/migrate/20160129135155_remove_dot_atom_path_ending_of_projects.rb
blob: 6254017615b8bf65177d72aef6a2540154539f89 (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
class RemoveDotAtomPathEndingOfProjects < ActiveRecord::Migration[4.2]
  include Gitlab::ShellAdapter

  class ProjectPath
    attr_reader :old_path, :id, :namespace_path

    def initialize(old_path, id, namespace_path, namespace_id)
      @old_path = old_path
      @id = id
      @namespace_path = namespace_path
      @namespace_id = namespace_id
    end

    def clean_path
      @_clean_path ||= PathCleaner.clean(@old_path, @namespace_id)
    end
  end

  class PathCleaner
    def initialize(path, namespace_id)
      @namespace_id = namespace_id
      @path = path
    end

    def self.clean(*args)
      new(*args).clean
    end

    def clean
      path = cleaned_path
      count = 0
      while path_exists?(path)
        path = "#{cleaned_path}#{count}"
        count += 1
      end
      path
    end

    private

    def cleaned_path
      @_cleaned_path ||= @path.gsub(/\.atom\z/, '-atom')
    end

    def path_exists?(path)
      Project.find_by_path_and_namespace_id(path, @namespace_id)
    end
  end

  def projects_with_dot_atom
    select_all("SELECT p.id, p.path, n.path as namespace_path, n.id as namespace_id FROM projects p inner join namespaces n on n.id = p.namespace_id WHERE p.path LIKE '%.atom'")
  end

  def up
    projects_with_dot_atom.each do |project|
      project_path = ProjectPath.new(project['path'], project['id'], project['namespace_path'], project['namespace_id'])
      clean_path(project_path) if rename_project_repo(project_path)
    end
  end

  private

  def clean_path(project_path)
    execute "UPDATE projects SET path = #{sanitize(project_path.clean_path)} WHERE id = #{project_path.id}"
  end

  def rename_project_repo(project_path)
    old_path_with_namespace = File.join(project_path.namespace_path, project_path.old_path)
    new_path_with_namespace = File.join(project_path.namespace_path, project_path.clean_path)

    gitlab_shell.mv_repository("#{old_path_with_namespace}.wiki", "#{new_path_with_namespace}.wiki")
    gitlab_shell.mv_repository(old_path_with_namespace, new_path_with_namespace)
  rescue
    false
  end

  def sanitize(value)
    ActiveRecord::Base.connection.quote(value)
  end
end