Commit 7303af24 authored by Luke Duncalfe's avatar Luke Duncalfe

Add LfsObject.for_oids scope

Replacing instances of LfsObject.where(oid:) with the new scope.

Allows us to remove some rubocop: disable CodeReuse/ActiveRecord
pragma comment.
parent bb40d720
......@@ -11,6 +11,7 @@ class LfsObject < ApplicationRecord
scope :with_files_stored_locally, -> { where(file_store: LfsObjectUploader::Store::LOCAL) }
scope :with_files_stored_remotely, -> { where(file_store: LfsObjectUploader::Store::REMOTE) }
scope :for_oids, -> (oids) { where(oid: oids) }
validates :oid, presence: true, uniqueness: true
......
......@@ -25,7 +25,6 @@ module Projects
private
# rubocop: disable CodeReuse/ActiveRecord
def link_existing_lfs_objects(oids)
linked_existing_objects = []
iterations = 0
......@@ -33,7 +32,7 @@ module Projects
oids.each_slice(BATCH_SIZE) do |oids_batch|
# Load all existing LFS Objects immediately so we don't issue an extra
# query for the `.any?`
existent_lfs_objects = LfsObject.where(oid: oids_batch).load
existent_lfs_objects = LfsObject.for_oids(oids_batch).load
next unless existent_lfs_objects.any?
rows = existent_lfs_objects
......@@ -49,7 +48,6 @@ module Projects
linked_existing_objects
end
# rubocop: enable CodeReuse/ActiveRecord
def log_lfs_link_results(lfs_objects_linked_count, iterations)
Gitlab::Import::Logger.info(
......
......@@ -9,7 +9,6 @@ module Gitlab
@time_left = time_left
end
# rubocop: disable CodeReuse/ActiveRecord
def objects_missing?
return false unless @newrev && @project.lfs_enabled?
......@@ -19,12 +18,11 @@ module Gitlab
return false unless new_lfs_pointers.present?
existing_count = @project.all_lfs_objects
.where(oid: new_lfs_pointers.map(&:lfs_oid))
.for_oids(new_lfs_pointers.map(&:lfs_oid))
.count
existing_count != new_lfs_pointers.count
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
end
......@@ -13,6 +13,15 @@ describe LfsObject do
expect(described_class.not_linked_to_project(project)).to contain_exactly(other_lfs_object)
end
end
describe 'for_oids' do
it 'returns the correct LfsObjects' do
lfs_object_1, lfs_object_2 = create_list(:lfs_object, 2)
expect(described_class.for_oids(lfs_object_1.oid)).to contain_exactly(lfs_object_1)
expect(described_class.for_oids([lfs_object_1.oid, lfs_object_2.oid])).to contain_exactly(lfs_object_1, lfs_object_2)
end
end
end
it 'has a distinct has_many :projects relation through lfs_objects_projects' do
......
......@@ -60,8 +60,8 @@ describe Projects::LfsPointers::LfsLinkService do
stub_const("#{described_class}::BATCH_SIZE", 1)
oids = %w(one two)
expect(LfsObject).to receive(:where).with(oid: %w(one)).once.and_call_original
expect(LfsObject).to receive(:where).with(oid: %w(two)).once.and_call_original
expect(LfsObject).to receive(:for_oids).with(%w(one)).once.and_call_original
expect(LfsObject).to receive(:for_oids).with(%w(two)).once.and_call_original
subject.execute(oids)
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