diff --git a/app/models/spree/option_values_line_item.rb b/app/models/spree/option_values_line_item.rb new file mode 100644 index 0000000000..4f1ab992a8 --- /dev/null +++ b/app/models/spree/option_values_line_item.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +module Spree + class OptionValuesLineItem < ActiveRecord::Base + belongs_to :line_item, class_name: 'Spree::LineItem' + belongs_to :option_value, class_name: 'Spree::OptionValue' + end +end diff --git a/lib/tasks/data/remove_transient_data.rb b/lib/tasks/data/remove_transient_data.rb index e4fe755f23..a0b8128f04 100644 --- a/lib/tasks/data/remove_transient_data.rb +++ b/lib/tasks/data/remove_transient_data.rb @@ -18,7 +18,6 @@ class RemoveTransientData Session.where("updated_at < ?", SHORT_RETENTION).delete_all clear_old_cart_data! - clear_line_item_option_values! end private @@ -29,25 +28,15 @@ class RemoveTransientData merge(orders_without_payments) old_cart_line_items = Spree::LineItem.where(order_id: old_carts) + old_line_item_options = Spree::OptionValuesLineItem.where(line_item_id: old_cart_line_items) old_cart_adjustments = Spree::Adjustment.where(order_id: old_carts) old_cart_adjustments.delete_all + old_line_item_options.delete_all old_cart_line_items.delete_all old_carts.delete_all end - def clear_line_item_option_values! - ActiveRecord::Base.connection.execute <<-SQL - DELETE FROM spree_option_values_line_items - WHERE line_item_id IN ( - SELECT line_item_id FROM spree_option_values_line_items - LEFT OUTER JOIN spree_line_items - ON spree_option_values_line_items.line_item_id = spree_line_items.id - WHERE spree_line_items.id IS NULL - ); - SQL - end - def orders_without_payments # Carts with failed payments are ignored, as they contain potentially useful data Spree::Order. diff --git a/spec/lib/tasks/data/remove_transient_data_spec.rb b/spec/lib/tasks/data/remove_transient_data_spec.rb index 34811682c1..fddf33aef7 100644 --- a/spec/lib/tasks/data/remove_transient_data_spec.rb +++ b/spec/lib/tasks/data/remove_transient_data_spec.rb @@ -61,23 +61,11 @@ describe RemoveTransientData do expect{ old_adjustment.reload }.to raise_error ActiveRecord::RecordNotFound end - context "removing defunct line item option value records" do - let(:connection) { ActiveRecord::Base.connection } - let(:query) { - <<-SQL - SELECT * FROM spree_option_values_line_items - LEFT OUTER JOIN spree_line_items - ON spree_option_values_line_items.line_item_id = spree_line_items.id - WHERE spree_line_items.id IS NULL; - SQL - } + it "removes any defunct line item option value records" do + line_item.delete - it "removes the records" do - line_item.delete - - expect{ RemoveTransientData.new.call }. - to change{ connection.execute(query).count }.by(-1) - end + expect{ RemoveTransientData.new.call }. + to change{ Spree::OptionValuesLineItem.count }.by(-1) end end end