Skip to content

Commit

Permalink
update CollectionType GlobalID update task to use valkyrie (#6242)
Browse files Browse the repository at this point in the history
* update CollectionType GlobalID update task to use valkyrie

* Move force arg of collection_type_gid= to thread local var

PcdmCollection cannot handle the `force: true` argument supplied by the rake task, so move it to a thread local var for this special case. This is rather hacky, but avoids using a property and cluttering the data model. Using an instance variable to store the force state does not work in wings because it rebuilds the object from attributes when persisting.

---------

Co-authored-by: Daniel Pierce <[email protected]>
  • Loading branch information
tamsin johnson and dlpierce authored Sep 13, 2023
1 parent 2d2f11a commit 28cc5f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
6 changes: 4 additions & 2 deletions app/models/concerns/hyrax/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ module CollectionBehavior
validates :collection_type_gid, presence: true

# Need to define here in order to override setter defined by ActiveTriples
def collection_type_gid=(new_collection_type_gid, force: false)
def collection_type_gid=(new_collection_type_gid)
new_collection_type_gid = new_collection_type_gid&.to_s
raise "Can't modify collection type of this collection" if !force && persisted? && !collection_type_gid_was.nil? && collection_type_gid_was != new_collection_type_gid
raise "Can't modify collection type of this collection" if
!Thread.current[:force_collection_type_gid] && # Used by update_collection_type_global_ids rake task
persisted? && !collection_type_gid_was.nil? && collection_type_gid_was != new_collection_type_gid
new_collection_type = Hyrax::CollectionType.find_by_gid!(new_collection_type_gid)
super(new_collection_type_gid)
@collection_type = new_collection_type
Expand Down
13 changes: 9 additions & 4 deletions lib/tasks/collection_type_global_id.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ namespace :hyrax do

count = 0

Collection.all.each do |collection|
next if collection.collection_type_gid == collection.collection_type.to_global_id.to_s
Hyrax.query_service.find_all_of_model(model: Hyrax::PcdmCollection).each do |collection|
type = Hyrax::CollectionType.find_by_gid(collection.collection_type_gid)
next if collection.collection_type_gid == type.to_global_id.to_s

collection.public_send(:collection_type_gid=, collection.collection_type.to_global_id, force: true)
# Awful hack to allow converted AF collections to force update collection_type_gid
Thread.current[:force_collection_type_gid] = true
collection.collection_type_gid = type.to_global_id

collection.save &&
Hyrax.persister.save(resource: collection) &&
count += 1
ensure
Thread.current[:force_collection_type_gid] = false
end

puts "Updated #{count} collections."
Expand Down
12 changes: 7 additions & 5 deletions spec/tasks/rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,20 @@
let(:other_collection_type) { FactoryBot.create(:collection_type) }

let(:collections_with_legacy_gids) do
[FactoryBot.create(:collection, collection_type_gid: "gid://internal/sometext/#{collection_type.id}"),
FactoryBot.create(:collection, collection_type_gid: "gid://internal/sometext/#{other_collection_type.id}")]
[FactoryBot.valkyrie_create(:hyrax_collection, collection_type_gid: "gid://internal/sometext/#{collection_type.id}"),
FactoryBot.valkyrie_create(:hyrax_collection, collection_type_gid: "gid://internal/sometext/#{other_collection_type.id}")]
end

before do
FactoryBot.create_list(:collection, 3, collection_type_gid: collection_type.to_global_id)
FactoryBot.create_list(:collection, 3, collection_type_gid: other_collection_type.to_global_id)
3.times do
FactoryBot.valkyrie_create(:hyrax_collection, collection_type_gid: collection_type.to_global_id)
FactoryBot.valkyrie_create(:hyrax_collection, collection_type_gid: other_collection_type.to_global_id)
end
end

it 'updates collections to use standard GlobalId URI' do
expect { run_task 'hyrax:collections:update_collection_type_global_ids' }
.to change { collections_with_legacy_gids.map { |col| col.reload.collection_type_gid } }
.to change { collections_with_legacy_gids.map { |col| Hyrax.query_service.find_by(id: col.id).collection_type_gid } }
.to eq [collection_type.to_global_id.to_s, other_collection_type.to_global_id.to_s]
end
end
Expand Down

0 comments on commit 28cc5f1

Please sign in to comment.