-
Notifications
You must be signed in to change notification settings - Fork 8
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
Adds Fedora record removal to DeletePhysicalInstantiations fix #925
Open
afred
wants to merge
1
commit into
develop
Choose a base branch
from
854-more-more-more-delete-Fedora-hangers-on
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,68 @@ | |
|
||
module Fix | ||
class DeleteAssetResources < BatchProcess | ||
|
||
def run | ||
asset_resources.each do |ar| | ||
log.info "Destroying Asset Resource #{ar.id}" | ||
begin | ||
Hyrax.persister.delete(resource: ar) | ||
Hyrax.index_adapter.delete(resource: ar) | ||
Hyrax.index_adapter.connection.commit | ||
log.info "Asset Resource #{ar.id} destroyed." | ||
rescue => e | ||
log_error e | ||
end | ||
log.info "Deleting #{asset_resources.count} Asset Resources..." | ||
asset_resources.map do |ar| | ||
destroy_work_and_members(resource: ar) | ||
end | ||
log.info "Done." | ||
log.info report | ||
end | ||
|
||
private | ||
|
||
|
||
def results; @results ||= []; end | ||
|
||
def work_destroy_transaction | ||
Hyrax::Transactions::WorkDestroy.new | ||
end | ||
|
||
def destroy_work_and_members(resource:) | ||
result = { resource: resource } | ||
resource.members.each do |member| | ||
resource.members.delete(member) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain the purpose of this line? (28) |
||
destroy_work_and_members(resource: member) | ||
end | ||
log.info "DELETING #{resource.class} #{resource.id.id}..." | ||
work_destroy_transaction.call(resource) | ||
log.info "DELETED #{resource.class} #{resource.id.id}." | ||
delete_from_fedora(resource) | ||
result[:error] = false | ||
results << result | ||
rescue => e | ||
result[:error] = e | ||
results << result | ||
log_error e | ||
end | ||
|
||
def delete_from_fedora(resource) | ||
log.info "SEARCHING FEDORA: #{resource.class} #{resource.id}..." | ||
af_object = ActiveFedora::Base.find(resource.id.to_s) | ||
af_object.destroy! | ||
log.info "DELETED: #{af_object.class} #{af_object.id} from Fedora." | ||
rescue ActiveFedora::ObjectNotFoundError | ||
# Not a real error, just means it's not in Fedora. Carry on. | ||
log.info "NOT FOUND." | ||
end | ||
|
||
def report | ||
r = "\nRESULTS:\n" | ||
r += "Successfully Deleted #{successes.count}:\n" | ||
successes.each do |result| | ||
r += "#{result[:resource].id}, #{result[:resource].class}\n" | ||
end | ||
puts "Done." | ||
r += "Failed while Deleting #{failures.count}:\n" | ||
failures.each do |result| | ||
r += "#{result[:resource].id}, #{result[:error]}\n" | ||
end | ||
r | ||
end | ||
|
||
def successes; results.reject { |r| r[:error] }; end | ||
def failures; results.select { |r| r[:error] }; end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# first shell into a running container with bash, then run pry. | ||
# load the app | ||
require_relative '../../config/environment' | ||
|
||
# Set some variables | ||
class SampleData | ||
attr_reader :asset_sample_size, :log | ||
|
||
def initialize(asset_sample_size: 100) | ||
@asset_sample_size = asset_sample_size | ||
@log = Logger.new(STDOUT) | ||
end | ||
|
||
def solr | ||
@solr ||= Blacklight.default_index.connection | ||
end | ||
|
||
def a_docs | ||
@a_docs ||= solr.get('select', params: {q: 'has_model_ssim:Asset', rows: asset_sample_size, start: rand_start})['response']['docs'] | ||
end | ||
|
||
def rand_start | ||
@rand_start ||= rand(0..(total_assets - asset_sample_size - 1)) | ||
end | ||
|
||
def total_assets | ||
solr.get('select', params: {q: 'has_model_ssim:Asset', rows: 0})['response']['numFound'] | ||
end | ||
|
||
def ars(refresh: false) | ||
# Rest instance variables if refresh is true | ||
@ars = @ars_with_pids = @ars_with_pis_in_fedora = nil if refresh | ||
@ars ||= a_docs.map{|doc| AssetResource.find(doc['id'])} | ||
end | ||
|
||
def ars_with_pis(media_type: nil) | ||
@ars_with_pis ||= ars.select{ |ar| ar.physical_instantiation_resources.present? } | ||
if media_type | ||
filter(ars: @ars_with_pis, media_type: media_type) | ||
else | ||
@ars_with_pis | ||
end | ||
end | ||
|
||
def ars_with_pis_in_fedora(media_type: nil) | ||
@ars_with_pis_in_fedora ||= ars_with_pis.select do |ar| | ||
ar.physical_instantiation_resources.any? do |pi| | ||
begin | ||
pi = PhysicalInstantiation.find(pi.id) | ||
rescue ActiveFedora::ObjectNotFoundError, Ldp::Gone => e | ||
log.error("#{e.class}: #{e.message}") | ||
false | ||
end | ||
end | ||
end | ||
|
||
if media_type | ||
filter(ars: @ars_with_pis_in_fedora, media_type: media_type) | ||
else | ||
@ars_with_pis_in_fedora | ||
end | ||
end | ||
|
||
def save_assets_to_pg | ||
ars.each do |ar| | ||
Hyrax.persister.save(resource: ar) | ||
end | ||
ars(refresh: true) | ||
end | ||
|
||
# Class method for filtering a set of AssetResources | ||
def self.filter(ars: [], media_type: nil) | ||
filtered = ars | ||
if media_type | ||
filtered = filtered.select do |ar| | ||
ar.physical_instantiation_resources.any? do |pi| | ||
pi.media_type.to_s.strip.downcase == media_type.to_s.strip.downcase | ||
end | ||
end | ||
end | ||
filtered | ||
end | ||
|
||
# Delegated instance method | ||
def filter(*args, **kwargs); self.class.filter(*args, **kwargs); end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain more about what's happening on these lines and why?