Add Geo::Fdw::Project registry model to avoid code duplication

With this change we can dry up finders avoiding  code duplication.
parent 72f1f210
......@@ -31,11 +31,9 @@ module Geo
# rubocop:disable CodeReuse/ActiveRecord
def registries_pending_verification
Geo::ProjectRegistry
.joins(Geo::Fdw::GeoNode.fdw_inner_join_projects)
.joins(Geo::Fdw::GeoNode.fdw_inner_join_repository_state)
.where(Geo::Fdw::GeoNode.fdw_registries_pending_verification)
.where(Geo::Fdw::Project.within_shard(shard_name))
Geo::ProjectRegistry.all
.merge(Geo::Fdw::ProjectRegistry.registries_pending_verification)
.merge(Geo::Fdw::ProjectRegistry.within_shards(shard_name))
.limit(batch_size)
end
# rubocop:enable CodeReuse/ActiveRecord
......
......@@ -17,17 +17,25 @@ module Geo
@batch_size = batch_size
end
# rubocop:disable CodeReuse/ActiveRecord
def execute
return Geo::ProjectRegistry.none unless valid_shard?
current_node.project_registries
.joins(Geo::Fdw::GeoNode.fdw_inner_join_projects)
.joins(Geo::Fdw::GeoNode.fdw_inner_join_repository_state)
.where(Geo::Fdw::GeoNode.fdw_registries_pending_verification)
.where(Geo::Fdw::Project.within_shard(shard_name))
.merge(Geo::Fdw::ProjectRegistry.registries_pending_verification)
.merge(Geo::Fdw::ProjectRegistry.within_shards(shard_name))
.limit(batch_size)
end
# rubocop:enable CodeReuse/ActiveRecord
private
attr_reader :current_node, :shard_name,:batch_size
attr_reader :current_node, :shard_name, :batch_size
def valid_shard?
return true unless current_node.selective_sync_by_shards?
current_node.selective_sync_shards.include?(shard_name)
end
end
end
......@@ -14,44 +14,6 @@ module Geo
has_many :geo_node_namespace_links, class_name: 'Geo::Fdw::GeoNodeNamespaceLink'
has_many :namespaces, class_name: 'Geo::Fdw::Namespace', through: :geo_node_namespace_links
def self.fdw_inner_join_projects
project_registries_table = Geo::ProjectRegistry.arel_table
fdw_projects_table = Geo::Fdw::Project.arel_table
project_registries_table
.join(fdw_projects_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_projects_table[:id]))
.join_sources
end
def self.fdw_inner_join_repository_state
project_registries_table = Geo::ProjectRegistry.arel_table
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
project_registries_table
.join(fdw_repository_state_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_repository_state_table[:project_id]))
.join_sources
end
def self.fdw_registries_pending_verification
fdw_repositories_pending_verification.or(fdw_wikis_pending_verification)
end
def self.fdw_repositories_pending_verification
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
Geo::ProjectRegistry.repositories_pending_verification
.and(fdw_repository_state_table[:repository_verification_checksum].not_eq(nil))
end
def self.fdw_wikis_pending_verification
fdw_repository_state_table = Geo::Fdw::ProjectRepositoryState.arel_table
Geo::ProjectRegistry.wikis_pending_verification
.and(fdw_repository_state_table[:wiki_verification_checksum].not_eq(nil))
end
def project_registries
return Geo::ProjectRegistry.all unless selective_sync?
......@@ -67,11 +29,8 @@ module Geo
private
def registries_for_selected_namespaces
query = selected_namespaces_and_descendants
Geo::ProjectRegistry
.joins(self.class.fdw_inner_join_projects)
.where(fdw_projects_table.name => { namespace_id: query.select(:id) })
Geo::Fdw::ProjectRegistry
.within_namespaces(selected_namespaces_and_descendants.select(:id))
end
def selected_namespaces_and_descendants
......@@ -98,13 +57,7 @@ module Geo
end
def registries_for_selected_shards
Geo::ProjectRegistry
.joins(self.class.fdw_inner_join_projects)
.where(fdw_projects_table.name => { repository_storage: selective_sync_shards })
end
def fdw_projects_table
Geo::Fdw::Project.arel_table
Geo::Fdw::ProjectRegistry.within_shards(selective_sync_shards)
end
def fdw_namespaces_table
......
......@@ -7,8 +7,6 @@ module Geo
self.table_name = Gitlab::Geo::Fdw.foreign_table_name('projects')
scope :within_shard, -> (shard_name) { arel_table[:repository_storage].eq(shard_name) }
class << self
# Searches for a list of projects based on the query given in `query`.
#
......
# frozen_string_literal: true
module Geo
module Fdw
class ProjectRegistry
class << self
def registries_pending_verification
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.joins(fdw_inner_join_repository_state)
.where(repositories_pending_verification.or(wikis_pending_verification))
end
def within_namespaces(namespace_ids)
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.where(fdw_projects_table.name => { namespace_id: namespace_ids })
end
def within_shards(shard_names)
Geo::ProjectRegistry
.joins(fdw_inner_join_projects)
.where(fdw_projects_table.name => { repository_storage: Array(shard_names) })
end
private
def project_registries_table
Geo::ProjectRegistry.arel_table
end
def fdw_projects_table
Geo::Fdw::Project.arel_table
end
def fdw_repository_state_table
Geo::Fdw::ProjectRepositoryState.arel_table
end
def fdw_inner_join_projects
project_registries_table
.join(fdw_projects_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_projects_table[:id]))
.join_sources
end
def fdw_inner_join_repository_state
project_registries_table
.join(fdw_repository_state_table, Arel::Nodes::InnerJoin)
.on(project_registries_table[:project_id].eq(fdw_repository_state_table[:project_id]))
.join_sources
end
def repositories_pending_verification
Geo::ProjectRegistry.repositories_pending_verification
.and(fdw_repository_state_table[:repository_verification_checksum].not_eq(nil))
end
def wikis_pending_verification
Geo::ProjectRegistry.wikis_pending_verification
.and(fdw_repository_state_table[:wiki_verification_checksum].not_eq(nil))
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