BigW Consortium Gitlab

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

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

Andrew8xx8 committed
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." }
Andrew8xx8 committed
29
  validates :description, length: { within: 0..255 }
30
  validates :path, uniqueness: { case_sensitive: false }, presence: true, length: { within: 1..255 },
Dmitriy Zaporozhets committed
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

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)
Dmitriy Zaporozhets committed
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
73
      begin
74
        gitlab_shell.rm_satellites(path_was)
75
        send_update_instructions
76
      rescue
Johannes Schleifenbaum committed
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