Skip to content

Commit

Permalink
Log destroy result when duplicate record is found
Browse files Browse the repository at this point in the history
When a duplicate record is found, a message is logged that the record
has been detected *and destroyed* before attempting to destroy the
record. If the destroy returns False, and the record is not destroyed,
nothing is logged. This happens in the case of a read-only record.

An alternate solution could be to call destroy! so that an
'ActiveRecord::RecordNotDestroyed' exception is raised if the record is
not destroyed, and read-only records could be handled in a rescue block.
  • Loading branch information
jaywcarman committed Jan 25, 2024
1 parent b7ce09a commit 6cb039e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/inventory_refresh/save_collection/saver/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def assert_unique_record(record, index)
if unique_db_indexes.include?(index) # Include on Set is O(1)
# We have a duplicate in the DB, destroy it. A find_each method does automatically .order(:id => :asc)
# so we always keep the oldest record in the case of duplicates.
logger.warn("A duplicate record was detected and destroyed, inventory_collection: "\
destroyed = record.destroy ? "and destroyed" : "but could not be destroyed"
logger.warn("A duplicate record was detected #{destroyed}, inventory_collection:" \
"'#{inventory_collection}', record: '#{record}', duplicate_index: '#{index}'")
record.destroy
return false
else
unique_db_indexes << index
Expand Down

0 comments on commit 6cb039e

Please sign in to comment.