Improve legacy query to retrieve projects when selective sync is enabled

parent e529673f
......@@ -14,4 +14,51 @@ module Geo::SelectiveSync
def selective_sync_by_shards?
selective_sync_type == 'shards'
end
private
def selected_namespaces_and_descendants
relation = selected_namespaces_and_descendants_cte.apply_to(namespaces_model.all)
relation.extend(Gitlab::Database::ReadOnlyRelation)
relation
end
def selected_namespaces_and_descendants_cte
cte = Gitlab::SQL::RecursiveCTE.new(:base_and_descendants)
cte << geo_node_namespace_links
.select(geo_node_namespace_links_table[:namespace_id].as('id'))
.except(:order)
# Recursively get all the descendants of the base set.
cte << namespaces_model
.select(namespaces_table[:id])
.from([namespaces_table, cte.table])
.where(namespaces_table[:parent_id].eq(cte.table[:id]))
.except(:order)
cte
end
# This concern doesn't define a namespaces relation. That's done in ::GeoNode
# or ::Geo::Fdw::GeoNode respectively. So when we use the same code from the
# two places, they act differently - the first doesn't use FDW, the second does.
def namespaces_model
namespaces.model
end
# This concern doesn't define a namespaces relation. That's done in ::GeoNode
# or ::Geo::Fdw::GeoNode respectively. So when we use the same code from the
# two places, they act differently - the first doesn't use FDW, the second does.
def namespaces_table
namespaces.arel_table
end
# This concern doesn't define a geo_node_namespace_links relation. That's
# done in ::GeoNode or ::Geo::Fdw::GeoNode respectively. So when we use the
# same code from the two places, they act differently - the first doesn't
# use FDW, the second does.
def geo_node_namespace_links_table
geo_node_namespace_links.arel_table
end
end
......@@ -71,37 +71,6 @@ module Geo
Gitlab::Geo::Fdw::ProjectRegistryQueryBuilder.new
.within_shards(selective_sync_shards)
end
def selected_namespaces_and_descendants
relation = selected_namespaces_and_descendants_cte.apply_to(Geo::Fdw::Namespace.all)
relation.extend(Gitlab::Database::ReadOnlyRelation)
relation
end
def selected_namespaces_and_descendants_cte
cte = Gitlab::SQL::RecursiveCTE.new(:base_and_descendants)
cte << geo_node_namespace_links
.select(fdw_geo_node_namespace_links_table[:namespace_id].as('id'))
.except(:order)
# Recursively get all the descendants of the base set.
cte << Geo::Fdw::Namespace
.select(fdw_namespaces_table[:id])
.from([fdw_namespaces_table, cte.table])
.where(fdw_namespaces_table[:parent_id].eq(cte.table[:id]))
.except(:order)
cte
end
def fdw_namespaces_table
Geo::Fdw::Namespace.arel_table
end
def fdw_geo_node_namespace_links_table
Geo::Fdw::GeoNodeNamespaceLink.arel_table
end
end
end
end
......@@ -204,10 +204,9 @@ class GeoNode < ApplicationRecord
return Project.all unless selective_sync?
if selective_sync_by_namespaces?
query = Gitlab::ObjectHierarchy.new(namespaces).base_and_descendants
Project.in_namespace(query.select(:id))
projects_for_selected_namespaces
elsif selective_sync_by_shards?
Project.within_shards(selective_sync_shards)
projects_for_selected_shards
else
Project.none
end
......@@ -316,4 +315,12 @@ class GeoNode < ApplicationRecord
"#{value}/"
end
def projects_for_selected_namespaces
Project.where(Project.arel_table.name => { namespace_id: selected_namespaces_and_descendants.select(:id) })
end
def projects_for_selected_shards
Project.within_shards(selective_sync_shards)
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