2012-11-24 20:16:51 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: namespaces
|
|
|
|
#
|
2013-02-08 11:42:14 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(255) not null
|
|
|
|
# path :string(255) not null
|
2013-10-01 12:15:28 +00:00
|
|
|
# owner_id :integer
|
2014-04-09 12:05:03 +00:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-02-08 11:42:14 +00:00
|
|
|
# type :string(255)
|
2013-03-15 13:16:02 +00:00
|
|
|
# description :string(255) default(""), not null
|
2014-01-27 21:34:05 +00:00
|
|
|
# avatar :string(255)
|
2012-11-24 20:16:51 +00:00
|
|
|
#
|
|
|
|
|
2012-11-22 18:34:16 +00:00
|
|
|
class Namespace < ActiveRecord::Base
|
2013-03-21 20:11:08 +00:00
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2012-11-23 18:11:09 +00:00
|
|
|
has_many :projects, dependent: :destroy
|
2012-11-22 18:34:16 +00:00
|
|
|
belongs_to :owner, class_name: "User"
|
|
|
|
|
2013-09-26 11:49:22 +00:00
|
|
|
validates :owner, presence: true, unless: ->(n) { n.type == "Group" }
|
2013-02-18 07:28:18 +00:00
|
|
|
validates :name, presence: true, uniqueness: true,
|
|
|
|
length: { within: 0..255 },
|
|
|
|
format: { with: Gitlab::Regex.name_regex,
|
2014-06-26 07:53:01 +00:00
|
|
|
message: Gitlab::Regex.name_regex_message }
|
2013-02-07 10:42:52 +00:00
|
|
|
validates :description, length: { within: 0..255 }
|
2014-02-20 08:45:25 +00:00
|
|
|
validates :path, uniqueness: { case_sensitive: false }, presence: true, length: { within: 1..255 },
|
2013-06-12 19:11:35 +00:00
|
|
|
exclusion: { in: Gitlab::Blacklist.path },
|
2012-11-28 03:14:05 +00:00
|
|
|
format: { with: Gitlab::Regex.path_regex,
|
2014-06-26 07:53:01 +00:00
|
|
|
message: Gitlab::Regex.path_regex_message }
|
2012-11-22 18:34:16 +00:00
|
|
|
|
|
|
|
delegate :name, to: :owner, allow_nil: true, prefix: true
|
|
|
|
|
2012-11-24 20:11:46 +00:00
|
|
|
after_create :ensure_dir_exist
|
2013-03-21 20:11:08 +00:00
|
|
|
after_update :move_dir, if: :path_changed?
|
2012-11-21 05:54:05 +00:00
|
|
|
after_destroy :rm_dir
|
2012-11-23 06:11:09 +00:00
|
|
|
|
2013-02-12 07:16:45 +00:00
|
|
|
scope :root, -> { where('type IS NULL') }
|
2012-11-23 04:24:09 +00:00
|
|
|
|
2014-09-25 22:07:40 +00:00
|
|
|
def self.search(query)
|
2012-11-23 18:11:09 +00:00
|
|
|
where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
|
2012-11-22 18:34:16 +00:00
|
|
|
end
|
|
|
|
|
2012-11-27 06:31:15 +00:00
|
|
|
def self.global_id
|
|
|
|
'GLN'
|
|
|
|
end
|
|
|
|
|
2012-11-22 18:34:16 +00:00
|
|
|
def to_param
|
2012-11-23 18:11:09 +00:00
|
|
|
path
|
2012-11-22 18:34:16 +00:00
|
|
|
end
|
2012-11-23 04:11:09 +00:00
|
|
|
|
|
|
|
def human_name
|
|
|
|
owner_name
|
|
|
|
end
|
2012-11-23 06:11:09 +00:00
|
|
|
|
|
|
|
def ensure_dir_exist
|
2013-03-21 20:11:08 +00:00
|
|
|
gitlab_shell.add_namespace(path)
|
2012-12-28 03:24:05 +00:00
|
|
|
end
|
|
|
|
|
2013-03-21 20:11:08 +00:00
|
|
|
def rm_dir
|
|
|
|
gitlab_shell.rm_namespace(path)
|
2012-11-23 06:11:09 +00:00
|
|
|
end
|
2012-11-24 20:11:46 +00:00
|
|
|
|
|
|
|
def move_dir
|
2013-03-21 20:11:08 +00:00
|
|
|
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
|
2013-01-17 10:21:52 +00:00
|
|
|
begin
|
2013-03-21 20:11:08 +00:00
|
|
|
gitlab_shell.rm_satellites(path_was)
|
2012-12-20 20:16:51 +00:00
|
|
|
send_update_instructions
|
2013-03-21 20:11:08 +00:00
|
|
|
rescue
|
2013-07-29 10:46:00 +00:00
|
|
|
# Returning false does not rollback after_* transaction but gives
|
2013-03-21 20:11:08 +00:00
|
|
|
# us information about failing some of tasks
|
|
|
|
false
|
2012-12-20 20:16:51 +00:00
|
|
|
end
|
2013-03-21 20:11:08 +00:00
|
|
|
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')
|
2012-11-27 06:31:15 +00:00
|
|
|
end
|
2012-11-24 20:11:46 +00:00
|
|
|
end
|
2012-11-21 05:54:05 +00:00
|
|
|
|
2012-12-20 20:16:51 +00:00
|
|
|
def send_update_instructions
|
|
|
|
projects.each(&:send_move_instructions)
|
|
|
|
end
|
2013-11-15 13:25:37 +00:00
|
|
|
|
|
|
|
def kind
|
|
|
|
type == 'Group' ? 'group' : 'user'
|
|
|
|
end
|
2012-11-22 18:34:16 +00:00
|
|
|
end
|