namespace.rb 2.57 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
2
3
4
# == Schema Information
#
# Table name: namespaces
#
5
6
7
8
9
10
11
#  id          :integer          not null, primary key
#  name        :string(255)      not null
#  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
12
#  description :string(255)      default(""), not null
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
13
14
#

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

Andrew8xx8's avatar
Andrew8xx8 committed
18
  attr_accessible :name, :description, :path
19

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

23
24
25
26
27
  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
28
  validates :description, length: { within: 0..255 }
29
  validates :path, uniqueness: true, presence: true, length: { within: 1..255 },
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
30
            exclusion: { in: Gitlab::Blacklist.path },
31
            format: { with: Gitlab::Regex.path_regex,
32
                      message: "only letters, digits & '_' '-' '.' allowed. Letter should be first" }
33
34
35

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

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

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

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

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

50
  def to_param
51
    path
52
  end
53
54
55
56

  def human_name
    owner_name
  end
57
58

  def ensure_dir_exist
59
    gitlab_shell.add_namespace(path)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
60
61
  end

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

  def move_dir
67
68
69
70
71
    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
72
      begin
73
        gitlab_shell.rm_satellites(path_was)
74
        send_update_instructions
75
76
77
78
      rescue
        # Returning false does not rolback after_* transaction but gives
        # us information about failing some of tasks
        false
79
      end
80
81
82
83
    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')
84
    end
85
  end
86

87
88
89
  def send_update_instructions
    projects.each(&:send_move_instructions)
  end
90
end