diff --git a/app/models/doi.rb b/app/models/doi.rb index 0ac4ddbbe..ac0c24f97 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -585,7 +585,7 @@ def self.convert_affiliation_by_id(options={}) Doi.where(id: id..(id + 499)).find_each do |doi| should_update = false creators = Array.wrap(doi.creators).map do |c| - if !c.is_a?(Hash) + if !(c.is_a?(Hash)) logger.error "[MySQL] creators for DOI #{doi.doi} should be a hash." elsif c["affiliation"].nil? c["affiliation"] = [] @@ -597,7 +597,7 @@ def self.convert_affiliation_by_id(options={}) c["affiliation"] = Array.wrap(c["affiliation"]) should_update = true elsif c["affiliation"].is_a?(Array) - c["affiliation"].map do |a| + c["affiliation"] = c["affiliation"].map do |a| if a.nil? should_update = true @@ -615,7 +615,7 @@ def self.convert_affiliation_by_id(options={}) c end contributors = Array.wrap(doi.contributors).map do |c| - if !c.is_a?(Hash) + if !(c.is_a?(Hash)) logger.error "[MySQL] creators for DOI #{doi.doi} should be a hash." elsif c["affiliation"].nil? c["affiliation"] = [] @@ -627,7 +627,7 @@ def self.convert_affiliation_by_id(options={}) c["affiliation"] = Array.wrap(c["affiliation"]) should_update = true elsif c["affiliation"].is_a?(Array) - c["affiliation"].map do |a| + c["affiliation"] = c["affiliation"].map do |a| if a.nil? should_update = true @@ -646,16 +646,25 @@ def self.convert_affiliation_by_id(options={}) end if should_update + Doi.auditing_enabled = false doi.update_columns(creators: creators, contributors: contributors) + Doi.auditing_enabled = true + count += 1 end + + unless (Array.wrap(doi.creators).all? { |c| c.is_a?(Hash) && c["affiliation"].is_a?(Array) && c["affiliation"].all? { |a| a.is_a?(Hash) } } && Array.wrap(doi.contributors).all? { |c| c.is_a?(Hash) && c["affiliation"].is_a?(Array) && c["affiliation"].all? { |a| a.is_a?(Hash) } }) + logger.error "[MySQL] Error converting affiliations for doi #{doi.doi}: creators #{doi.creators.inspect} contributors #{doi.contributors.inspect}." + fail TypeError, "Affiliation for doi #{doi.doi} is of wrong type" if Rails.env.test? + end end logger.info "[MySQL] Converted affiliations for #{count} DOIs with IDs #{id} - #{(id + 499)}." if count > 0 count - rescue Faraday::ConnectionFailed, ActiveRecord::LockWaitTimeout => error - logger.info "[MySQL] Error #{error.message} converting affiliations for DOIs with IDs #{id} - #{(id + 499)}." + rescue TypeError, ActiveRecord::ActiveRecordError, ActiveRecord::LockWaitTimeout => error + logger.error "[MySQL] Error converting affiliations for DOIs with IDs #{id} - #{(id + 499)}." + count end def doi=(value) diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 6c1a0ed6c..fafcc44ee 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -540,6 +540,102 @@ end end + describe "convert_affiliations" do + let(:doi) { create(:doi)} + + context "affiliation nil" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": nil + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1) + end + end + + context "affiliation empty array" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": [] + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(0) + end + end + + context "affiliation array of hashes" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": [{ "name": "Department of Microbiology; Tartu University; Tartu Estonia" }] + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(0) + end + end + + context "affiliation hash" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": { "name": "Department of Microbiology; Tartu University; Tartu Estonia" } + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1) + end + end + + context "affiliation array of strings" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": ["Andrology Centre; Tartu University Hospital; Tartu Estonia", "Department of Surgery; Tartu University; Tartu Estonia"] + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1) + end + end + + context "affiliation string" do + let(:creators) { [{ + "name": "Ausmees, K.", + "nameType": "Personal", + "givenName": "K.", + "familyName": "Ausmees", + "affiliation": "Andrology Centre; Tartu University Hospital; Tartu Estonia" + }] } + let(:doi) { create(:doi, creators: creators, contributors: [])} + + it "convert" do + expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1) + end + end + end + + + describe "migrates landing page" do let(:provider) { create(:provider, symbol: "ADMIN") } let(:client) { create(:client, provider: provider) }