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 orphaned OptionValueVariant rows after product re-import (#15035)
Damian Legawiec09ca35e77ee3
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rbindex 0702357dcd..16a7faa06e 100644--- a/spree/core/app/models/spree/variant.rb+++ b/spree/core/app/models/spree/variant.rb@@ -71,7 +71,7 @@ module Spreehas_many :stock_reservationsend- has_many :option_value_variants, class_name: 'Spree::OptionValueVariant'+ has_many :option_value_variants, class_name: 'Spree::OptionValueVariant', dependent: :destroyhas_many :option_values, through: :option_value_variants, dependent: :destroy, class_name: 'Spree::OptionValue'has_many :images, -> { order(:position) }, as: :viewable, dependent: :destroy, class_name: 'Spree::Media'diff --git a/spree/core/db/migrate/20261031100001_delete_orphaned_spree_option_value_variants.rb b/spree/core/db/migrate/20261031100001_delete_orphaned_spree_option_value_variants.rbnew file mode 100644index 0000000000..4c91b2ec04--- /dev/null+++ b/spree/core/db/migrate/20261031100001_delete_orphaned_spree_option_value_variants.rb@@ -0,0 +1,10 @@+class DeleteOrphanedSpreeOptionValueVariants < ActiveRecord::Migration[8.1]+ # Replacing a variant's option_value_variants used to nullify the old join rows instead of+ # deleting them. Rows with no variant belong to nothing and block new variants from reusing+ # their option values through the uniqueness validation scoped to a NULL variant_id.+ def up+ execute 'DELETE FROM spree_option_value_variants WHERE variant_id IS NULL'+ end++ def down; end+enddiff --git a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rbindex 98cc6f6b76..805e706f68 100644--- a/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb+++ b/spree/core/spec/services/spree/imports/row_processors/product_variant_spec.rb@@ -423,6 +423,21 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service doexpect(variant.option_values.map(&:label).sort).to contain_exactly('Blue', 'XS')end+ it 'does not leave orphaned option value variants behind on re-import' do+ subject.process!++ expect(Spree::OptionValueVariant.where(variant_id: nil)).to be_empty+ expect(variant.reload.option_values.map(&:label)).to contain_exactly('Blue', 'XS')+ end++ it 'still allows a new variant to reuse the same option values after a re-import' do+ subject.process!++ other_data = csv_row_hash(row_data.merge('sku' => 'DENIM-SHIRT-XS-BLUE-2'))+ other_row = create(:import_row, import: import, data: other_data.to_json)+ expect { described_class.new(other_row).process! }.not_to raise_error+ end+context 'when updating inventory values' dobefore dostock_level = variant.stock_levels.find_or_initialize_by(stock_location: store.default_stock_location)