Commit 389057f0 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Rename Projects & Namespaces based on entire paths

parent e50f4bc0
...@@ -27,7 +27,8 @@ class RenameReservedDynamicPaths < ActiveRecord::Migration ...@@ -27,7 +27,8 @@ class RenameReservedDynamicPaths < ActiveRecord::Migration
users users
] ]
DISALLOWED_WILDCARD_PATHS = %w[objects folders file] DISALLOWED_WILDCARD_PATHS = %w[info/lfs/objects gitlab-lfs/objects
environments/folders]
def up def up
rename_root_paths(DISALLOWED_ROOT_PATHS) rename_root_paths(DISALLOWED_ROOT_PATHS)
......
...@@ -13,6 +13,10 @@ module Gitlab ...@@ -13,6 +13,10 @@ module Gitlab
@migration = migration @migration = migration
end end
def path_patterns
@path_patterns ||= paths.map { |path| "%#{path}" }
end
def rename_path_for_routable(routable) def rename_path_for_routable(routable)
old_path = routable.path old_path = routable.path
old_full_path = routable.full_path old_full_path = routable.full_path
......
...@@ -16,9 +16,9 @@ module Gitlab ...@@ -16,9 +16,9 @@ module Gitlab
elsif type == :top_level elsif type == :top_level
MigrationClasses::Namespace.where(parent_id: nil) MigrationClasses::Namespace.where(parent_id: nil)
end end
with_paths = MigrationClasses::Namespace.arel_table[:path]. with_paths = MigrationClasses::Route.arel_table[:path].
matches_any(paths) matches_any(path_patterns)
namespaces.where(with_paths) namespaces.joins(:route).where(with_paths)
end end
def rename_namespace(namespace) def rename_namespace(namespace)
...@@ -43,8 +43,8 @@ module Gitlab ...@@ -43,8 +43,8 @@ module Gitlab
end end
def repo_paths_for_namespace(namespace) def repo_paths_for_namespace(namespace)
projects_for_namespace(namespace). projects_for_namespace(namespace).distinct.select(:repository_storage).
select('distinct(repository_storage)').map(&:repository_storage_path) map(&:repository_storage_path)
end end
def projects_for_namespace(namespace) def projects_for_namespace(namespace)
......
...@@ -28,9 +28,10 @@ module Gitlab ...@@ -28,9 +28,10 @@ module Gitlab
end end
def projects_for_paths def projects_for_paths
with_paths = MigrationClasses::Project.arel_table[:path] with_paths = MigrationClasses::Route.arel_table[:path]
.matches_any(paths) .matches_any(path_patterns)
MigrationClasses::Project.where(with_paths)
MigrationClasses::Project.joins(:route).where(with_paths)
end end
end end
end end
......
...@@ -14,6 +14,19 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameNamespaces do ...@@ -14,6 +14,19 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameNamespaces do
end end
describe '#namespaces_for_paths' do describe '#namespaces_for_paths' do
context 'nested namespaces' do
let(:subject) { described_class.new(['parent/the-Path'], migration) }
it 'includes the namespace' do
parent = create(:namespace, path: 'parent')
child = create(:namespace, path: 'the-path', parent: parent)
found_ids = subject.namespaces_for_paths(type: :wildcard).
map(&:id)
expect(found_ids).to contain_exactly(child.id)
end
end
context 'for wildcard namespaces' do context 'for wildcard namespaces' do
it 'only returns child namespaces with the correct path' do it 'only returns child namespaces with the correct path' do
_root_namespace = create(:namespace, path: 'THE-path') _root_namespace = create(:namespace, path: 'THE-path')
......
...@@ -9,6 +9,16 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameProjects do ...@@ -9,6 +9,16 @@ describe Gitlab::Database::RenameReservedPathsMigration::RenameProjects do
end end
describe '#projects_for_paths' do describe '#projects_for_paths' do
it 'searches using nested paths' do
namespace = create(:namespace, path: 'hello')
project = create(:empty_project, path: 'THE-path', namespace: namespace)
result_ids = described_class.new(['Hello/the-path'], migration).
projects_for_paths.map(&:id)
expect(result_ids).to contain_exactly(project.id)
end
it 'includes the correct projects' do it 'includes the correct projects' do
project = create(:empty_project, path: 'THE-path') project = create(:empty_project, path: 'THE-path')
_other_project = create(:empty_project) _other_project = create(:empty_project)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment