Skip to content

Commit

Permalink
Update all bookmarks to Alma ids
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Apr 27, 2023
1 parent a3b19ae commit 8042e15
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
22 changes: 16 additions & 6 deletions app/models/bookmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def document_type
end

def self.destroy_without_solr_documents
Bookmark.find_in_batches do |bookmarks|
Bookmark.find_in_batches.with_index do |bookmarks, batch|
Rails.logger.info { "Processing destroy_without_solr_documents group ##{batch}" }
bookmark_doc_ids = bookmark_doc_ids(bookmarks)
doc_ids_without_solr_doc = bookmark_doc_ids - doc_ids_in_solr(bookmark_doc_ids)

Expand All @@ -26,9 +27,18 @@ def self.destroy_without_solr_documents
end
end

def self.update_to_alma_ids
Bookmark.where("LENGTH(document_id) <= 7").find_in_batches.with_index do |bookmarks, batch|
Rails.logger.info { "Processing update_to_alma_ids group ##{batch}" }
bookmarks.each do |bookmark|
bookmark.document_id = bookmark.voyager_to_alma_id
bookmark.save if bookmark.changed?
end
end
end

def self.bookmark_doc_ids(bookmarks)
ids = bookmarks.map(&:document_id).to_set
ids.map { |id| ensure_voyager_to_alma_id(id) }.to_set
bookmarks.map(&:document_id).to_set
end

def self.doc_ids_in_solr(bookmark_doc_ids)
Expand All @@ -39,8 +49,8 @@ def self.doc_ids_in_solr(bookmark_doc_ids)

# This method duplicates logic in the SolrDocument, but it did not seem well-suited for
# re-use directly here (needing to instantiate a SolrDocument for each ID before being able to call it)
def self.ensure_voyager_to_alma_id(id)
return id if id.length > 7 && id.start_with?("99")
"99#{id}3506421"
def voyager_to_alma_id
return document_id if document_id.length > 7 && document_id.start_with?("99")
"99#{document_id}3506421"
end
end
1 change: 1 addition & 0 deletions lib/tasks/maintenance.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace :orangelight do
end
# orangelight:clean:bookmarks:without_solr_documents
task without_solr_documents: :environment do
Bookmark.update_to_alma_ids
Bookmark.destroy_without_solr_documents
end
end
Expand Down
35 changes: 18 additions & 17 deletions spec/models/bookmark_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
describe 'removing bookmarks with deleted solr records' do
let(:in_solr_alma_id_one) { '994956003506421' }
let(:in_solr_alma_id_two) { '99118600973506421' }
let(:in_solr_voyager_id) { '8908514' }
let(:not_in_solr_alma_id) { '991234567806421' }
let(:not_in_solr_voyager_id) { '1234567' }
let!(:bookmark_one) { FactoryBot.create(:bookmark, document_id: document_ids[0]) }
let!(:bookmark_two) { FactoryBot.create(:bookmark, document_id: document_ids[1]) }

Expand Down Expand Up @@ -46,25 +44,28 @@
end.to change { Bookmark.count }.by(-2)
end
end
end

context 'with a voyager id that is in solr' do
let(:document_ids) { [in_solr_alma_id_one, in_solr_voyager_id] }
describe 'converting all document_ids to alma ids' do
let(:voyager_id_one) { '8908514' }
let(:voyager_id_two) { '1234567' }
let(:converted_voyager_id) { '9989085143506421' }
let(:alma_id) { '99118600973506421' }
let(:document_ids) { [voyager_id_one, voyager_id_two, alma_id] }
let!(:bookmark_one) { FactoryBot.create(:bookmark, document_id: document_ids[0]) }
let!(:bookmark_two) { FactoryBot.create(:bookmark, document_id: document_ids[1]) }
let!(:bookmark_three) { FactoryBot.create(:bookmark, document_id: document_ids[2]) }

it 'does not delete the voyager bookmark' do
expect do
Bookmark.destroy_without_solr_documents
end.not_to change { Bookmark.count }
end
it 'does not change Bookmarks with an alma document_id' do
expect do
Bookmark.update_to_alma_ids
end.not_to change { bookmark_three.reload.document_id }
end

context 'with a voyager id that is not in solr' do
let(:document_ids) { [in_solr_voyager_id, not_in_solr_voyager_id] }

xit 'deletes the voyager bookmark that is not in solr' do
expect do
Bookmark.destroy_without_solr_documents
end.to change { Bookmark.count }.by(-1)
end
it 'updates bookmarks with a voyager document_id' do
expect do
Bookmark.update_to_alma_ids
end.to change { bookmark_one.reload.document_id }
end
end
end

0 comments on commit 8042e15

Please sign in to comment.