Commit 24920bc5 authored by Yorick Peterse's avatar Yorick Peterse

Add Project.where_paths_in

This method can be used to find multiple projects for multiple paths.
For example, take this snippet:

    Project.where_paths_in(%w{gitlab-org/gitlab-ce gitlab-org/gitlab-ee})

This will return an ActiveRecord::Relation containing the GitLab CE and
GitLab EE projects.

This method takes care of matching rows both case-sensitively and
case-insensitively where needed.

Project.find_with_namespace in turn has been modified to use
Project.where_paths_in without nuking any scoping (instead it uses
reorder(nil)). This means that any default scopes (e.g. those used for
"pending_delete" stay intact).

The method Project.where_paths_in was added so the various Markdown
filters can use a single query to grab all the projects referenced in a
set of documents, something Project.find_with_namespace didn't allow.
parent cea3cf17
...@@ -253,20 +253,69 @@ class Project < ActiveRecord::Base ...@@ -253,20 +253,69 @@ class Project < ActiveRecord::Base
non_archived.where(table[:name].matches(pattern)) non_archived.where(table[:name].matches(pattern))
end end
def find_with_namespace(id) # Finds a single project for the given path.
namespace_path, project_path = id.split('/', 2) #
# path - The full project path (including namespace path).
#
# Returns a Project, or nil if no project could be found.
def find_with_namespace(path)
where_paths_in([path]).reorder(nil).take
end
return nil if !namespace_path || !project_path # Builds a relation to find multiple projects by their full paths.
#
# Each path must be in the following format:
#
# namespace_path/project_path
#
# For example:
#
# gitlab-org/gitlab-ce
#
# Usage:
#
# Project.where_paths_in(%w{gitlab-org/gitlab-ce gitlab-org/gitlab-ee})
#
# This would return the projects with the full paths matching the values
# given.
#
# paths - An Array of full paths (namespace path + project path) for which
# to find the projects.
#
# Returns an ActiveRecord::Relation.
def where_paths_in(paths)
wheres = []
cast_lower = Gitlab::Database.postgresql?
# Use of unscoped ensures we're not secretly adding any ORDER BYs, which paths.each do |path|
# have a negative impact on performance (and aren't needed for this namespace_path, project_path = path.split('/', 2)
# query).
projects = unscoped. next unless namespace_path && project_path
joins(:namespace).
iwhere('namespaces.path' => namespace_path) namespace_path = connection.quote(namespace_path)
project_path = connection.quote(project_path)
projects.find_by('projects.path' => project_path) || where = "(namespaces.path = #{namespace_path}
projects.iwhere('projects.path' => project_path).take AND projects.path = #{project_path})"
if cast_lower
where = "(
#{where}
OR (
LOWER(namespaces.path) = LOWER(#{namespace_path})
AND LOWER(projects.path) = LOWER(#{project_path})
)
)"
end
wheres << where
end
if wheres.empty?
none
else
joins(:namespace).where(wheres.join(' OR '))
end
end end
def visibility_levels def visibility_levels
......
...@@ -859,4 +859,37 @@ describe Project, models: true do ...@@ -859,4 +859,37 @@ describe Project, models: true do
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
end end
end end
describe '.where_paths_in' do
context 'without any paths' do
it 'returns an empty relation' do
expect(Project.where_paths_in([])).to eq([])
end
end
context 'without any valid paths' do
it 'returns an empty relation' do
expect(Project.where_paths_in(%w[foo])).to eq([])
end
end
context 'with valid paths' do
let!(:project1) { create(:project) }
let!(:project2) { create(:project) }
it 'returns the projects matching the paths' do
projects = Project.where_paths_in([project1.path_with_namespace,
project2.path_with_namespace])
expect(projects).to contain_exactly(project1, project2)
end
it 'returns projects regardless of the casing of paths' do
projects = Project.where_paths_in([project1.path_with_namespace.upcase,
project2.path_with_namespace.upcase])
expect(projects).to contain_exactly(project1, project2)
end
end
end
end end
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