Fix CodeReuse/ActiveRecord violation

parent 82e15524
...@@ -84,7 +84,7 @@ module Geo ...@@ -84,7 +84,7 @@ module Geo
def find_retryable_failed_registries(batch_size:, except_artifact_ids: []) def find_retryable_failed_registries(batch_size:, except_artifact_ids: [])
find_failed_registries find_failed_registries
.retry_due .retry_due
.where.not(artifact_id: except_artifact_ids) .artifact_id_not_in(except_artifact_ids)
.limit(batch_size) .limit(batch_size)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -93,7 +93,7 @@ module Geo ...@@ -93,7 +93,7 @@ module Geo
def find_retryable_synced_missing_on_primary_registries(batch_size:, except_artifact_ids: []) def find_retryable_synced_missing_on_primary_registries(batch_size:, except_artifact_ids: [])
find_synced_missing_on_primary_registries find_synced_missing_on_primary_registries
.retry_due .retry_due
.where.not(artifact_id: except_artifact_ids) .artifact_id_not_in(except_artifact_ids)
.limit(batch_size) .limit(batch_size)
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -154,67 +154,45 @@ module Geo ...@@ -154,67 +154,45 @@ module Geo
Geo::JobArtifactRegistry.failed Geo::JobArtifactRegistry.failed
end end
#
# FDW accessors
#
# rubocop: disable CodeReuse/ActiveRecord
def fdw_find def fdw_find
job_artifacts.joins("INNER JOIN job_artifact_registry ON job_artifact_registry.artifact_id = #{fdw_table}.id") job_artifacts
.inner_join_job_artifact_registry
.geo_syncable .geo_syncable
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def fdw_find_unsynced(except_artifact_ids:) def fdw_find_unsynced(except_artifact_ids:)
job_artifacts.joins("LEFT OUTER JOIN job_artifact_registry job_artifacts
ON job_artifact_registry.artifact_id = #{fdw_table}.id") .missing_job_artifact_registry
.geo_syncable .geo_syncable
.where(job_artifact_registry: { artifact_id: nil }) .id_not_in(except_artifact_ids)
.where.not(id: except_artifact_ids)
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def fdw_find_migrated_local(except_artifact_ids:) def fdw_find_migrated_local(except_artifact_ids:)
job_artifacts.joins("INNER JOIN job_artifact_registry ON job_artifact_registry.artifact_id = #{fdw_table}.id") job_artifacts
.inner_join_job_artifact_registry
.with_files_stored_remotely .with_files_stored_remotely
.where.not(id: except_artifact_ids) .id_not_in(except_artifact_ids)
.merge(Geo::JobArtifactRegistry.all) .merge(Geo::JobArtifactRegistry.all)
end end
# rubocop: enable CodeReuse/ActiveRecord
def fdw_table
Geo::Fdw::Ci::JobArtifact.table_name
end
#
# Legacy accessors (non FDW)
#
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_synced def legacy_find_synced
legacy_inner_join_registry_ids( legacy_inner_join_registry_ids(
syncable, syncable,
find_synced_registries.pluck(:artifact_id), find_synced_registries.pluck_artifact_key,
Ci::JobArtifact Ci::JobArtifact
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_failed def legacy_find_failed
legacy_inner_join_registry_ids( legacy_inner_join_registry_ids(
syncable, syncable,
find_failed_registries.pluck(:artifact_id), find_failed_registries.pluck_artifact_key,
Ci::JobArtifact Ci::JobArtifact
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_unsynced(except_artifact_ids:) def legacy_find_unsynced(except_artifact_ids:)
registry_artifact_ids = Geo::JobArtifactRegistry.pluck(:artifact_id) | except_artifact_ids registry_artifact_ids = Geo::JobArtifactRegistry.pluck_artifact_key | except_artifact_ids
legacy_left_outer_join_registry_ids( legacy_left_outer_join_registry_ids(
syncable, syncable,
...@@ -222,11 +200,9 @@ module Geo ...@@ -222,11 +200,9 @@ module Geo
Ci::JobArtifact Ci::JobArtifact
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_migrated_local(except_artifact_ids:) def legacy_find_migrated_local(except_artifact_ids:)
registry_artifact_ids = Geo::JobArtifactRegistry.pluck(:artifact_id) - except_artifact_ids registry_artifact_ids = Geo::JobArtifactRegistry.pluck_artifact_key - except_artifact_ids
legacy_inner_join_registry_ids( legacy_inner_join_registry_ids(
legacy_finder.job_artifacts.with_files_stored_remotely, legacy_finder.job_artifacts.with_files_stored_remotely,
...@@ -234,16 +210,13 @@ module Geo ...@@ -234,16 +210,13 @@ module Geo
Ci::JobArtifact Ci::JobArtifact
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def legacy_find_synced_missing_on_primary def legacy_find_synced_missing_on_primary
legacy_inner_join_registry_ids( legacy_inner_join_registry_ids(
syncable, syncable,
find_synced_missing_on_primary_registries.pluck(:artifact_id), find_synced_missing_on_primary_registries.pluck_artifact_key,
Ci::JobArtifact Ci::JobArtifact
) )
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
...@@ -15,6 +15,37 @@ module Geo ...@@ -15,6 +15,37 @@ module Geo
scope :not_expired, -> { where('expire_at IS NULL OR expire_at > ?', Time.current) } scope :not_expired, -> { where('expire_at IS NULL OR expire_at > ?', Time.current) }
scope :geo_syncable, -> { with_files_stored_locally.not_expired } scope :geo_syncable, -> { with_files_stored_locally.not_expired }
scope :project_id_in, ->(ids) { joins(:project).merge(Geo::Fdw::Project.id_in(ids)) } scope :project_id_in, ->(ids) { joins(:project).merge(Geo::Fdw::Project.id_in(ids)) }
class << self
def inner_join_job_artifact_registry
join_statement =
arel_table
.join(job_artifact_registry_table, Arel::Nodes::InnerJoin)
.on(arel_table[:id].eq(job_artifact_registry_table[:artifact_id]))
joins(join_statement.join_sources)
end
def missing_job_artifact_registry
left_outer_join_job_artifact_registry
.where(job_artifact_registry_table[:id].eq(nil))
end
private
def job_artifact_registry_table
Geo::JobArtifactRegistry.arel_table
end
def left_outer_join_job_artifact_registry
join_statement =
arel_table
.join(job_artifact_registry_table, Arel::Nodes::OuterJoin)
.on(arel_table[:id].eq(job_artifact_registry_table[:artifact_id]))
joins(join_statement.join_sources)
end
end
end end
end end
end end
......
...@@ -2,4 +2,12 @@ ...@@ -2,4 +2,12 @@
class Geo::JobArtifactRegistry < Geo::BaseRegistry class Geo::JobArtifactRegistry < Geo::BaseRegistry
include Geo::Syncable include Geo::Syncable
def self.artifact_id_not_in(ids)
where.not(artifact_id: ids)
end
def self.pluck_artifact_key
where(nil).pluck(:artifact_id)
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