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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: Keep 5.6 delivery method zones through the 6.0 upgrade (#15060)
Damian Legawiec9faf50d534df
diff --git a/spree/core/db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rb b/spree/core/db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rbindex 25b288e370..677161d1ee 100644--- a/spree/core/db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rb+++ b/spree/core/db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rb@@ -52,6 +52,7 @@ class PromoteShippingCategoriesToDeliveryProfiles < ActiveRecord::Migration[8.1]remove_column :spree_product_types, :fulfillment_typesbackfill_store_defaults+ copy_first_zone_onto_methodsdrop_table :spree_delivery_method_zonesend@@ -141,13 +142,21 @@ class PromoteShippingCategoriesToDeliveryProfiles < ActiveRecord::Migration[8.1]# never carried a category, so they all belong to the default profile.MigrationProduct.where(store_id: store.id, delivery_profile_id: nil).update_all(delivery_profile_id: profile.id)- MigrationMethod.where(store_id: store.id).find_each do |method|- zone_ids = MigrationMethodZone.where(delivery_method_id: method.id).order(:id).pluck(:delivery_zone_id)- if zone_ids.size > 1- say "Delivery method #{method.id} was linked to #{zone_ids.size} zones; keeping the first — recreate the method for the other zones if both were intended."- end- method.update_columns(delivery_profile_id: profile.id, delivery_origin_group_id: origin_group.id, delivery_zone_id: zone_ids.first)+ MigrationMethod.where(store_id: store.id).update_all(delivery_profile_id: profile.id, delivery_origin_group_id: origin_group.id)+ end+ end++ # Runs for every method, including 5.x rows whose store_id is still nil: the+ # link table is dropped right after, so this is the last chance to keep it.+ def copy_first_zone_onto_methods+ zone_ids_by_method = MigrationMethodZone.order(:id).pluck(:delivery_method_id, :delivery_zone_id).+ group_by(&:first).transform_values { |pairs| pairs.map(&:last) }++ zone_ids_by_method.each do |method_id, zone_ids|+ if zone_ids.size > 1+ say "Delivery method #{method_id} was linked to #{zone_ids.size} zones; keeping the first — recreate the method for the other zones if both were intended."end+ MigrationMethod.where(id: method_id).update_all(delivery_zone_id: zone_ids.first)endendenddiff --git a/spree/core/lib/tasks/delivery_migration.rake b/spree/core/lib/tasks/delivery_migration.rakeindex 9546ac95cc..f762bf2b74 100644--- a/spree/core/lib/tasks/delivery_migration.rake+++ b/spree/core/lib/tasks/delivery_migration.rake@@ -171,6 +171,11 @@ namespace :spree doendend+ # 5.x methods have no store until backfill_delivery_and_stock_store_ids+ # runs (later in the upgrade), and a DeliveryZone requires one. Resolve+ # them to the default store, exactly as that backfill will.+ default_store_id = Spree::Store.default&.id+legacy_zones.find_each do |zone|if Spree::DeliveryZone.exists?(id: zone.id) && !migrated.value?(zone.id)puts "SKIP zone #{zone.id} (#{zone.name}): a DeliveryZone with the same id already exists — resolve manually"@@ -178,7 +183,7 @@ namespace :spree doendmethods_by_home = Spree::DeliveryMethod.unscoped.where(delivery_zone_id: zone.id).- group_by { |method| [method.store_id, method.delivery_profile_id] }+ group_by { |method| [method.store_id || default_store_id, method.delivery_profile_id] }methods_by_home.each do |(store_id, profile_id), methods|profile_id ||= Spree::DeliveryProfile.where(store_id: store_id, default: true).pick(:id)@@ -208,6 +213,12 @@ namespace :spree doputs "SKIP unreferenced shipping zone #{zone.id} (#{zone.name}) — no delivery method uses it"end+ zoneless = Spree::DeliveryMethod.unscoped.where(delivery_zone_id: nil).pluck(:id, :name)+ if zoneless.any?+ puts "WARNING: #{zoneless.size} delivery methods have no delivery zone and return no rates at checkout. Assign a zone to each in the admin:"+ zoneless.each { |id, name| puts " - ##{id} #{name}" }+ end+puts 'migrate_zones_to_delivery_zones done. Tax-scoped zones stay on Spree::Zone for the tax provider migration.'endenddiff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rbindex 0e51ea603c..8f204a2dfb 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@@ -423,6 +423,64 @@ describe '6.0 data migration tasks' dorun_task('spree:migrate_zones_to_delivery_zones')expect { run_task('spree:migrate_zones_to_delivery_zones') }.not_to change(Spree::DeliveryZone, :count)end++ context 'when the method predates its store binding (store_id nil)' do+ before { delivery_method.update_columns(store_id: nil, delivery_profile_id: nil) }++ it 'resolves the store to the default store' do+ run_task('spree:migrate_zones_to_delivery_zones')++ delivery_zone = Spree::DeliveryZone.find_by(name: zone.name)+ expect(delivery_zone).to be_present+ expect(delivery_zone.store_id).to eq(Spree::Store.default.id)+ expect(delivery_method.reload.delivery_zone_id).to eq(delivery_zone.id)+ end+ end++ it 'warns about delivery methods left without a zone' do+ zoneless = create(:delivery_method, name: 'Zoneless method')+ zoneless.update_columns(delivery_zone_id: nil)++ expect { Rake::Task['spree:migrate_zones_to_delivery_zones'].tap(&:reenable).invoke }.+ to output(/WARNING: 1 delivery methods have no delivery zone.*Zoneless method/m).to_stdout+ end+ end++ describe 'PromoteShippingCategoriesToDeliveryProfiles#copy_first_zone_onto_methods' do+ let(:connection) { ActiveRecord::Base.connection }+ let(:migration) do+ require Spree::Core::Engine.root.join('db/migrate/20260809150001_promote_shipping_categories_to_delivery_profiles.rb').to_s+ PromoteShippingCategoriesToDeliveryProfiles.new+ end+ let(:stored_method) { create(:delivery_method) }+ let(:unbound_method) { create(:delivery_method).tap { |method| method.update_columns(store_id: nil) } }++ # DDL commits implicitly on MySQL, so the table is created and dropped+ # outside the per-example transaction.+ before(:all) do+ ActiveRecord::Base.connection.create_table :spree_delivery_method_zones, force: true do |t|+ t.references :delivery_method, null: false+ t.references :delivery_zone, null: false+ t.timestamps+ end+ end++ after(:all) { ActiveRecord::Base.connection.drop_table :spree_delivery_method_zones, if_exists: true }++ it 'copies the first linked zone onto every method, including those without a store' do+ now = Time.current+ [[stored_method.id, 11], [unbound_method.id, 21], [unbound_method.id, 22]].each do |method_id, zone_id|+ connection.insert(+ "INSERT INTO spree_delivery_method_zones (delivery_method_id, delivery_zone_id, created_at, updated_at) " \+ "VALUES (#{method_id}, #{zone_id}, '#{now.to_fs(:db)}', '#{now.to_fs(:db)}')"+ )+ end++ silence_stream($stdout) { migration.send(:copy_first_zone_onto_methods) }++ expect(stored_method.reload.delivery_zone_id).to eq(11)+ expect(unbound_method.reload.delivery_zone_id).to eq(21)+ endenddescribe 'spree:migrate_adjustments_to_typed_rows' do