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
Add 5.6 to 6.0 data migration tasks and upgrade manifest
Damian Legawiec598639810285
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.ymlindex 346fe86f3f..b35ac68731 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@@ -52,3 +52,83 @@ steps:Idempotent. Multi-tenant installs run the per-tenant override shipped byspree_multi_tenant.++ - id: migrate_shipping_to_delivery+ name: "Backfill fulfillment & delivery naming"+ task: "spree:migrate_shipping_to_delivery"+ notes: |+ Rewrites stored 'Spree::Shipment' class-name strings (state changes,+ stock movements, legacy adjustments), renames the terminal fulfillment+ status 'shipped' -> 'fulfilled', backfills fulfillment_type and the+ fulfillment provider on delivery methods (DigitalDelivery calculators+ become digital + the Digital provider) and copies the type onto existing+ fulfillments, assigns the Digital product type to products in+ digital-only shipping categories, and converts display_on into the+ storefront_visible boolean. Idempotent.++ - id: migrate_zones_to_delivery_zones+ name: "Convert delivery zones"+ task: "spree:migrate_zones_to_delivery_zones"+ notes: |+ Converts legacy Spree::Zone rows referenced by delivery methods into+ Spree::DeliveryZone records with typed country/state members and+ re-points the join rows. Run immediately after db:migrate, before+ creating new delivery zones, so unmigrated ids stay unambiguous.+ Tax-scoped zones are left for the tax-provider migration; unreferenced+ shipping zones are logged and skipped. Idempotent via a+ migrated_from_zone_id marker.++ - id: migrate_adjustments_to_typed_rows+ name: "Convert adjustments to typed rows"+ task: "spree:migrate_adjustments_to_typed_rows"+ notes: |+ Converts legacy spree_adjustments into TaxLine / Discount / Fee rows:+ tax-rate rows become tax lines with rate snapshots, eligible promotion+ rows become discounts (order-level amounts distributed across line items+ largest-remainder), sourceless rows map by sign to manual discounts or+ surcharges, and unknown sources become fees with provenance metadata.+ Order totals are never modified; per-order reconciliation freezes and+ logs any order whose typed sums don't match its stored totals.+ ReturnAuthorization adjustments freeze the order for the returns+ migration. spree_adjustments stays intact as the rollback source.+ Idempotent — orders that already have typed rows are skipped.++ - id: backfill_order_markets+ name: "Backfill order markets"+ task: "spree:backfill_order_markets"+ notes: |+ Market is mandatory in Spree 6. Assigns the store's default market to+ orders that predate markets. Channel needs no step here — the 5.4→5.5+ manifest's channels step already backfills channel_id on every upgrade+ path. Run before the cart conversion so converted carts inherit+ resolved values. The NOT NULL constraints on spree_orders land in 6.1.+ Idempotent.++ - id: migrate_incomplete_orders_to_carts+ name: "Convert incomplete orders to carts"+ task: "spree:migrate_incomplete_orders_to_carts"+ notes: |+ The final flip: every order that never completed nor canceled becomes a+ Spree::Cart with the same token, binding, addresses and totals. Line+ items, payments, payment sessions, reservations, coupon codes, applied+ promotions and typed adjustment rows are re-owned onto the cart and the+ hollow order row is deleted. Orders holding payment sessions convert+ last. Completed and canceled orders are untouched. Run AFTER+ migrate_adjustments_to_typed_rows so the typed rows exist to re-own.++ - id: migrate_calculator_bounds_to_delivery_method_rules+ name: "Convert calculator eligibility bounds to delivery method rules"+ task: "spree:migrate_calculator_bounds_to_delivery_method_rules"+ notes: |+ Folds the FlatRate calculator's min/max item-total and weight+ preferences into DeliveryMethodRule rows (eligibility separated from+ pricing — see 6.0-delivery-method-rules.md) and clears the migrated+ preferences so they aren't double-enforced. Run after+ migrate_shipping_to_delivery. Idempotent.++ - id: product_types_backfill+ name: "Backfill product type store ownership"+ task: "spree:product_types:backfill"+ notes: |+ Assigns store-less product types to the default store, removes orphaned+ option-type joins, and resets product counters. Idempotent.diff --git a/spree/core/lib/tasks/carts_migration.rake b/spree/core/lib/tasks/carts_migration.rakenew file mode 100644index 0000000000..6199d6a676--- /dev/null+++ b/spree/core/lib/tasks/carts_migration.rake@@ -0,0 +1,98 @@+# frozen_string_literal: true++# 5.6 → 6.0: convert incomplete orders into Spree::Cart rows+# (docs/plans/6.0-cart-order-split.md, Wave 7 — the last flip).+#+# Every order that never completed and was never canceled becomes a cart with+# the same token/binding; its money and checkout records are re-owned+# (cart_id set, order_id nulled) and the hollow order row is deleted.+# Completed and canceled orders are untouched (their cart_id stays nil).+# Orders holding payment sessions convert last so an interrupted run leaves+# the riskiest rows for the retry. Idempotent — converted orders are gone.+namespace :spree do+ desc 'Convert incomplete legacy orders into Spree::Cart rows'+ task migrate_incomplete_orders_to_carts: :environment do+ batch_size = ENV.fetch('BATCH_SIZE', 250).to_i+ stats = Hash.new(0)++ incomplete = Spree::Order.unscoped.where(completed_at: nil, canceled_at: nil)+ with_sessions_ids = incomplete.joins(:payment_sessions).distinct.ids+ plain_ids = incomplete.where.not(id: with_sessions_ids).ids++ puts "#{plain_ids.size} incomplete orders to convert (+#{with_sessions_ids.size} with payment sessions, converted last)"++ convert = lambda do |order|+ ActiveRecord::Base.transaction do+ cart = Spree::Cart.new(+ store_id: order.store_id,+ token: order.token,+ email: order.email,+ currency: order.currency,+ locale: order.locale,+ customer_id: order.user_id,+ market_id: order.market_id,+ channel_id: order.channel_id,+ ship_address_id: order.ship_address_id,+ bill_address_id: order.bill_address_id,+ accept_marketing: order.accept_marketing,+ special_instructions: order.special_instructions,+ last_ip_address: order.last_ip_address,+ gift_card_id: order.gift_card_id,+ metadata: order.metadata,+ item_total: order.item_total,+ total_quantity: order.total_quantity,+ adjustment_total: order.adjustment_total,+ included_tax_total: order.included_tax_total,+ additional_tax_total: order.additional_tax_total,+ taxable_adjustment_total: order.taxable_adjustment_total,+ non_taxable_adjustment_total: order.non_taxable_adjustment_total,+ discount_total: order.discount_total,+ fee_total: order.fee_total,+ delivery_total: order.delivery_total,+ payment_total: order.payment_total,+ total: order.total+ )+ # Raw preference write — the public setter validates pickup_enabled,+ # which must not reject historical selections.+ cart.assign_stock_location_id_preference(order.preferred_stock_location_id) if order.preferred_stock_location_id.present?+ # save!(validate: false) skips before_validation, so resolve the now+ # mandatory market/channel explicitly (legacy rows may have neither).+ cart.send(:ensure_market_presence)+ cart.send(:ensure_channel_presence)+ cart.save!(validate: false)+ cart.update_columns(created_at: order.created_at, updated_at: order.updated_at)++ # Re-own every dual-FK record. Money records move, never copy.+ [+ Spree::LineItem, Spree::Fulfillment, Spree::Payment, Spree::PaymentSession,+ Spree::StockReservation, Spree::CouponCode, Spree::OrderPromotion,+ Spree::TaxLine, Spree::Discount, Spree::Fee+ ].each do |klass|+ klass.unscoped.where(order_id: order.id).update_all(cart_id: cart.id, order_id: nil)+ end++ # Order-owned support rows that don't survive the conversion: the+ # order row must go, and these reference it without a cart leg.+ Spree::FulfillmentItem.unscoped.where(order_id: order.id).update_all(order_id: nil)+ Spree::StateChange.where(stateful_type: 'Spree::Order', stateful_id: order.id).delete_all++ order.delete+ stats[:converted] += 1+ end+ rescue StandardError => e+ stats[:errored] += 1+ puts "ERROR order #{order.number}: #{e.class} #{e.message}"+ end++ [plain_ids, with_sessions_ids].each do |ids|+ ids.each_slice(batch_size) do |slice|+ Spree::Order.unscoped.where(id: slice).each { |order| convert.call(order) }+ print '.'+ end+ end++ puts+ stats.each { |key, value| puts "#{key}: #{value}" }+ puts 'migrate_incomplete_orders_to_carts done.'+ end+enddiff --git a/spree/core/lib/tasks/delivery_migration.rake b/spree/core/lib/tasks/delivery_migration.rakenew file mode 100644index 0000000000..9a8e49fba3--- /dev/null+++ b/spree/core/lib/tasks/delivery_migration.rake@@ -0,0 +1,150 @@+# frozen_string_literal: true++# 5.6 → 6.0 data backfills for the fulfillment & delivery rename+# (docs/plans/6.0-fulfillment-and-delivery.md, Wave 7). Batched and+# idempotent — every statement narrows to legacy values only.+namespace :spree do+ desc 'Backfill fulfillment/delivery naming into stored strings, statuses and types'+ task migrate_shipping_to_delivery: :environment do+ batch_size = ENV.fetch('BATCH_SIZE', 5_000).to_i++ say = ->(message) { puts message }++ # 1. Stored polymorphic class names.+ {+ Spree::StateChange => :stateful_type,+ Spree::StockMovement => :originator_type+ }.each do |klass, column|+ count = klass.where(column => 'Spree::Shipment').in_batches(of: batch_size).update_all(column => 'Spree::Fulfillment')+ say.call "#{klass.table_name}.#{column}: #{count} rows Spree::Shipment → Spree::Fulfillment"+ end++ adjustments = Class.new(ActiveRecord::Base) { self.table_name = 'spree_adjustments' }+ if adjustments.table_exists?+ count = adjustments.where(adjustable_type: 'Spree::Shipment').in_batches(of: batch_size).update_all(adjustable_type: 'Spree::Fulfillment')+ say.call "spree_adjustments.adjustable_type: #{count} rows Spree::Shipment → Spree::Fulfillment"+ end++ # StateChange#name distinguishes the machine ('shipment' rows become+ # 'fulfillment'); the recorded states keep their history except the+ # renamed terminal value.+ Spree::StateChange.where(name: 'shipment').in_batches(of: batch_size).update_all(name: 'fulfillment')+ Spree::StateChange.where(name: 'fulfillment', previous_state: 'shipped').in_batches(of: batch_size).update_all(previous_state: 'fulfilled')+ Spree::StateChange.where(name: 'fulfillment', next_state: 'shipped').in_batches(of: batch_size).update_all(next_state: 'fulfilled')++ # 2. Status value rename on fulfillments ('shipped' → 'fulfilled').+ count = Spree::Fulfillment.unscoped.where(status: 'shipped').in_batches(of: batch_size).update_all(status: 'fulfilled')+ say.call "spree_fulfillments.status: #{count} rows shipped → fulfilled"++ # 3. fulfillment_type + provider backfill on delivery methods.+ digital_method_ids = Spree::DeliveryMethod.unscoped.+ joins("INNER JOIN #{Spree::Calculator.table_name} calculators ON calculators.calculable_id = #{Spree::DeliveryMethod.table_name}.id AND calculators.calculable_type IN ('Spree::ShippingMethod', 'Spree::DeliveryMethod')").+ where("calculators.type LIKE '%DigitalDelivery'").+ ids++ if digital_method_ids.any?+ Spree::DeliveryMethod.unscoped.where(id: digital_method_ids).update_all(+ fulfillment_type: 'digital',+ fulfillment_provider: 'Spree::FulfillmentProvider::Digital'+ )+ say.call "delivery methods marked digital: #{digital_method_ids.size}"+ end++ Spree::DeliveryMethod.unscoped.where(fulfillment_type: nil).update_all(fulfillment_type: 'shipping')+ Spree::DeliveryMethod.unscoped.where(fulfillment_provider: nil).update_all(fulfillment_provider: 'Spree::FulfillmentProvider::Manual')++ # Copy the resolved type onto existing fulfillments from their selected+ # delivery rate's method; anything unresolvable defaults to shipping.+ Spree::Fulfillment.unscoped.where(fulfillment_type: nil).in_batches(of: batch_size) do |batch|+ batch.each do |fulfillment|+ method_type = Spree::DeliveryMethod.unscoped.+ joins("INNER JOIN #{Spree::DeliveryRate.table_name} rates ON rates.delivery_method_id = #{Spree::DeliveryMethod.table_name}.id").+ where(rates: { fulfillment_id: fulfillment.id, selected: true }).+ pick(:fulfillment_type)+ fulfillment.update_columns(fulfillment_type: method_type || 'shipping')+ end+ print '.'+ end+ puts++ # 4. Digital shipping categories → the Digital product type. Products in a+ # category that only digital-delivery methods serve are digital products.+ digital_category_ids = Spree::ShippingCategory.+ joins("INNER JOIN spree_shipping_method_categories smc ON smc.shipping_category_id = #{Spree::ShippingCategory.table_name}.id").+ where(smc: { shipping_method_id: digital_method_ids }).+ ids.uniq++ if digital_category_ids.any?+ Spree::Store.all.find_each do |store|+ product_scope = store.products.where(shipping_category_id: digital_category_ids, product_type_id: nil)+ next unless product_scope.exists?++ digital_type = Spree::ProductType.where(store_id: store.id).detect(&:digital?) ||+ Spree::ProductType.create!(name: 'Digital', store_id: store.id, fulfillment_types: ['digital'])+ count = product_scope.in_batches(of: batch_size).update_all(product_type_id: digital_type.id)+ say.call "store #{store.code}: #{count} digital products assigned product type #{digital_type.id}"+ end+ end++ # 5. display_on → storefront_visible boolean.+ Spree::DeliveryMethod.unscoped.where(storefront_visible: nil).where(display_on: 'back_end').update_all(storefront_visible: false)+ Spree::DeliveryMethod.unscoped.where(storefront_visible: nil).update_all(storefront_visible: true)++ say.call 'migrate_shipping_to_delivery done.'+ end++ desc 'Convert delivery-referenced legacy Zones into DeliveryZones with typed members'+ task migrate_zones_to_delivery_zones: :environment do+ join_table = Spree::DeliveryMethodZone.table_name++ # Rows whose delivery_zone_id predates the rename still point at legacy+ # spree_zones ids. Run this immediately after db:migrate — before new+ # DeliveryZones are created — so unmigrated ids are unambiguous.+ migrated = {}+ Spree::DeliveryZone.find_each do |delivery_zone|+ source_id = delivery_zone.metadata&.dig('migrated_from_zone_id')+ migrated[source_id.to_i] = delivery_zone.id if source_id+ end++ referenced_ids = Spree::DeliveryMethodZone.distinct.pluck(:delivery_zone_id).compact+ legacy_zones = Spree::Zone.where(id: referenced_ids - migrated.values)++ legacy_zones.find_each do |zone|+ next if migrated.key?(zone.id)++ 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"+ next+ end++ delivery_zone = Spree::DeliveryZone.create!(+ name: zone.name,+ description: zone.description,+ metadata: { 'migrated_from_zone_id' => zone.id }+ )++ zone.zone_members.find_each do |member|+ case member.zoneable_type+ when 'Spree::Country'+ delivery_zone.members.create!(member_type: 'country', country_id: member.zoneable_id)+ when 'Spree::State'+ state = Spree::State.find_by(id: member.zoneable_id)+ next unless state++ delivery_zone.members.create!(member_type: 'state', state_id: state.id, country_id: state.country_id)+ end+ end++ Spree::DeliveryMethodZone.where(delivery_zone_id: zone.id).update_all(delivery_zone_id: delivery_zone.id)+ migrated[zone.id] = delivery_zone.id+ puts "zone #{zone.id} (#{zone.name}) → delivery zone #{delivery_zone.id} (#{delivery_zone.members.count} members)"+ end++ unreferenced = Spree::Zone.where.not(id: referenced_ids).where(kind: 'shipping')+ unreferenced.find_each do |zone|+ puts "SKIP unreferenced shipping zone #{zone.id} (#{zone.name}) — no delivery method uses it"+ end++ puts 'migrate_zones_to_delivery_zones done. Tax-scoped zones stay on Spree::Zone for the tax provider migration.'+ end+enddiff --git a/spree/core/lib/tasks/order_market_backfill.rake b/spree/core/lib/tasks/order_market_backfill.rakenew file mode 100644index 0000000000..3fefcba0d9--- /dev/null+++ b/spree/core/lib/tasks/order_market_backfill.rake@@ -0,0 +1,27 @@+# frozen_string_literal: true++# 5.6 → 6.0: market is mandatory on orders in Spree 6. Orders created before+# markets existed carry NULLs — backfill from the store's default market so+# the 6.1 NOT NULL constraint can land. (channel_id has no equivalent task+# here: the 5.4→5.5 manifest's spree:channels:backfill_order_channel_ids+# already covers it on every upgrade path.)+namespace :spree do+ desc 'Backfill missing market on orders from the store default market'+ task backfill_order_markets: :environment do+ batch_size = ENV.fetch('BATCH_SIZE', 5_000).to_i++ Spree::Store.all.find_each do |store|+ market_id = store.default_market&.id++ if market_id+ count = Spree::Order.unscoped.where(store_id: store.id, market_id: nil).+ in_batches(of: batch_size).update_all(market_id: market_id)+ puts "store #{store.code}: #{count} orders assigned market #{market_id}" if count.positive?+ else+ puts "store #{store.code}: no default market — orders left as-is"+ end+ end++ puts 'backfill_order_markets done. The NOT NULL constraint lands in 6.1.'+ end+enddiff --git a/spree/core/lib/tasks/products.rake b/spree/core/lib/tasks/products.rakeindex a0b43108f3..7c2c4995bc 100644--- a/spree/core/lib/tasks/products.rake+++ b/spree/core/lib/tasks/products.rake@@ -1,4 +1,23 @@namespace :spree do+ namespace :product_types do+ desc 'Backfill store ownership on product types and clean orphaned joins'+ task backfill: :environment do+ default_store = Spree::Store.default+ abort 'No default store found' unless default_store&.persisted?++ count = Spree::ProductType.where(store_id: nil).update_all(store_id: default_store.id)+ puts "product types assigned to #{default_store.code}: #{count}"++ orphans = Spree::OptionTypeProductType.where.missing(:product_type)+ puts "orphaned option-type joins removed: #{orphans.delete_all}"++ Spree::ProductType.find_each do |product_type|+ Spree::ProductType.reset_counters(product_type.id, :products)+ end+ puts 'product counters reset.'+ end+ end+namespace :products dodesc 'Reset counter caches (variant_count, categories_count, media_count) on products'task reset_counter_caches: :environment do |_t, _args|diff --git a/spree/core/lib/tasks/typed_adjustments_migration.rake b/spree/core/lib/tasks/typed_adjustments_migration.rakenew file mode 100644index 0000000000..c53f5df35a--- /dev/null+++ b/spree/core/lib/tasks/typed_adjustments_migration.rake@@ -0,0 +1,189 @@+# frozen_string_literal: true++# 5.6 → 6.0: convert legacy polymorphic spree_adjustments rows into the typed+# TaxLine / Discount / Fee tables (docs/plans/6.0-split-adjustments.md, Wave 7).+#+# Contract:+# - Legacy rows are READ ONLY — spree_adjustments stays in place through 6.0+# and remains the rollback source.+# - Order totals are never touched; a per-order reconciliation asserts the+# typed sums match the order's stored totals. Orders that do not reconcile+# are logged and marked recalculation-frozen (private_metadata flag), never+# force-balanced.+# - Idempotent: orders with any typed rows already present are skipped.+namespace :spree do+ desc 'Convert legacy spree_adjustments into typed TaxLine/Discount/Fee rows'+ task migrate_adjustments_to_typed_rows: :environment do+ legacy = Class.new(ActiveRecord::Base) { self.table_name = 'spree_adjustments' }+ abort 'spree_adjustments table not found — nothing to migrate' unless legacy.table_exists?++ batch_size = ENV.fetch('BATCH_SIZE', 500).to_i+ stats = Hash.new(0)++ order_ids = legacy.distinct.pluck(:order_id)+ puts "#{order_ids.size} orders carry legacy adjustments"++ order_ids.each_slice(batch_size) do |slice|+ Spree::Order.unscoped.where(id: slice).includes(:line_items).each do |order|+ if Spree::TaxLine.where(order_id: order.id).exists? ||+ Spree::Discount.where(order_id: order.id).exists? ||+ Spree::Fee.where(order_id: order.id).exists?+ stats[:skipped_already_typed] += 1+ next+ end++ rows = legacy.where(order_id: order.id).to_a+ frozen_reason = nil++ ActiveRecord::Base.transaction do+ rows.each do |row|+ adjustable = Spree::TypedAdjustmentsMigration.resolve_typed_adjustable(order, row)++ case row.source_type+ when 'Spree::TaxRate'+ next unless row.eligible++ tax_rate = Spree::TaxRate.with_deleted.find_by(id: row.source_id)+ Spree::TaxLine.create!(+ order_id: order.id,+ line_item: adjustable[:line_item],+ fulfillment: adjustable[:fulfillment],+ tax_rate_id: tax_rate&.id,+ amount: row.amount,+ rate: tax_rate&.amount || 0,+ label: row.label.to_s.presence || 'Tax',+ included: row.included,+ provider_id: 'internal'+ )+ stats[:tax_lines] += 1+ when 'Spree::PromotionAction'+ next unless row.eligible++ action = Spree::PromotionAction.with_deleted.find_by(id: row.source_id)+ promotion = action&.promotion+ Spree::TypedAdjustmentsMigration.build_distributed_discounts(order, row, adjustable, promotion, action, stats)+ when nil+ if row.amount.to_f.negative?+ Spree::TypedAdjustmentsMigration.build_distributed_discounts(order, row, adjustable, nil, nil, stats, kind: 'manual')+ else+ Spree::Fee.create!(+ order_id: order.id,+ line_item: adjustable[:line_item],+ fulfillment: adjustable[:fulfillment],+ amount: row.amount,+ label: row.label.to_s.presence || 'Fee',+ kind: 'surcharge'+ )+ stats[:fees] += 1+ end+ when 'Spree::ReturnAuthorization'+ frozen_reason = 'return_authorization_adjustment'+ stats[:skipped_return_authorization] += 1+ else+ Spree::Fee.create!(+ order_id: order.id,+ line_item: adjustable[:line_item],+ fulfillment: adjustable[:fulfillment],+ amount: [row.amount, 0].max,+ label: row.label.to_s.presence || row.source_type,+ kind: 'surcharge',+ metadata: { 'legacy_source_type' => row.source_type, 'legacy_source_id' => row.source_id }+ )+ stats[:fees_unknown_source] += 1+ end+ end++ # Reconciliation: typed sums must match the stored order totals the+ # customer was charged. discount_total compares against promotion rows;+ # tax against tax lines.+ unless frozen_reason+ promo_delta = (Spree::Discount.where(order_id: order.id, kind: 'promotion').sum(:amount) - order.discount_total.to_d).abs+ tax_delta = (Spree::TaxLine.where(order_id: order.id, included: false).sum(:amount) - order.additional_tax_total.to_d).abs+ frozen_reason = 'totals_do_not_reconcile' if promo_delta > 0.01 || tax_delta > 0.01+ end++ if frozen_reason+ order.update_columns(+ private_metadata: (order.private_metadata || {}).merge(+ 'typed_adjustments_frozen' => frozen_reason,+ 'typed_adjustments_frozen_at' => Time.current.iso8601+ )+ )+ stats[:frozen_orders] += 1+ puts "FROZEN order #{order.number}: #{frozen_reason}"+ end+ end+ rescue StandardError => e+ stats[:errored_orders] += 1+ puts "ERROR order #{order.number}: #{e.class} #{e.message}"+ end+ print '.'+ end++ puts+ stats.each { |key, value| puts "#{key}: #{value}" }+ puts 'migrate_adjustments_to_typed_rows done. spree_adjustments left intact for rollback.'+ end+end++module Spree+ module TypedAdjustmentsMigration+ module_function++ # Resolves the legacy adjustable into the typed target hash. Order-level+ # rows return an empty target — the caller distributes them across line items.+ def resolve_typed_adjustable(order, row)+ case row.adjustable_type+ when 'Spree::LineItem'+ line_item = order.line_items.detect { |item| item.id == row.adjustable_id }+ { line_item: line_item }+ when 'Spree::Fulfillment', 'Spree::Shipment'+ { fulfillment: Spree::Fulfillment.unscoped.find_by(id: row.adjustable_id) }+ else+ {}+ end+ end++ # Line/fulfillment-targeted discounts become one row; order-level amounts+ # are distributed across line items largest-remainder over their amounts,+ # matching the runtime adjuster so migrated and fresh data split identically.+ def build_distributed_discounts(order, row, adjustable, promotion, action, stats, kind: 'promotion')+ base_attributes = {+ order_id: order.id,+ label: row.label.to_s.presence || promotion&.name || 'Discount',+ kind: kind,+ code: promotion&.code.presence,+ promotion_id: promotion&.id,+ promotion_action_id: action&.id+ }++ if adjustable[:line_item] || adjustable[:fulfillment]+ Spree::Discount.create!(+ base_attributes.merge(+ line_item: adjustable[:line_item],+ fulfillment: adjustable[:fulfillment],+ amount: [row.amount, 0].min+ )+ )+ stats[:discounts] += 1+ return+ end++ line_items = order.line_items.to_a+ bases = line_items.map { |item| [item.amount.to_d, BigDecimal(0)].max }+ bases_sum = bases.sum+ return if bases_sum <= 0++ total_cents = ([row.amount.to_d.abs, bases_sum].min * 100).round+ shares = Spree::Adjusters::LargestRemainder.largest_remainder_shares(total_cents, bases)++ line_items.each_with_index do |item, index|+ amount = -BigDecimal(shares[index]) / 100+ next if amount.zero?++ Spree::Discount.create!(base_attributes.merge(line_item: item, amount: amount))+ stats[:discounts_distributed] += 1+ end+ end+ end+enddiff --git a/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rbnew file mode 100644index 0000000000..92a88e26c2--- /dev/null+++ b/spree/core/spec/lib/tasks/six_zero_data_migrations_spec.rb@@ -0,0 +1,225 @@+require 'spec_helper'+require 'rake'++describe '6.0 data migration tasks' do+ before(:all) do+ Rake::Task.define_task(:environment)+ load Spree::Core::Engine.root.join('lib', 'tasks', 'delivery_migration.rake')+ load Spree::Core::Engine.root.join('lib', 'tasks', 'typed_adjustments_migration.rake')+ load Spree::Core::Engine.root.join('lib', 'tasks', 'carts_migration.rake')+ load Spree::Core::Engine.root.join('lib', 'tasks', 'products.rake')+ load Spree::Core::Engine.root.join('lib', 'tasks', 'order_market_backfill.rake')+ end++ let(:store) { @default_store }++ def run_task(name)+ task = Rake::Task[name]+ task.reenable+ silence_stream($stdout) { task.invoke }+ end++ def silence_stream(stream)+ old = stream.dup+ stream.reopen(File::NULL, 'w')+ yield+ ensure+ stream.reopen(old)+ end++ describe 'spree:migrate_shipping_to_delivery' do+ let!(:order) { create(:order_with_line_items, store: store) }+ let(:fulfillment) { order.fulfillments.first }++ it 'renames stored class-name strings and the shipped status' do+ fulfillment.update_columns(status: 'shipped', fulfillment_type: nil)+ Spree::StateChange.create!(stateful_type: 'Spree::Shipment', stateful_id: fulfillment.id,+ name: 'shipment', previous_state: 'ready', next_state: 'shipped')++ run_task('spree:migrate_shipping_to_delivery')++ expect(fulfillment.reload.status).to eq('fulfilled')+ expect(fulfillment.fulfillment_type).to eq('shipping')+ change = Spree::StateChange.where(stateful_id: fulfillment.id).last+ expect(change.stateful_type).to eq('Spree::Fulfillment')+ expect(change.name).to eq('fulfillment')+ expect(change.next_state).to eq('fulfilled')+ end++ it 'marks digital-delivery methods and converts display_on' do+ digital_method = create(:shipping_method)+ digital_method.calculator.destroy!+ Spree::Calculator::Shipping::DigitalDelivery.create!(calculable: digital_method)+ digital_method.update_columns(fulfillment_type: nil, fulfillment_provider: nil, storefront_visible: nil, display_on: 'back_end')++ run_task('spree:migrate_shipping_to_delivery')++ digital_method.reload+ expect(digital_method.fulfillment_type).to eq('digital')+ expect(digital_method.fulfillment_provider).to eq('Spree::FulfillmentProvider::Digital')+ expect(digital_method.read_attribute(:storefront_visible)).to be(false)+ end++ it 'is idempotent' do+ fulfillment.update_columns(status: 'shipped')+ run_task('spree:migrate_shipping_to_delivery')+ expect { run_task('spree:migrate_shipping_to_delivery') }.not_to raise_error+ expect(fulfillment.reload.status).to eq('fulfilled')+ end+ end++ describe 'spree:migrate_zones_to_delivery_zones' do+ let!(:country) { Spree::Country.find_by(iso: 'US') || create(:country_us) }+ let!(:zone) do+ zone = Spree::Zone.create!(name: "Legacy Ship Zone #{Time.current.to_f}", kind: 'shipping')+ zone.zone_members.create!(zoneable: country)+ zone+ end+ let!(:delivery_method) { create(:shipping_method) }++ before do+ Spree::DeliveryMethodZone.create!(delivery_method_id: delivery_method.id, delivery_zone_id: zone.id)+ end++ it 'converts referenced zones into delivery zones and re-points the joins' 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.metadata['migrated_from_zone_id']).to eq(zone.id)+ expect(delivery_zone.members.pluck(:member_type, :country_id)).to eq([['country', country.id]])+ expect(Spree::DeliveryMethodZone.where(delivery_method_id: delivery_method.id).pluck(:delivery_zone_id)).to eq([delivery_zone.id])+ end++ it 'is idempotent' do+ run_task('spree:migrate_zones_to_delivery_zones')+ expect { run_task('spree:migrate_zones_to_delivery_zones') }.not_to change(Spree::DeliveryZone, :count)+ end+ end++ describe 'spree:migrate_adjustments_to_typed_rows' do+ let!(:order) { create(:completed_order_with_totals, store: store) }+ let(:line_item) { order.line_items.first }+ let(:legacy) { Class.new(ActiveRecord::Base) { self.table_name = 'spree_adjustments' } }++ before do+ order.tax_lines.delete_all+ order.discounts.delete_all+ order.fees.delete_all+ end++ it 'maps tax, promotion and sourceless rows onto typed tables without touching totals' do+ tax_rate = create(:tax_rate, amount: 0.1)+ promotion = create(:promotion, name: 'Legacy promo', code: 'LEGACY')+ action = Spree::Promotion::Actions::CreateItemAdjustments.create!(promotion: promotion)++ legacy.create!(order_id: order.id, adjustable_type: 'Spree::LineItem', adjustable_id: line_item.id,+ source_type: 'Spree::TaxRate', source_id: tax_rate.id, amount: 1.5, label: 'Tax 10%',+ eligible: true, included: false)+ legacy.create!(order_id: order.id, adjustable_type: 'Spree::LineItem', adjustable_id: line_item.id,+ source_type: 'Spree::PromotionAction', source_id: action.id, amount: -2, label: 'Promo',+ eligible: true, included: false)+ legacy.create!(order_id: order.id, adjustable_type: 'Spree::Order', adjustable_id: order.id,+ source_type: nil, source_id: nil, amount: 3, label: 'Handling', eligible: true, included: false)++ order.update_columns(discount_total: -2, additional_tax_total: 1.5)+ totals_before = order.reload.attributes.slice('total', 'discount_total', 'additional_tax_total')++ run_task('spree:migrate_adjustments_to_typed_rows')++ tax_line = Spree::TaxLine.find_by(order_id: order.id)+ expect(tax_line.amount).to eq(1.5)+ expect(tax_line.rate).to eq(0.1)+ expect(tax_line.line_item_id).to eq(line_item.id)++ discount = Spree::Discount.find_by(order_id: order.id)+ expect(discount.amount).to eq(-2)+ expect(discount.kind).to eq('promotion')+ expect(discount.code).to eq('legacy')++ fee = Spree::Fee.find_by(order_id: order.id)+ expect(fee.amount).to eq(3)+ expect(fee.kind).to eq('surcharge')++ expect(order.reload.attributes.slice('total', 'discount_total', 'additional_tax_total')).to eq(totals_before)+ expect(order.private_metadata['typed_adjustments_frozen']).to be_nil+ end++ it 'freezes orders whose typed sums do not reconcile' do+ legacy.create!(order_id: order.id, adjustable_type: 'Spree::LineItem', adjustable_id: line_item.id,+ source_type: 'Spree::PromotionAction', source_id: nil, amount: -2, label: 'Ghost promo',+ eligible: true, included: false)+ order.update_columns(discount_total: -10)++ run_task('spree:migrate_adjustments_to_typed_rows')++ expect(order.reload.private_metadata['typed_adjustments_frozen']).to eq('totals_do_not_reconcile')+ end++ it 'skips orders that already carry typed rows' do+ create(:fee, order: order, amount: 1, label: 'Existing')+ legacy.create!(order_id: order.id, adjustable_type: 'Spree::Order', adjustable_id: order.id,+ source_type: nil, source_id: nil, amount: 3, label: 'Handling', eligible: true, included: false)++ expect { run_task('spree:migrate_adjustments_to_typed_rows') }.not_to change { Spree::Fee.where(order_id: order.id).count }+ end+ end++ describe 'spree:migrate_incomplete_orders_to_carts' do+ let!(:incomplete_order) do+ create(:order_with_line_items, store: store).tap do |order|+ order.update_columns(completed_at: nil, status: 'draft')+ end+ end+ let!(:completed_order) { create(:completed_order_with_totals, store: store) }++ it 'converts incomplete orders into carts and re-owns their records' do+ line_item_ids = incomplete_order.line_items.ids+ token = incomplete_order.token+ order_id = incomplete_order.id++ run_task('spree:migrate_incomplete_orders_to_carts')++ cart = Spree::Cart.find_by(token: token)+ expect(cart).to be_present+ expect(cart.store_id).to eq(store.id)+ expect(cart.line_items.ids).to match_array(line_item_ids)+ expect(Spree::LineItem.where(id: line_item_ids).pluck(:order_id).uniq).to eq([nil])+ expect(Spree::Order.unscoped.exists?(order_id)).to be(false)+ end++ it 'leaves completed orders untouched' do+ run_task('spree:migrate_incomplete_orders_to_carts')++ expect(Spree::Order.unscoped.exists?(completed_order.id)).to be(true)+ expect(completed_order.reload.completed_at).to be_present+ end++ it 'is idempotent' do+ run_task('spree:migrate_incomplete_orders_to_carts')+ expect { run_task('spree:migrate_incomplete_orders_to_carts') }.not_to change(Spree::Cart, :count)+ end+ end++ describe 'spree:backfill_order_markets' do+ it 'assigns the store default market to orders missing one' do+ order = create(:order, store: store)+ order.update_columns(market_id: nil)++ run_task('spree:backfill_order_markets')++ expect(order.reload.market_id).to eq(store.default_market.id)+ end+ end++ describe 'spree:product_types:backfill' do+ it 'assigns store-less product types to the default store' do+ product_type = Spree::ProductType.create!(name: "Orphan #{Time.current.to_f}", fulfillment_types: ['shipping'])+ product_type.update_columns(store_id: nil)++ run_task('spree:product_types:backfill')++ expect(product_type.reload.store_id).to eq(Spree::Store.default.id)+ end+ end+end