diff --git a/app/jobs/migrate_resources_job.rb b/app/jobs/migrate_resources_job.rb new file mode 100644 index 000000000..7c88f1a8a --- /dev/null +++ b/app/jobs/migrate_resources_job.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# migrates models from AF to valkyrie +class MigrateResourcesJob < ApplicationJob + # input [Array>>String] Array of ActiveFedora model names to migrate to valkyrie objects + # defaults to AdminSet & Collection models if empty + def perform(models: []) + models = collection_models_list if models.empty? + + models.each do |model| + model.constantize.find_each do |item| + res = Hyrax.query_service.find_by(id: item.id) + # start with a form for the resource + fm = form_for(model:).constantize.new(resource: res) + # save the form + converted = Hyrax.persister.save(resource: fm) + # reindex + Hyrax.index_adapter.save(resource: converted) + end + end + end + + def form_for(model:) + model.to_s + 'ResourceForm' + end + + def collection_models_list + %w[AdminSet Collection] + end +end diff --git a/config/initializers/hyrax.rb b/config/initializers/hyrax.rb index 37e8c2684..01d5fe380 100644 --- a/config/initializers/hyrax.rb +++ b/config/initializers/hyrax.rb @@ -39,6 +39,10 @@ # breadcrumbs. config.file_set_model = 'Hyrax::FileSet' + # The default method used for Solr queries. Values are :get or :post. + # Post is suggested to prevent issues with URL length. + config.solr_default_method = :post + # The email address that messages submitted via the contact page are sent to # This is set by account settings # config.contact_email = 'changeme@example.com' diff --git a/lib/tasks/index.rake b/lib/tasks/index.rake index 8a105b3f7..2e9cd95aa 100644 --- a/lib/tasks/index.rake +++ b/lib/tasks/index.rake @@ -30,6 +30,13 @@ task index_file_sets: :environment do end end +desc "migrate all collections & admin sets to valkyrie in the background" +task migrate_collections: :environment do + in_each_account do + MigrateResourcesJob.perform_later + end +end + def in_each_account Account.find_each do |account| puts "=============== #{account.name}============" diff --git a/spec/jobs/migrate_resources_job_spec.rb b/spec/jobs/migrate_resources_job_spec.rb new file mode 100644 index 000000000..92973df19 --- /dev/null +++ b/spec/jobs/migrate_resources_job_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'freyja/persister' +RSpec.describe MigrateResourcesJob, clean: true do + before do + ActiveJob::Base.queue_adapter = :test + FactoryBot.create(:group, name: "public") + end + + after do + clear_enqueued_jobs + end + + let(:account) { create(:account_with_public_schema) } + + let!(:af_admin_set) do + as = AdminSet.new(title: ['AF Admin Set']) + as.save + AdminSet.find(as.id) + end + + describe '#perform' do + it "migrates admin sets to valkyrie", active_fedora_to_valkyrie: true do + expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_admin_set.id.to_s)).to be_nil + + ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true + switch!(account) + MigrateResourcesJob.perform_now + + expect(Valkyrie::Persistence::Postgres::ORM::Resource.find_by(id: af_admin_set.id.to_s)).to be_present + end + end +end