namespace.rb 2.65 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4
# == Schema Information
#
# Table name: namespaces
#
5 6
#  id          :integer          not null, primary key
#  name        :string(255)      not null
Andrew8xx8's avatar
Andrew8xx8 committed
7
#  description :string(255)      not null
8 9 10 11 12
#  path        :string(255)      not null
#  owner_id    :integer          not null
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  type        :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13 14
#

15
class Namespace < ActiveRecord::Base
Andrew8xx8's avatar
Andrew8xx8 committed
16
  attr_accessible :name, :description, :path
17

18
  has_many :projects, dependent: :destroy
19 20
  belongs_to :owner, class_name: "User"

21 22 23 24 25
  validates :owner, presence: true
  validates :name, presence: true, uniqueness: true,
            length: { within: 0..255 },
            format: { with: Gitlab::Regex.name_regex,
                      message: "only letters, digits, spaces & '_' '-' '.' allowed." }
Andrew8xx8's avatar
Andrew8xx8 committed
26
  validates :description, length: { within: 0..255 }
27
  validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
28
            format: { with: Gitlab::Regex.path_regex,
29
                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
30 31 32

  delegate :name, to: :owner, allow_nil: true, prefix: true

33 34
  after_create :ensure_dir_exist
  after_update :move_dir
35
  after_destroy :rm_dir
36

Andrew8xx8's avatar
Andrew8xx8 committed
37
  scope :root, -> { where('type IS NULL') }
38

39
  def self.search query
40
    where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
41 42
  end

43 44 45 46
  def self.global_id
    'GLN'
  end

47
  def to_param
48
    path
49
  end
50 51 52 53

  def human_name
    owner_name
  end
54 55

  def ensure_dir_exist
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
56
    unless dir_exists?
57
      FileUtils.mkdir( namespace_full_path, mode: 0770 )
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
58
    end
59 60 61
  end

  def dir_exists?
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
62 63 64 65
    File.exists?(namespace_full_path)
  end

  def namespace_full_path
66
    @namespace_full_path ||= File.join(Gitlab.config.gitlab_shell.repos_path, path)
67
  end
68 69

  def move_dir
70
    if path_changed?
71 72
      old_path = File.join(Gitlab.config.gitlab_shell.repos_path, path_was)
      new_path = File.join(Gitlab.config.gitlab_shell.repos_path, path)
73 74 75
      if File.exists?(new_path)
        raise "Already exists"
      end
76 77 78 79 80 81 82 83 84


      begin
        # Remove satellite when moving repo
        if path_was.present?
          satellites_path = File.join(Gitlab.config.satellites.path, path_was)
          FileUtils.rm_r( satellites_path, force: true )
        end

85
        FileUtils.mv( old_path, new_path )
86
        send_update_instructions
87
      rescue Exception => e
88
        raise "Namespace move error #{old_path} #{new_path}"
89
      end
90
    end
91
  end
92 93

  def rm_dir
94
    dir_path = File.join(Gitlab.config.gitlab_shell.repos_path, path)
95
    FileUtils.rm_r( dir_path, force: true )
96
  end
97 98 99 100

  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
101
end