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: Rename calculator calculable_type to Spree::DeliveryMethod on upgrade (#15059)

Damian Legawiec68b5a16a35a5
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdx
index 9acee42903..612e340fb1 100644
--- a/docs/developer/upgrades/5.6-to-6.0.mdx
+++ b/docs/developer/upgrades/5.6-to-6.0.mdx
@@ -128,7 +128,7 @@ bundle exec rake spree:migrate_zones_to_delivery_zones
bundle exec rake spree:migrate_calculator_bounds_to_delivery_method_rules
```
-Moves stored strings, statuses and class names to the fulfillment/delivery vocabulary, converts delivery-referenced `Spree::Zone` records into `Spree::DeliveryZone` with typed members, and converts FlatRate calculator eligibility bounds into `Spree::DeliveryMethodRule` records.
+Moves stored strings, statuses and class names to the fulfillment/delivery vocabulary (including the calculator link of each delivery method), converts delivery-referenced `Spree::Zone` records into `Spree::DeliveryZone` with typed members, and converts FlatRate calculator eligibility bounds into `Spree::DeliveryMethodRule` records.
### Remove master variants
diff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
index 5a254ef39c..f6fd68b922 100644
--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
@@ -212,7 +212,8 @@ steps:
task: "spree:migrate_shipping_to_delivery"
notes: |
Rewrites stored 'Spree::Shipment' class-name strings (state changes,
- stock movements, legacy adjustments), renames the terminal fulfillment
+ stock movements, legacy adjustments) and the calculators' 'Spree::ShippingMethod'
+ calculable type (so delivery methods find their calculator), renames the terminal fulfillment
status 'shipped' -> 'fulfilled', backfills the fulfillment provider on
delivery methods (DigitalDelivery calculators become the Digital
provider) and copies the provider-derived type onto existing
diff --git a/spree/core/lib/tasks/delivery_migration.rake b/spree/core/lib/tasks/delivery_migration.rake
index f762bf2b74..fb46413a89 100644
--- a/spree/core/lib/tasks/delivery_migration.rake
+++ b/spree/core/lib/tasks/delivery_migration.rake
@@ -29,6 +29,11 @@ namespace :spree do
say.call "spree_adjustments.adjustable_type: #{count} rows Spree::Shipment → Spree::Fulfillment"
end
+ # Calculators are polymorphic on the renamed shipping method; without this
+ # DeliveryMethod#calculator finds nothing.
+ count = Spree::Calculator.where(calculable_type: 'Spree::ShippingMethod').in_batches(of: batch_size).update_all(calculable_type: 'Spree::DeliveryMethod')
+ say.call "spree_calculators.calculable_type: #{count} rows Spree::ShippingMethod → Spree::DeliveryMethod"
+
# The name column distinguishes the machine ('shipment' rows become
# 'fulfillment'); the recorded states keep their history except the
# renamed terminal value.
diff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
index 8f204a2dfb..55fdced4b6 100644
--- a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
+++ b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb
@@ -121,6 +121,19 @@ describe '6.0 data migration tasks' do
expect(method.reload.read_attribute(:storefront_visible)).to be(true)
end
+ it 'repoints legacy shipping method calculators at the delivery method' do
+ method = create(:delivery_method)
+ calculator = method.calculator
+ calculator.update_columns(calculable_type: 'Spree::ShippingMethod')
+ expect(method.reload.calculator).to be_nil
+
+ run_task('spree:migrate_shipping_to_delivery')
+ run_task('spree:migrate_shipping_to_delivery')
+
+ expect(calculator.reload.calculable_type).to eq('Spree::DeliveryMethod')
+ expect(method.reload.calculator).to eq(calculator)
+ end
+
it 'is idempotent' do
fulfillment.update_columns(status: 'shipped')
run_task('spree:migrate_shipping_to_delivery')