namespace.rb 2.7 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3 4
# == Schema Information
#
# Table name: namespaces
#
A
Andrew8xx8 已提交
5 6 7
#  id          :integer          not null, primary key
#  name        :string(255)      not null
#  path        :string(255)      not null
D
Dmitriy Zaporozhets 已提交
8
#  owner_id    :integer
A
Andrew8xx8 已提交
9 10 11
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#  type        :string(255)
D
Dmitriy Zaporozhets 已提交
12
#  description :string(255)      default(""), not null
S
Steven Thonus 已提交
13
#  avatar      :string(255)
D
Dmitriy Zaporozhets 已提交
14 15
#

16
class Namespace < ActiveRecord::Base
17 18
  include Gitlab::ShellAdapter

A
Andrew8xx8 已提交
19
  attr_accessible :name, :description, :path
20

21
  has_many :projects, dependent: :destroy
22 23
  belongs_to :owner, class_name: "User"

24
  validates :owner, presence: true, unless: ->(n) { n.type == "Group" }
25 26 27 28
  validates :name, presence: true, uniqueness: true,
            length: { within: 0..255 },
            format: { with: Gitlab::Regex.name_regex,
                      message: "only letters, digits, spaces & '_' '-' '.' allowed." }
A
Andrew8xx8 已提交
29
  validates :description, length: { within: 0..255 }
30
  validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
D
Dmitriy Zaporozhets 已提交
31
            exclusion: { in: Gitlab::Blacklist.path },
32
            format: { with: Gitlab::Regex.path_regex,
33
                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
34 35 36

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

37
  after_create :ensure_dir_exist
38
  after_update :move_dir, if: :path_changed?
39
  after_destroy :rm_dir
40

A
Andrew8xx8 已提交
41
  scope :root, -> { where('type IS NULL') }
42

43
  def self.search query
44
    where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
45 46
  end

47 48 49 50
  def self.global_id
    'GLN'
  end

51
  def to_param
52
    path
53
  end
54 55 56 57

  def human_name
    owner_name
  end
58 59

  def ensure_dir_exist
60
    gitlab_shell.add_namespace(path)
D
Dmitriy Zaporozhets 已提交
61 62
  end

63 64
  def rm_dir
    gitlab_shell.rm_namespace(path)
65
  end
66 67

  def move_dir
68 69 70 71 72
    if gitlab_shell.mv_namespace(path_was, path)
      # If repositories moved successfully we need to remove old satellites
      # and send update instructions to users.
      # However we cannot allow rollback since we moved namespace dir
      # So we basically we mute exceptions in next actions
G
GitLab 已提交
73
      begin
74
        gitlab_shell.rm_satellites(path_was)
75
        send_update_instructions
76
      rescue
J
Johannes Schleifenbaum 已提交
77
        # Returning false does not rollback after_* transaction but gives
78 79
        # us information about failing some of tasks
        false
80
      end
81 82 83 84
    else
      # if we cannot move namespace directory we should rollback
      # db changes in order to prevent out of sync between db and fs
      raise Exception.new('namespace directory cannot be moved')
85
    end
86
  end
87

88 89 90
  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
91 92 93 94

  def kind
    type == 'Group' ? 'group' : 'user'
  end
95
end