Ignore ProjectNamespace objects in hierarchy queries
What does this MR do and why?
When getting ancestor/descendants of a namespace, only objects of the same type are fetched. This effectively makes difference for groups only (user namespaces can not have ancestors/descendants).
The reason of this change is that when getting group descendants (or self_and_ancestors
), we want to avoid getting namespaces of different classes because the code is not ready for it yet (IOW when group.descendants
is used somewhere, it doesn't expect that a ProjectNamespace object might be included in the result).
Eventually we will want to include project namespaces when querying namespace hierarchy (so we can get rid of having project vs group queries), but to be able to do this we need to eliminate all places where different namespaces may cause an issue first.
Related to #341823 (closed)
DB migration
== 20211020095357 AddGroupTraversalIdIndex: migrating =========================
-- transaction_open?()
-> 0.0000s
-- index_exists?(:namespaces, :traversal_ids, {:using=>:gin, :where=>"type='Group'", :name=>"index_namespaces_on_traversal_ids_for_groups", :algorithm=>:concurrently})
-> 0.0047s
-- add_index(:namespaces, :traversal_ids, {:using=>:gin, :where=>"type='Group'", :name=>"index_namespaces_on_traversal_ids_for_groups", :algorithm=>:concurrently})
-> 0.0124s
== 20211020095357 AddGroupTraversalIdIndex: migrated (0.0180s) ================
rollback:
$ rake db:rollback
== 20211020095357 AddGroupTraversalIdIndex: reverting =========================
-- transaction_open?()
-> 0.0000s
-- indexes(:namespaces)
-> 0.0059s
-- execute("SET statement_timeout TO 0")
-> 0.0003s
-- remove_index(:namespaces, {:algorithm=>:concurrently, :name=>"index_namespaces_on_traversal_ids_for_groups"})
-> 0.0042s
-- execute("RESET statement_timeout")
-> 0.0004s
== 20211020095357 AddGroupTraversalIdIndex: reverted (0.0123s) ================
DB query
The difference of this MR is that now also type = 'Group'
is used. Bellow is a basic query for getting group's self_and_descendants
.
cold cache: https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/6886/commands/24334
SELECT "namespaces"."id" FROM "namespaces" WHERE "namespaces"."type" = 'Group' AND (traversal_ids @> ('{9970}'))
Time: 750.847 ms
- planning: 2.369 ms
- execution: 748.478 ms
- I/O read: 717.613 ms
- I/O write: 0.000 ms
Shared buffers:
- hits: 1 (~8.00 KiB) from the buffer pool
- reads: 445 (~3.50 MiB) from the OS file cache, including disk I/O
- dirtied: 21 (~168.00 KiB)
- writes: 0
There is about
Screenshots or screen recordings
These are strongly recommended to assist reviewers and reduce the time to merge your change.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.