summaryrefslogtreecommitdiff
path: root/app/models/namespace.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/namespace.rb')
-rw-r--r--app/models/namespace.rb39
1 files changed, 33 insertions, 6 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb
index 3342fb1fce9..3f7ccdb977e 100644
--- a/app/models/namespace.rb
+++ b/app/models/namespace.rb
@@ -63,12 +63,11 @@ class Namespace < ApplicationRecord
validates :max_artifacts_size, numericality: { only_integer: true, greater_than: 0, allow_nil: true }
+ validate :validate_parent_type, if: -> { Feature.enabled?(:validate_namespace_parent_type) }
validate :nesting_level_allowed
validate :changing_shared_runners_enabled_is_allowed
validate :changing_allow_descendants_override_disabled_shared_runners_is_allowed
- validates_associated :runners
-
delegate :name, to: :owner, allow_nil: true, prefix: true
delegate :avatar_url, to: :owner, allow_nil: true
@@ -84,6 +83,8 @@ class Namespace < ApplicationRecord
before_destroy(prepend: true) { prepare_for_destroy }
after_destroy :rm_dir
+ before_save :ensure_delayed_project_removal_assigned_to_namespace_settings, if: :delayed_project_removal_changed?
+
scope :for_user, -> { where('type IS NULL') }
scope :sort_by_type, -> { order(Gitlab::Database.nulls_first_order(:type)) }
scope :include_route, -> { includes(:route) }
@@ -164,6 +165,10 @@ class Namespace < ApplicationRecord
name = host.delete_suffix(gitlab_host)
Namespace.where(parent_id: nil).by_path(name)
end
+
+ def top_most
+ where(parent_id: nil)
+ end
end
def package_settings
@@ -255,11 +260,12 @@ class Namespace < ApplicationRecord
# Includes projects from this namespace and projects from all subgroups
# that belongs to this namespace
def all_projects
- if Feature.enabled?(:recursive_approach_for_all_projects)
- namespace = user? ? self : self_and_descendants
- Project.where(namespace: namespace)
+ return Project.where(namespace: self) if user?
+
+ if Feature.enabled?(:recursive_namespace_lookup_as_inner_join, self)
+ Project.joins("INNER JOIN (#{self_and_descendants.select(:id).to_sql}) namespaces ON namespaces.id=projects.namespace_id")
else
- Project.inside_path(full_path)
+ Project.where(namespace: self_and_descendants)
end
end
@@ -400,8 +406,19 @@ class Namespace < ApplicationRecord
!has_parent?
end
+ def recent?
+ created_at >= 90.days.ago
+ end
+
private
+ def ensure_delayed_project_removal_assigned_to_namespace_settings
+ return if Feature.disabled?(:migrate_delayed_project_removal, default_enabled: true)
+
+ self.namespace_settings || build_namespace_settings
+ namespace_settings.delayed_project_removal = delayed_project_removal
+ end
+
def all_projects_with_pages
if all_projects.pages_metadata_not_migrated.exists?
Gitlab::BackgroundMigration::MigratePagesMetadata.new.perform_on_relation(
@@ -437,6 +454,16 @@ class Namespace < ApplicationRecord
end
end
+ def validate_parent_type
+ return unless has_parent?
+
+ if user?
+ errors.add(:parent_id, 'a user namespace cannot have a parent')
+ elsif group?
+ errors.add(:parent_id, 'a group cannot have a user namespace as its parent') if parent.user?
+ end
+ end
+
def sync_share_with_group_lock_with_parent
if parent&.share_with_group_lock?
self.share_with_group_lock = true