Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration script for collections #2310

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading