Skip to content

Commit

Permalink
add a rake task to update collection references to collection types
Browse files Browse the repository at this point in the history
as part of #4696: since we're deprecating our older semi-custom global id
syntax, we need to be able to support applications updating their collections.

the rake task `hyrax:collections:update_collection_type_global_ids` provides a
way to do this, and is a non-destructive if collections all use the standard
syntax.

note that this task may create substantial load, even if all collections are
already up to date; so it shouldn't be run repeatedly.
  • Loading branch information
tamsin johnson committed Jan 6, 2021
1 parent ef8c559 commit 97093fe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/models/concerns/hyrax/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,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)
def collection_type_gid=(new_collection_type_gid, force: false)
new_collection_type_gid = new_collection_type_gid&.to_s
raise "Can't modify collection type of this collection" if persisted? && !collection_type_gid_was.nil? && collection_type_gid_was != new_collection_type_gid
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
new_collection_type = Hyrax::CollectionType.find_by_gid!(new_collection_type_gid)
super
super(new_collection_type_gid)
@collection_type = new_collection_type
collection_type_gid
end
Expand Down
18 changes: 18 additions & 0 deletions lib/tasks/collection_type_global_id.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true
namespace :hyrax do
namespace :collections do
desc 'Update CollectionType global id references for Hyrax 3.0.0'
task :update_collection_type_global_ids do
count = Collection.all.each_with_object(0) do |collection, updated_count|
next if collection.collection_type_gid == collection.collection_type.to_global_id

collection.public_send(:collection_type_gid=, collection.collection_type.to_global_id, force: true)

collection.save &&
updated_count += 1
end

puts "Updated #{count} collections."
end
end
end
35 changes: 35 additions & 0 deletions spec/tasks/rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,39 @@
File.delete("abc123.txt")
end
end

describe 'hyrax:collections', :clean_repo do
describe ':update_collection_type_global_ids' do
before do
load_rake_environment [File.expand_path('../../../lib/tasks/collection_type_global_id.rake', __FILE__)]
end

context 'with no collections' do
it 'outputs that 0 collections were updated' do
run_task 'hyrax:collections:update_collection_type_global_ids'
end
end

context 'with collections' do
let(:collection_type) { FactoryBot.create(:collection_type) }
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}")]
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)
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 eq [collection_type.to_global_id.to_s, other_collection_type.to_global_id.to_s]
end
end
end
end
end

0 comments on commit 97093fe

Please sign in to comment.