Skip to content

Instantly share code, notes, and snippets.

@metade
Last active August 29, 2015 14:08
Show Gist options
  • Save metade/5a02c930a26bac1a1189 to your computer and use it in GitHub Desktop.
Save metade/5a02c930a26bac1a1189 to your computer and use it in GitHub Desktop.
namespace :spree do
task destroy_duplicate_shipments: :environment do
# These are the orders with duplicate shipments
orders = Spree::Order.
select("spree_orders.id, spree_orders.number").
joins(:shipments).
group("spree_orders.id, spree_orders.number").
having("COUNT(spree_shipments.id) > 1")
orders.each do |order|
# Just make sure that the basic shipment details are indeed duplicated
details = order.shipments.pluck(:order_id, :address_id, :stock_location_id)
if details.size > 1 && details.uniq.size == 1
# Also make sure that the line items are indeed duplicates
line_items_ids = order.shipments.map(&:line_items).map { |a| a.map(&:id) }
if line_items_ids.size > 1 && line_items_ids.uniq.size == 1
# Just being extra extra sure
if order.shipments.size > 1
Rails.logger.info "DESTROYING DUPLICATE SHIPMENT #{order.number} #{order.shipments.map(&:number)}"
order.shipments.last.destroy
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment