Skip to content

Commit a9c8e9c

Browse files
authored
move valkyrie resource migration code from hyku to hyrax (#6973)
* move valkyrie resource migration code from hyku to hyrax * prevent works from becoming inaccessible during valkyrie transition * more cross compatible fix for collection thumbnail issue * make adminset wings identifiable
1 parent 8548a92 commit a9c8e9c

File tree

10 files changed

+140
-6
lines changed

10 files changed

+140
-6
lines changed

app/controllers/concerns/hyrax/works_controller_behavior.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ def hash_key_for_curation_concern
263263

264264
def contextual_path(presenter, parent_presenter)
265265
::Hyrax::ContextualPath.new(presenter, parent_presenter).show
266+
rescue NoMethodError
267+
''
266268
end
267269

268270
##

app/forms/hyrax/forms/pcdm_collection_form.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ def display_additional_fields?
8181
secondary_terms.any?
8282
end
8383

84+
##
85+
# This feature is not supported in Valkyrie collections and should be removed as part of #5764
86+
# However, the depreciated method is still needed for some specs
87+
# @return [] always empty.
88+
def select_files
89+
Deprecation.warn "`Hyrax::PcdmCollection` does not currently support thumbnail_id. Collection thumbnails need to be redesigned as part of issue #5764"
90+
[]
91+
end
92+
8493
private
8594

8695
def _form_field_definitions

app/jobs/migrate_resources_job.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
# migrates models from AF to valkyrie
4+
class MigrateResourcesJob < ApplicationJob
5+
attr_writer :errors
6+
# input [Array>>String] Array of ActiveFedora model names to migrate to valkyrie objects
7+
# defaults to AdminSet & Collection models if empty
8+
def perform(ids: [], models: ['AdminSet', 'Collection'])
9+
if ids.blank?
10+
models.each do |model|
11+
model.constantize.find_each do |item|
12+
migrate(item.id)
13+
end
14+
end
15+
else
16+
ids.each do |id|
17+
migrate(id)
18+
end
19+
end
20+
raise errors.inspect if errors.present?
21+
end
22+
23+
def errors
24+
@errors ||= []
25+
end
26+
27+
def migrate(id)
28+
resource = Hyrax.query_service.find_by(id: id)
29+
return unless resource.wings? # this resource has already been converted
30+
result = MigrateResourceService.new(resource: resource).call
31+
errors << result unless result.success?
32+
result
33+
end
34+
end

app/jobs/valkyrie_create_derivatives_job.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def perform(file_set_id, file_id, _filepath = nil)
1616

1717
def reindex_parent(file_set_id)
1818
file_set = Hyrax.query_service.find_by(id: file_set_id)
19+
return unless file_set
1920
parent = Hyrax.custom_queries.find_parent_work(resource: file_set)
20-
return unless parent.thumbnail_id == file_set.id
21+
return unless parent&.thumbnail_id == file_set.id
2122
Hyrax.logger.debug { "Reindexing #{parent.id} due to creation of thumbnail derivatives." }
2223
Hyrax.index_adapter.save(resource: parent)
2324
end

app/models/admin_set.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# @see Hyrax::DefaultAdminSetActor
1818
# @see Hyrax::ApplyPermissionTemplateActor
1919
class AdminSet < ActiveFedora::Base
20+
include Hydra::PCDM::CollectionBehavior
2021
include Hydra::AccessControls::Permissions
2122
include Hyrax::Noid
2223
include Hyrax::HumanReadableType

app/models/hyrax/resource.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def work?
149149
self.class.work?
150150
end
151151

152+
# Its nice to know if a record is still in AF or not
153+
def wings?
154+
respond_to?(:head) && respond_to?(:tail)
155+
end
156+
152157
def ==(other)
153158
attributes.except(:created_at, :updated_at) == other.attributes.except(:created_at, :updated_at) if other.respond_to?(:attributes)
154159
end
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
# migrates models from AF to valkyrie
4+
class MigrateResourceService
5+
attr_accessor :resource
6+
def initialize(resource:)
7+
@resource = resource
8+
end
9+
10+
def model
11+
@model || Wings::ModelRegistry.lookup(resource.class).to_s
12+
end
13+
14+
def call
15+
prep_resource
16+
Hyrax::Transactions::Container[model_events(model)]
17+
.with_step_args(**model_steps(model)).call(resource_form)
18+
end
19+
20+
def prep_resource
21+
case model
22+
when 'FileSet'
23+
resource.creator << ::User.batch_user.email if resource.creator.blank?
24+
end
25+
end
26+
27+
def resource_form
28+
@resource_form ||= Hyrax::Forms::ResourceForm.for(resource: resource)
29+
end
30+
31+
def model_events(model)
32+
{
33+
'AdminSet' => 'admin_set_resource.update',
34+
'Collection' => 'change_set.update_collection',
35+
'FileSet' => 'change_set.update_file_set'
36+
}[model] || 'change_set.update_work'
37+
end
38+
39+
def model_steps(model)
40+
{
41+
'AdminSet' => {},
42+
'Collection' => {
43+
'collection_resource.save_collection_banner' => { banner_unchanged_indicator: true },
44+
'collection_resource.save_collection_logo' => { logo_unchanged_indicator: true }
45+
},
46+
'FileSet' => {
47+
'file_set.save_acl' => {}
48+
}
49+
}[model] || {
50+
'work_resource.add_file_sets' => { uploaded_files: [], file_set_params: [] },
51+
'work_resource.update_work_members' => { work_members_attributes: [] },
52+
'work_resource.save_acl' => { permissions_params: [] }
53+
}
54+
end
55+
end

lib/freyja/persister.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def save(resource:, external_resource: false, perform_af_validation: false)
3232

3333
def convert_and_migrate_resource(orm_object)
3434
new_resource = resource_factory.to_resource(object: orm_object)
35-
if Hyrax.config.valkyrie_transition? && new_resource.is_a?(Hyrax::FileSet) && new_resource.file_ids.size == 1 && new_resource.file_ids.first.id.to_s.match('/files/')
36-
MigrateFilesToValkyrieJob.perform_later(new_resource)
35+
if Hyrax.config.valkyrie_transition?
36+
MigrateFilesToValkyrieJob.perform_later(new_resource) if new_resource.is_a?(Hyrax::FileSet) && new_resource.file_ids.size == 1 && new_resource.file_ids.first.id.to_s.match('/files/')
37+
MigrateResourcesJob.perform_later(ids: new_resource.member_ids) if new_resource.is_a?(Hyrax::Work)
3738
end
3839
new_resource
3940
end

spec/controllers/hyrax/dashboard/collections_controller_spec.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,6 @@
805805
before { sign_in user }
806806

807807
it 'shows a list of member files' do
808-
pending "update implementation to work with CollectionResource" if
809-
model.safe_constantize == CollectionResource
810-
811808
get :files, params: { id: collection }, format: :json
812809

813810
expect(response).to be_successful
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require 'freyja/persister'
4+
RSpec.describe MigrateResourcesJob, index_adapter: :solr_index, valkyrie_adapter: :freyja_adapter do
5+
let(:af_file_set) { create(:file_set, title: ['TestFS']) }
6+
7+
let!(:af_admin_set) do
8+
as = AdminSet.new(title: ['AF Admin Set'])
9+
as.save
10+
AdminSet.find(as.id)
11+
end
12+
13+
describe '#perform' do
14+
it "migrates admin sets to valkyrie", active_fedora_to_valkyrie: true do
15+
expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_admin_set.id.to_s)).to be_nil
16+
17+
MigrateResourcesJob.perform_now(ids: [af_admin_set.id])
18+
expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_admin_set.id.to_s)).to be_present
19+
end
20+
21+
it "migrates a file set by its id", active_fedora_to_valkyrie: true do
22+
expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_file_set.id.to_s)).to be_nil
23+
24+
MigrateResourcesJob.perform_now(ids: [af_file_set.id])
25+
26+
expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_file_set.id.to_s)).to be_present
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)