Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HYC-1956 - script for updating depositors #1113

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading