mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Fix issue with orphaned adjustments in migration
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user