Skip to content

Commit

Permalink
feat: add a new method in deleter class to to delete collection with …
Browse files Browse the repository at this point in the history
…all its works (#938)
  • Loading branch information
pghorpade authored May 25, 2023
1 parent 2ab0580 commit 6effaf3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/lib/californica/deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,37 @@ def delete
destroy_and_eradicate
end

def delete_collection_with_works(of_type: nil)
# replace log to puts when running locally
log('In delete_collection_with_works start.')
delete_works(of_type: of_type)
delete if of_type.nil? || record.is_a?(of_type)
log('In delete_collection_with_works stop.')
end

def delete_works(of_type: nil)
# log('In delete_works start.')
work_id_list&.each do |work_id|
Californica::Deleter.new(id: work_id, logger: logger)
.delete_with_children(of_type: of_type)
end
# log('In delete_works end.')
end

def delete_with_children(of_type: nil)
# log('In delete_with_children start.')
delete_children(of_type: of_type)
delete if of_type.nil? || record.is_a?(of_type)
# log('In delete_with_children end.')
end

def delete_children(of_type: nil)
# log('In delete_children start.')
record&.member_ids&.each do |child_id|
Californica::Deleter.new(id: child_id, logger: logger)
.delete_with_children(of_type: of_type)
end
# log('In delete_children end.')
end

private
Expand All @@ -37,6 +58,7 @@ def destroy_and_eradicate
record&.destroy&.eradicate
Hyrax.config.callback.run(:after_destroy, record.id, User.batch_user)
log("Deleted #{record.class} #{record.id} in #{ActiveSupport::Duration.build(Time.current - start_time)}")
log("deleted item ark is: #{record.ark}")
rescue Ldp::HttpError, Faraday::TimeoutError, Faraday::ConnectionFailed => e
log("#{e.class}: #{e.message}")
retries ||= 0
Expand Down Expand Up @@ -69,5 +91,16 @@ def record
def record_name
@record ? "#{record.class} #{record.ark}" : id
end

def solr
Blacklight.default_index.connection
end

# Get the list of IDs from the query results:
def work_id_list
query = { params: { q: "member_of_collection_ids_ssim:#{id} AND has_model_ssim:Work", fl: "id", rows: "100000" } }
results = solr.select(query)
results['response']['docs'].flat_map(&:values)
end
end
end
5 changes: 5 additions & 0 deletions lib/tasks/delete.rake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ namespace :californica do
puts('Done!')
end

task delete_collection: [:environment] do
Californica::Deleter.new(id: ENV.fetch('DELETE_COLLECTION_ID')).delete_collection_with_works
puts('Done!')
end

task delete_all_child_works: [:environment] do
DeleteAllChildWorksJob.perform_later unless Flipflop.child_works?
end
Expand Down

0 comments on commit 6effaf3

Please sign in to comment.