From 6685f56de6f6a0b2ee330f97257ff9efe806e2ee Mon Sep 17 00:00:00 2001 From: LaRita Robinson Date: Fri, 30 Aug 2024 16:03:43 -0400 Subject: [PATCH 1/4] Add rake task to migrate collections Uses lazy migration to migrate all AdminSets & Collections via rake task `migrate_collections` --- app/jobs/migrate_resources_job.rb | 30 ++++++++++++++++++++++++++++++ lib/tasks/index.rake | 7 +++++++ 2 files changed, 37 insertions(+) create mode 100644 app/jobs/migrate_resources_job.rb diff --git a/app/jobs/migrate_resources_job.rb b/app/jobs/migrate_resources_job.rb new file mode 100644 index 000000000..68fde851a --- /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 + def perform(models: []) + models = collection_models_list if models.empty? + + models.each do |model| + resources = Hyrax.query_service.find_all_of_model(model:) + resources.each do |res| + # start with a form for the resource + fm = form_for(model:).constantize.new(resource: as) + # save the form + converted = Hyrax.persister.save(resource: fm) + # reindex + Hyrax.index_adapter.save(resource: converted) + end + end + + def form_for(model:) + model.to_s + 'ResourceForm' + end + + def collection_models_list + %w(AdminSet Collection) + end + end +end 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}============" From 6282bc26586b120908a82af3f53db1c97cb1b94c Mon Sep 17 00:00:00 2001 From: LaRita Robinson Date: Fri, 30 Aug 2024 16:06:20 -0400 Subject: [PATCH 2/4] Set default solr method 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 --- config/initializers/hyrax.rb | 4 ++++ 1 file changed, 4 insertions(+) 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' From d5c65e815e743ce199bd97a2be3abca6c068d581 Mon Sep 17 00:00:00 2001 From: LaRita Robinson Date: Fri, 30 Aug 2024 20:01:33 -0400 Subject: [PATCH 3/4] Finish Migration job Fixes migrate resources job Adds spec for job --- app/jobs/migrate_resources_job.rb | 21 ++++++++-------- spec/jobs/migrate_resources_job_spec.rb | 33 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 spec/jobs/migrate_resources_job_spec.rb diff --git a/app/jobs/migrate_resources_job.rb b/app/jobs/migrate_resources_job.rb index 68fde851a..48a6c8650 100644 --- a/app/jobs/migrate_resources_job.rb +++ b/app/jobs/migrate_resources_job.rb @@ -3,28 +3,29 @@ # 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 + # defaults to AdminSet & Collection models if empty def perform(models: []) models = collection_models_list if models.empty? models.each do |model| - resources = Hyrax.query_service.find_all_of_model(model:) - resources.each do |res| + model.constantize.find_each do |item| + res = Hyrax.query_service.find_by(id: item.id) + puts "🦋🦋 Migrating #{model} #{res.title.first}, id: #{res.id.to_s} 🦋🦋" # start with a form for the resource - fm = form_for(model:).constantize.new(resource: as) + 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 form_for(model:) + model.to_s + 'ResourceForm' + end - def collection_models_list - %w(AdminSet Collection) - end + def collection_models_list + %w(AdminSet Collection) end end 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 From a0d0a4aae50a2b18e547364d2a486a85fc7fa730 Mon Sep 17 00:00:00 2001 From: LaRita Robinson Date: Sat, 31 Aug 2024 11:49:15 -0400 Subject: [PATCH 4/4] Rubocop fixes --- app/jobs/migrate_resources_job.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/jobs/migrate_resources_job.rb b/app/jobs/migrate_resources_job.rb index 48a6c8650..7c88f1a8a 100644 --- a/app/jobs/migrate_resources_job.rb +++ b/app/jobs/migrate_resources_job.rb @@ -10,7 +10,6 @@ def perform(models: []) models.each do |model| model.constantize.find_each do |item| res = Hyrax.query_service.find_by(id: item.id) - puts "🦋🦋 Migrating #{model} #{res.title.first}, id: #{res.id.to_s} 🦋🦋" # start with a form for the resource fm = form_for(model:).constantize.new(resource: res) # save the form @@ -26,6 +25,6 @@ def form_for(model:) end def collection_models_list - %w(AdminSet Collection) + %w[AdminSet Collection] end end