Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix variant_count left short by spree:remove_master_variant

Łukasz Adamczyk3fb2ef49d11a
diff --git a/spree/core/lib/tasks/remove_master_variant.rake b/spree/core/lib/tasks/remove_master_variant.rake
index 2dceec3915..a9263f87ad 100644
--- a/spree/core/lib/tasks/remove_master_variant.rake
+++ b/spree/core/lib/tasks/remove_master_variant.rake
@@ -44,7 +44,7 @@ namespace :spree do
end
variant_count = Spree::Variant.where(product_id: product.id, deleted_at: nil).count
- product.update_column(:variant_count, variant_count) unless product.variant_count == variant_count
+ product.update_column(:variant_count, variant_count)
migrated += 1
end
diff --git a/spree/core/spec/lib/tasks/remove_master_variant_spec.rb b/spree/core/spec/lib/tasks/remove_master_variant_spec.rb
index 1c7e3cbdc8..3d0dec62cd 100644
--- a/spree/core/spec/lib/tasks/remove_master_variant_spec.rb
+++ b/spree/core/spec/lib/tasks/remove_master_variant_spec.rb
@@ -34,6 +34,11 @@ describe 'spree:remove_master_variant' do
Spree::Variant.where(product_id: product.id).first.update_column(:is_master, true)
product.update_column(:default_variant_id, nil)
extra.times { create(:variant, product: product) }
+ # Legacy rows counted variant_count under the old master-excluded rule. Mirror
+ # that so the task must recompute correctly: deleting the master fires the
+ # counter callback (out-of-band decrement) and a stale-value guard would leave
+ # variant_count one short — matching this legacy baseline is what surfaces it.
+ product.update_column(:variant_count, Spree::Variant.where(product_id: product.id, is_master: false).count)
product
end