Fix issue with orphaned adjustments in migration

This commit is contained in:
Matt-Yorkley
2021-01-20 13:39:01 +00:00
parent a53cc6bc92
commit e1c13bc194

View File

@@ -21,7 +21,17 @@ class AddOrderToAdjustments < ActiveRecord::Migration
# Migrate adjustments on line_items
Spree::Adjustment.where(order_id: nil, adjustable_type: "Spree::LineItem").includes(:adjustable).find_each do |adjustment|
adjustment.update_column(:order_id, adjustment.adjustable.order_id)
line_item = adjustment.adjustable
# In some cases a line item has been deleted but an orphaned adjustment remains in the
# database. There is no way for this orphan to ever be returned or accessed via any scopes,
# and no way to know what order it related to. In this case we can remove the record.
if line_item.nil?
adjustment.delete
next
end
adjustment.update_column(:order_id, line_item.order_id)
end
end
end