Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
5679e63c
Commit
5679e63c
authored
Dec 02, 2021
by
Alex Pooley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Isolate descendant query in CTE
parent
e4de93b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
12 deletions
+36
-12
app/models/namespaces/traversal/linear_scopes.rb
app/models/namespaces/traversal/linear_scopes.rb
+17
-12
spec/support/shared_examples/namespaces/traversal_scope_examples.rb
...rt/shared_examples/namespaces/traversal_scope_examples.rb
+19
-0
No files found.
app/models/namespaces/traversal/linear_scopes.rb
View file @
5679e63c
...
...
@@ -105,27 +105,32 @@ module Namespaces
:traversal_ids
,
'LEAD (namespaces.traversal_ids, 1) OVER (ORDER BY namespaces.traversal_ids ASC) next_traversal_ids'
)
cte
=
Gitlab
::
SQL
::
CTE
.
new
(
:base_cte
,
base
)
base_
cte
=
Gitlab
::
SQL
::
CTE
.
new
(
:base_cte
,
base
)
namespaces
=
Arel
::
Table
.
new
(
:namespaces
)
records
=
unscoped
.
with
(
cte
.
to_arel
)
.
from
([
cte
.
table
,
namespaces
])
# Bound the search space to ourselves (optional) and descendants.
#
# WHERE (base_cte.next_traversal_ids IS NULL OR base_cte.next_traversal_ids > namespaces.traversal_ids)
# AND next_traversal_ids_sibling(base_cte.traversal_ids) > namespaces.traversal_ids
records
=
records
.
where
(
cte
.
table
[
:next_traversal_ids
].
eq
(
nil
).
or
(
cte
.
table
[
:next_traversal_ids
].
gt
(
namespaces
[
:traversal_ids
])))
.
where
(
next_sibling_func
(
cte
.
table
[
:traversal_ids
]).
gt
(
namespaces
[
:traversal_ids
]))
records
=
unscoped
.
from
([
base_cte
.
table
,
namespaces
])
.
where
(
base_cte
.
table
[
:next_traversal_ids
].
eq
(
nil
).
or
(
base_cte
.
table
[
:next_traversal_ids
].
gt
(
namespaces
[
:traversal_ids
])))
.
where
(
next_sibling_func
(
base_cte
.
table
[
:traversal_ids
]).
gt
(
namespaces
[
:traversal_ids
]))
# AND base_cte.traversal_ids <= namespaces.traversal_ids
if
include_self
records
.
where
(
cte
.
table
[
:traversal_ids
].
lteq
(
namespaces
[
:traversal_ids
]))
else
records
.
where
(
cte
.
table
[
:traversal_ids
].
lt
(
namespaces
[
:traversal_ids
]))
end
records
=
if
include_self
records
.
where
(
base_cte
.
table
[
:traversal_ids
].
lteq
(
namespaces
[
:traversal_ids
]))
else
records
.
where
(
base_cte
.
table
[
:traversal_ids
].
lt
(
namespaces
[
:traversal_ids
]))
end
records_cte
=
Gitlab
::
SQL
::
CTE
.
new
(
:descendants_cte
,
records
)
unscoped
.
unscope
(
where:
[
:type
])
.
with
(
base_cte
.
to_arel
,
records_cte
.
to_arel
)
.
from
(
records_cte
.
alias_to
(
namespaces
))
end
def
next_sibling_func
(
*
args
)
...
...
spec/support/shared_examples/namespaces/traversal_scope_examples.rb
View file @
5679e63c
...
...
@@ -213,6 +213,12 @@ RSpec.shared_examples 'namespace traversal scopes' do
it
{
is_expected
.
to
contain_exactly
(
deep_nested_group_1
,
deep_nested_group_2
)
}
end
context
'with offset and limit'
do
subject
{
described_class
.
where
(
id:
[
group_1
,
group_2
]).
offset
(
1
).
limit
(
1
).
self_and_descendants
}
it
{
is_expected
.
to
contain_exactly
(
group_2
,
nested_group_2
,
deep_nested_group_2
)
}
end
end
describe
'.self_and_descendants'
do
...
...
@@ -242,6 +248,19 @@ RSpec.shared_examples 'namespace traversal scopes' do
it
{
is_expected
.
to
contain_exactly
(
deep_nested_group_1
.
id
,
deep_nested_group_2
.
id
)
}
end
context
'with offset and limit'
do
subject
do
described_class
.
where
(
id:
[
group_1
,
group_2
])
.
limit
(
1
)
.
offset
(
1
)
.
self_and_descendant_ids
.
pluck
(
:id
)
end
it
{
is_expected
.
to
contain_exactly
(
group_2
.
id
,
nested_group_2
.
id
,
deep_nested_group_2
.
id
)
}
end
end
describe
'.self_and_descendant_ids'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment