Skip to content

Commit

Permalink
transition to new affiliations format
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 15, 2019
1 parent 3ab3323 commit aab41e6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
24 changes: 24 additions & 0 deletions app/jobs/affiliation_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class AffiliationJob < ActiveJob::Base
queue_as :lupo_background

def perform(doi_id)
logger = Logger.new(STDOUT)
doi = Doi.where(doi: doi_id).first

if doi.present?
new_creators = Array.wrap(creators).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
new_contributors = Array.wrap(contributors).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
doi.update_attributes(creators: new_creators, contributors: new_contributors)

doi.__elasticsearch__.index_document
else
logger.info "[Affiliation] Error updaing DOI " + doi_id + ": not found"
end
end
end
61 changes: 59 additions & 2 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ def as_indexed_json(options={})
"doi" => doi,
"identifier" => identifier,
"url" => url,
"creators" => creators,
"contributors" => contributors,
"creators" => creators_with_affiliations,
"contributors" => contributors_with_affiliations,
"creator_names" => creator_names,
"titles" => titles,
"descriptions" => descriptions,
Expand Down Expand Up @@ -544,6 +544,24 @@ def creator_names
end
end

# use newer index with old database following schema 4.3 changes
def creators_with_affiliations
Array.wrap(creators).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
end

def contributors_with_affiliations
Array.wrap(contributors).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
end

def convert_affiliation
end

def doi=(value)
write_attribute(:doi, value.upcase) if value.present?
end
Expand Down Expand Up @@ -820,6 +838,45 @@ def set_defaults
self.updated = Time.zone.now.utc.iso8601
end

# convert affiliations from string to hash, following changes in schema 4.3
def self.convert_affiliations
logger = Logger.new(STDOUT)

response = Doi.query("creators.affiliation:* -creators.affiliation.name:*", page: { size: 1, cursor: [] })
logger.info "#{response.results.total} DOIs found that have the affiliation in the old format."

if response.results.total > 0
# walk through results using cursor
cursor = []

while response.results.results.length > 0 do
response = Doi.query("creators.affiliation:* -creators.affiliation.name:*", page: { size: 1000, cursor: cursor })
break unless response.results.results.length > 0

logger.info "[Affiliation] Updating #{response.results.results.length} DOIs starting with _id #{response.results.to_a.first[:_id]}."
cursor = response.results.to_a.last[:sort]

response.results.results.each do |d|
AffiliationJob.perform_later(d.doi)
end
end
end
end

def creators_with_affiliations
Array.wrap(creators).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
end

def contributors_with_affiliations
Array.wrap(contributors).map do |c|
c["affiliation"] = { "name" => c["affiliation"] } if c["affiliation"].is_a?(String)
c
end
end

def self.migrate_landing_page(options={})
logger = Logger.new(STDOUT)
logger.info "Starting migration"
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ namespace :doi do
Doi.set_minted
end

desc 'Convert affiliations to new format'
task :convert_affiliations => :environment do
Doi.convert_affiliations
end

desc 'Migrates landing page data handling camelCase changes at same time'
task :migrate_landing_page => :environment do
Doi.migrate_landing_page
Expand Down

0 comments on commit aab41e6

Please sign in to comment.