Skip to content

Commit

Permalink
Migration script for collections (#2310)
Browse files Browse the repository at this point in the history
* Add rake task to migrate collections

Uses lazy migration to migrate all AdminSets & Collections via
rake task `migrate_collections`

* 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

* Finish Migration job

Fixes migrate resources job
Adds spec for job

* Rubocop fixes
  • Loading branch information
laritakr authored Sep 3, 2024
1 parent 2428f56 commit f09b786
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/jobs/migrate_resources_job.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions config/initializers/hyrax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]'
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/index.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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}============"
Expand Down
33 changes: 33 additions & 0 deletions spec/jobs/migrate_resources_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f09b786

Please sign in to comment.