From 95fcff8bb1ccfff40ad0c3045769a4e9c1baa05b Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Fri, 6 Sep 2019 18:47:07 +0100 Subject: [PATCH] Add migration to remove broken variants from open carts --- ...6165501_remove_broken_variants_from_carts.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 db/migrate/20190906165501_remove_broken_variants_from_carts.rb diff --git a/db/migrate/20190906165501_remove_broken_variants_from_carts.rb b/db/migrate/20190906165501_remove_broken_variants_from_carts.rb new file mode 100644 index 0000000000..3e6cfaed10 --- /dev/null +++ b/db/migrate/20190906165501_remove_broken_variants_from_carts.rb @@ -0,0 +1,17 @@ +class RemoveBrokenVariantsFromCarts < ActiveRecord::Migration + def up + # Removes line_items from open carts where the variant has a hard-deleted price + + variants_without_prices = execute( + 'SELECT spree_variants.id FROM spree_variants + LEFT OUTER JOIN spree_prices ON (spree_variants.id = spree_prices.variant_id) + WHERE spree_prices.id IS NULL' + ).to_a.map{ |v| v['id'] } + + broken_line_items = Spree::LineItem. + joins(:variant).where('spree_variants.id IN (?)', variants_without_prices). + joins(:order).merge(Spree::Order.where(state: 'cart')) + + broken_line_items.each(&:destroy) + end +end