Skip to content

Commit a0746e6

Browse files
authored
Merge pull request solidusio#6441 from SuperGoodSoft/jared/duplicate-base-codes
Handle duplicate base_codes in PromotionMigrator
2 parents 61e08a6 + 4d87b88 commit a0746e6

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

promotions/lib/solidus_promotions/promotion_migrator.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def copy_promotion_codes(new_promotion)
6161
SELECT solidus_promotions_promotions.id AS promotion_id, solidus_promotions_promotion_code_batches.id AS promotion_code_batch_id, value, spree_promotion_codes.created_at, spree_promotion_codes.updated_at
6262
FROM spree_promotion_codes
6363
LEFT OUTER JOIN spree_promotion_code_batches ON spree_promotion_code_batches.id = spree_promotion_codes.promotion_code_batch_id
64-
LEFT OUTER JOIN solidus_promotions_promotion_code_batches ON solidus_promotions_promotion_code_batches.base_code = spree_promotion_code_batches.base_code
64+
LEFT OUTER JOIN solidus_promotions_promotion_code_batches
65+
ON solidus_promotions_promotion_code_batches.base_code = spree_promotion_code_batches.base_code
66+
AND solidus_promotions_promotion_code_batches.promotion_id = #{new_promotion.id}
67+
AND solidus_promotions_promotion_code_batches.created_at = spree_promotion_code_batches.created_at
6568
INNER JOIN spree_promotions ON spree_promotion_codes.promotion_id = spree_promotions.id
6669
INNER JOIN solidus_promotions_promotions ON spree_promotions.id = solidus_promotions_promotions.original_promotion_id
6770
WHERE spree_promotion_codes.promotion_id = #{new_promotion.original_promotion_id};

promotions/spec/lib/solidus_promotions/promotion_migrator_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,55 @@
5252
end
5353
end
5454

55+
context "when multiple promotions have batches with the same base_code" do
56+
let(:shared_base_code) { "SUVIE" }
57+
let(:shared_time) { Time.current.change(usec: 0) }
58+
let!(:spree_promotion) { create(:promotion) }
59+
let!(:another_spree_promotion) { create(:promotion) }
60+
let!(:first_batch) do
61+
Spree::PromotionCodeBatch.create!(
62+
promotion: spree_promotion,
63+
base_code: shared_base_code,
64+
number_of_codes: 1,
65+
created_at: shared_time,
66+
updated_at: shared_time
67+
)
68+
end
69+
let!(:second_batch) do
70+
Spree::PromotionCodeBatch.create!(
71+
promotion: another_spree_promotion,
72+
base_code: shared_base_code,
73+
number_of_codes: 1,
74+
created_at: shared_time,
75+
updated_at: shared_time
76+
)
77+
end
78+
let!(:first_code) do
79+
create(
80+
:promotion_code,
81+
promotion: spree_promotion,
82+
promotion_code_batch: first_batch,
83+
value: "suvie-lgm4gw"
84+
)
85+
end
86+
let!(:second_code) do
87+
create(
88+
:promotion_code,
89+
promotion: another_spree_promotion,
90+
promotion_code_batch: second_batch,
91+
value: "suvie-abc123"
92+
)
93+
end
94+
95+
it "copies each code exactly once without raising a duplicate value error" do
96+
expect { subject }.not_to raise_error
97+
expect(SolidusPromotions::PromotionCode.where(value: [first_code.value, second_code.value]).count).to eq(2)
98+
expect(
99+
SolidusPromotions::PromotionCode.where(value: [first_code.value, second_code.value]).group(:value).count.values
100+
).to all(eq(1))
101+
end
102+
end
103+
55104
context "if our rules and actions are missing from the promotion map" do
56105
let(:promotion_map) do
57106
{

0 commit comments

Comments
 (0)