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.

Discontinue kept master variants in remove_master_variant task

Łukasz Adamczyk394c1d78df67
diff --git a/spree/core/lib/tasks/remove_master_variant.rake b/spree/core/lib/tasks/remove_master_variant.rake
index a9263f87ad..2770a641a1 100644
--- a/spree/core/lib/tasks/remove_master_variant.rake
+++ b/spree/core/lib/tasks/remove_master_variant.rake
@@ -6,8 +6,9 @@ namespace :spree do
For each product:
* With real variants: point default_variant_id at the first variant by
position, then delete the now-empty master — unless it still carries
- line items, in which case keep it as a regular variant (is_master=false)
- so historical orders stay intact.
+ line items, in which case keep it as a discontinued regular variant
+ (is_master=false) so historical orders stay intact without exposing it
+ as a sellable variant.
* Simple (no-variant) products: convert the master into a regular variant
(is_master=false) and point default_variant_id at it.
* Recompute variant_count to the real variant count.
@@ -28,7 +29,9 @@ namespace :spree do
product.update_column(:default_variant_id, first_variant.id)
if master && master.line_items.exists?
- master.update_column(:is_master, false)
+ # Keep for order history, but discontinue so the demoted master isn't
+ # exposed as a live, sellable variant.
+ master.update_columns(is_master: false, discontinue_on: master.discontinue_on || Time.current)
kept += 1
elsif master
Rails.logger.info("[remove_master_variant] deleting master variant #{master.id} of product #{product.id}")
@@ -50,6 +53,6 @@ namespace :spree do
end
puts " Migrated #{migrated} product(s): converted #{converted} simple master(s), " \
- "deleted #{deleted} ghost master(s), kept #{kept} master(s) with order history."
+ "deleted #{deleted} ghost master(s), kept #{kept} discontinued master(s) with order history."
end
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 3d0dec62cd..faeded17de 100644
--- a/spree/core/spec/lib/tasks/remove_master_variant_spec.rb
+++ b/spree/core/spec/lib/tasks/remove_master_variant_spec.rb
@@ -116,6 +116,12 @@ describe 'spree:remove_master_variant' do
expect(persisted_default_variant_id(product)).to eq(real_variant.id)
end
+ it 'discontinues the kept master so it is not a live variant' do
+ subject.invoke
+
+ expect(Spree::Variant.find(master_id)).to be_discontinued
+ end
+
it_behaves_like 'an idempotent migration'
end
end