Skip to content

Commit

Permalink
Merge pull request #5411 from mamhoff/destroy-dependent-order-promotions
Browse files Browse the repository at this point in the history
Add dependent: :destroy to Spree::Order#order_promotions
  • Loading branch information
tvdeyen authored Nov 1, 2023
2 parents e689e66 + 9e4a2c9 commit 39b1c92
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def states
foreign_key: :order_id,
dependent: :destroy,
inverse_of: :order
has_many :order_promotions, class_name: 'Spree::OrderPromotion'
has_many :order_promotions, class_name: 'Spree::OrderPromotion', dependent: :destroy
has_many :promotions, through: :order_promotions

# Payments
Expand Down
10 changes: 10 additions & 0 deletions core/db/migrate/20231027084517_add_order_promotions_foreign_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddOrderPromotionsForeignKey < ActiveRecord::Migration[7.0]
def up
Spree::OrderPromotion.left_joins(:order).where(spree_orders: { id: nil }).delete_all
add_foreign_key :spree_orders_promotions, :spree_orders, column: :order_id, validate: false, on_delete: :cascade
end

def down
remove_foreign_key :spree_orders_promotions, :spree_orders
end
end
14 changes: 14 additions & 0 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1818,4 +1818,18 @@ def validate(line_item)
end
end
end

describe "order deletion" do
let(:order) { create(:order) }
let(:promotion) { create(:promotion) }

subject { order.destroy }
before do
order.promotions << promotion
end

it "deletes join table entries when deleting an order" do
expect { subject }.to change { Spree::OrderPromotion.count }.from(1).to(0)
end
end
end

0 comments on commit 39b1c92

Please sign in to comment.