Skip to content

Commit

Permalink
Add script for updating depositors
Browse files Browse the repository at this point in the history
  • Loading branch information
bbpennel committed Aug 6, 2024
1 parent 127acd4 commit 8cf2364
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
49 changes: 49 additions & 0 deletions app/services/tasks/set_depositor_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true
module Tasks
require 'tasks/migration_helper'
# Sets the depositor for a list of objects
class SetDepositorService
def self.run(id_list_file, depositor_id)
SetDepositorService.new(id_list_file, depositor_id).run
end

def initialize(id_list_file, depositor_id)
@id_list_file = id_list_file
@depositor_id = depositor_id
end

def run
logger.info("Updating #{object_id_list.count} objects to have depositor #{@depositor_id}")
object_id_list.each do |object_id|
start_time = Time.now
object_id = object_id.chomp
begin
target = ActiveFedora::Base.find(object_id)
target.depositor = depositor.user_key
target.save!
logger.info("Updated depositor of #{object_id} to #{@depositor_id} in #{Time.now - start_time}")
rescue Ldp::Gone
logger.warn("Skipped deleted object #{object_id}")
rescue StandardError => e
logger.error("Failed to update #{object_id}:")
logger.error(e)
end
end
end

def object_id_list
@object_id_list ||= File.readlines(@id_list_file)
end

def depositor
@depositor ||= User.find_by(uid: @depositor_id)
end

def logger
@logger ||= begin
log_path = File.join(Rails.configuration.log_directory, 'set_depositor.log')
Logger.new(log_path, progname: 'set_depositor')
end
end
end
end
6 changes: 6 additions & 0 deletions lib/tasks/set_depositor.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
desc 'Update the depositor of a list of objects'
task :set_depositor, [:id_list_file, :depositor_id] => [:environment] do |_t, args|
Rails.logger.info('Prepapring to run SetDepositorService')
Tasks::SetDepositorService.run(args[:id_list_file], args[:depositor_id])
end
96 changes: 96 additions & 0 deletions spec/services/tasks/set_depositor_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true
require 'rails_helper'
include Warden::Test::Helpers

describe Tasks::SetDepositorService, :clean do
before do
ActiveFedora::Cleaner.clean!
end

let(:depositor1) { FactoryBot.create(:user) }
let(:depositor2) { FactoryBot.create(:user) }
let(:depositor3) { FactoryBot.create(:user) }

let(:admin_set) do
AdminSet.create(title: ['test admin set'],
description: ['some description'])
end

let(:permission_template) do
Hyrax::PermissionTemplate.create!(source_id: admin_set.id)
end

let(:workflow) do
Sipity::Workflow.create(name: 'test', allows_access_grant: true, active: true,
permission_template_id: permission_template.id)
end

let(:work1) do
Article.create(title: ['Test Article 1'],
depositor: depositor1.user_key,
creators_attributes: [{ name: 'Person, Test',
affiliation: 'Department of Biology' }])
end

let(:work2) do
Article.create(title: ['Test Article 2'],
depositor: depositor2.user_key,
creators_attributes: [{ name: 'Person, Test',
affiliation: 'Department of Biology' }])
end

let(:id_list_file) { Tempfile.new }

after do
id_list_file.unlink
end

describe '#run' do
context 'with a list of existing object ids' do
before do
File.write(id_list_file, "#{work1.id}\n#{work2.id}")
end

it 'updates depositor of a list of objects' do
Tasks::SetDepositorService.run(id_list_file.path, depositor3.user_key)

result_work1 = Article.find(work1.id)
result_work2 = Article.find(work2.id)

expect(result_work1.depositor).to eq depositor3.user_key
expect(result_work2.depositor).to eq depositor3.user_key
end
end

context 'with objects that have been deleted' do
before do
File.write(id_list_file, "#{work1.id}\n#{work2.id}")
work1.destroy
end

it 'updates depositor of the undeleted object' do
Tasks::SetDepositorService.run(id_list_file.path, depositor3.user_key)

result_work2 = Article.find(work2.id)

expect(result_work2.depositor).to eq depositor3.user_key
end
end

context 'object retrieval throws unexpected error' do
before do
File.write(id_list_file, "#{work1.id}\n#{work2.id}")
allow(ActiveFedora::Base).to receive(:find).with(work1.id).and_raise(ActiveFedora::ModelMismatch)
allow(ActiveFedora::Base).to receive(:find).with(work2.id).and_return(work2)
end

it 'updates depositor of the undeleted object' do
Tasks::SetDepositorService.run(id_list_file.path, depositor3.user_key)

result_work2 = Article.find(work2.id)

expect(result_work2.depositor).to eq depositor3.user_key
end
end
end
end

0 comments on commit 8cf2364

Please sign in to comment.