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: Store backfill migrations crashing on databases upgraded from earlier releases (#14927)
Damian Legawiec2c82f243aa6a
diff --git a/spree/core/db/migrate/20250902143122_fix_policies_store_association.rb b/spree/core/db/migrate/20250902143122_fix_policies_store_association.rbindex f08499990b..7bd45fed45 100644--- a/spree/core/db/migrate/20250902143122_fix_policies_store_association.rb+++ b/spree/core/db/migrate/20250902143122_fix_policies_store_association.rb@@ -2,10 +2,12 @@ class FixPoliciesStoreAssociation < ActiveRecord::Migration[7.2]def changeadd_reference :spree_policies, :owner, polymorphic: true, index: true- Spree::Policy.reset_column_information- Spree::Policy.all.each do |policy|- policy.update(owner_id: policy.store_id, owner_type: 'Spree::Store')- end+ policies = Arel::Table.new(:spree_policies)+ connection.update(+ Arel::UpdateManager.new.table(policies).set(+ [[policies[:owner_id], policies[:store_id]], [policies[:owner_type], 'Spree::Store']]+ )+ )remove_index :spree_policies, [:store_id, :slug], unique: true, if_exists: trueremove_index :spree_policies, [:store_id, :position], if_exists: truediff --git a/spree/core/db/migrate/20260515000001_add_store_id_to_spree_newsletter_subscribers.rb b/spree/core/db/migrate/20260515000001_add_store_id_to_spree_newsletter_subscribers.rbindex 30dd1546b5..159bde4097 100644--- a/spree/core/db/migrate/20260515000001_add_store_id_to_spree_newsletter_subscribers.rb+++ b/spree/core/db/migrate/20260515000001_add_store_id_to_spree_newsletter_subscribers.rb@@ -5,8 +5,17 @@ class AddStoreIdToSpreeNewsletterSubscribers < ActiveRecord::Migration[7.2]# For spree_multi_tenant we need handle the backfill and indices therereturn if defined?(SpreeMultiTenant)- default_store = Spree::Store.default- Spree::NewsletterSubscriber.update_all(store_id: default_store.id) if default_store&.persisted?+ # Read without the models: Spree::Store expects its columns (preferences,+ # metadata) in the format later migrations convert them to, so loading a+ # store here crashes on a database upgraded from an earlier release.+ stores = Arel::Table.new(:spree_stores)+ default_store_id = connection.select_value(+ stores.project(stores[:id]).where(stores[:deleted_at].eq(nil).and(stores[:default].eq(true))).take(1)+ )+ if default_store_id.present?+ subscribers = Arel::Table.new(:spree_newsletter_subscribers)+ connection.update(Arel::UpdateManager.new.table(subscribers).set([[subscribers[:store_id], default_store_id]]))+ endchange_column_null :spree_newsletter_subscribers, :store_id, falsediff --git a/spree/core/db/migrate/20260914230000_add_store_to_spree_option_types.rb b/spree/core/db/migrate/20260914230000_add_store_to_spree_option_types.rbindex d125ec05f8..a012ef5134 100644--- a/spree/core/db/migrate/20260914230000_add_store_to_spree_option_types.rb+++ b/spree/core/db/migrate/20260914230000_add_store_to_spree_option_types.rb@@ -5,8 +5,18 @@ class AddStoreToSpreeOptionTypes < ActiveRecord::Migration[8.1]# Existing option types were installation-wide, so they belong to the# store that has been using them. Left nullable: a fresh install has no# store yet, and the association enforces presence from the model side.- default_store = Spree::Store.where(default: true).first || Spree::Store.first- Spree::OptionType.update_all(store_id: default_store.id) if default_store+ # Read without the models: Spree::Store expects its columns (preferences,+ # metadata) in the format later migrations convert them to, so loading a+ # store here crashes on a database upgraded from an earlier release.+ stores = Arel::Table.new(:spree_stores)+ default_store_id = connection.select_value(+ stores.project(stores[:id]).where(stores[:deleted_at].eq(nil))+ .order(stores[:default].desc, stores[:id].asc).take(1)+ )+ if default_store_id.present?+ option_types = Arel::Table.new(:spree_option_types)+ connection.update(Arel::UpdateManager.new.table(option_types).set([[option_types[:store_id], default_store_id]]))+ end# Names are unique within a store now, so two stores may each define `size`.remove_index :spree_option_types, :name, unique: true, if_exists: true