-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3748 from samvera/1505_keywords
Change predicate used by keyword
- Loading branch information
Showing
4 changed files
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Hyrax | ||
module Works | ||
class MigrationService | ||
# @api public | ||
# | ||
# Migrate data stored in FCrepo from one predicate to another | ||
# The main use case is where the predicate used for a specific property is changed. | ||
# Data already stored in Fedora under the original predicate needs to be transfered | ||
# and the original data cleaned up. | ||
def self.migrate_predicate(predicate_from, predicate_to, works_to_update = ActiveFedora::Base.all) | ||
migrated = 0 | ||
Rails.logger.info "*** Migrating #{predicate_from} to #{predicate_to} in #{works_to_update.count} works" | ||
works_to_update.each do |work| | ||
next unless work.ldp_source.content.include?(predicate_from.to_s) | ||
migrate_data(predicate_from, predicate_to, work) | ||
migrated += 1 | ||
end | ||
Rails.logger.info "--- Migration Complete (#{migrated} migrated)" | ||
end | ||
|
||
# @api private | ||
# | ||
# Migrate data | ||
def self.migrate_data(predicate_from, predicate_to, work) | ||
orm = Ldp::Orm.new(work.ldp_source) | ||
orm.value(predicate_from).each { |val| orm.graph.insert([orm.resource.subject_uri, predicate_to, val.to_s]) } | ||
orm.graph.delete([orm.resource.subject_uri, predicate_from, nil]) | ||
orm.save | ||
Rails.logger.info " Data migrated from #{predicate_from} to #{predicate_to} - id: #{work.id}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# rubocop:disable RSpec/InstanceVariable | ||
RSpec.describe Hyrax::Works::MigrationService, clean_repo: true do | ||
let(:predicate_from) { ::RDF::Vocab::DC11.description } | ||
let(:predicate_to) { ::RDF::Vocab::SCHEMA.description } | ||
|
||
describe "#migrate_predicate" do | ||
it "uses DC description by default" do | ||
@work = GenericWork.create(title: ["War and Peace"], description: ["war", "peace"]) | ||
expect(@work.ldp_source.content).to include("http://purl.org/dc/elements/1.1/description") | ||
end | ||
|
||
it "updates to use SCHEMA description" do | ||
@work = GenericWork.create(title: ["War and Peace"], description: ["war", "peace"]) | ||
described_class.migrate_predicate(predicate_from, predicate_to) | ||
@work.reload | ||
expect(@work.ldp_source.content).to include("http://schema.org/description") | ||
expect(@work.ldp_source.content).not_to include("http://purl.org/dc/elements/1.1/description") | ||
end | ||
end | ||
end | ||
# rubocop:enable RSpec/InstanceVariable |