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.

On products CSV import, create and assing categories to products separaterly to avoid race conditions and deadlocks (#14024)

Kacper Mękarskicddc61396c16
diff --git a/spree/core/app/jobs/spree/imports/create_categories_job.rb b/spree/core/app/jobs/spree/imports/create_categories_job.rb
new file mode 100644
index 0000000000..2fcb969ff8
--- /dev/null
+++ b/spree/core/app/jobs/spree/imports/create_categories_job.rb
@@ -0,0 +1,35 @@
+module Spree
+ module Imports
+ class CreateCategoriesJob < Spree::BaseJob
+ queue_as Spree.queues.imports
+ retry_on StandardError, wait: :polynomially_longer, attempts: 5
+
+ def perform(product_id, store_id, taxon_pretty_names)
+ product = Spree::Product.find(product_id)
+ store = Spree::Store.find(store_id)
+ taxons = taxon_pretty_names.filter_map { |taxon_pretty_name| find_or_create_taxon(store, taxon_pretty_name) }
+
+ product.taxons = taxons
+ end
+
+ private
+
+ def find_or_create_taxon(store, taxon_pretty_name)
+ taxon_names = taxon_pretty_name.strip.split('->').map(&:strip).map(&:presence).compact
+ return if taxon_names.empty?
+
+ taxonomy_name = taxon_names.shift
+ taxonomy = store.taxonomies.with_matching_name(taxonomy_name).first || store.taxonomies.create!(name: taxonomy_name)
+
+ last_taxon = taxonomy.root
+
+ taxon_names.each do |taxon_name|
+ last_taxon = taxonomy.taxons.with_matching_name(taxon_name).where(parent: last_taxon).first ||
+ taxonomy.taxons.create!(name: taxon_name, parent: last_taxon)
+ end
+
+ last_taxon
+ end
+ end
+ end
+end
diff --git a/spree/core/app/services/spree/imports/row_processors/product_variant.rb b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
index 5d904c80e0..d8466fda82 100644
--- a/spree/core/app/services/spree/imports/row_processors/product_variant.rb
+++ b/spree/core/app/services/spree/imports/row_processors/product_variant.rb
@@ -68,7 +68,10 @@ module Spree
product = assign_attributes_to_product(product)
product.save!
- handle_metafields(product) if has_product_attributes?
+ if has_product_attributes?
+ handle_metafields(product)
+ handle_categories(product)
+ end
product
else
# For non-master variants, only look up the product
@@ -105,11 +108,6 @@ module Spree
shipping_category = prepare_shipping_category
product.shipping_category = shipping_category if shipping_category.present?
end
-
- taxons = prepare_taxons
- # Full product rows (with name/status/description) clear taxons when categories are blank.
- # Price-only rows (no product attributes) never touch taxons.
- product.taxons = taxons if taxons.any? || has_product_attributes?
end
product
@@ -125,35 +123,6 @@ module Spree
Spree::TaxCategory.find_by(name: tax_category_name)
end
- def prepare_taxons
- taxon_pretty_names = [
- attributes['category1'],
- attributes['category2'],
- attributes['category3']
- ].compact_blank.map(&:strip).uniq
-
- return [] if taxon_pretty_names.empty?
-
- taxons = taxon_pretty_names.map { |taxon_pretty_name| handle_taxon_line(taxon_pretty_name) }
- taxons.compact
- end
-
- def handle_taxon_line(taxon_pretty_name)
- taxon_names = taxon_pretty_name.strip.split('->').map(&:strip).map(&:presence).compact
- return if taxon_names.empty?
-
- taxonomy_name = taxon_names.shift
- taxonomy = store.taxonomies.with_matching_name(taxonomy_name).first || store.taxonomies.create!(name: taxonomy_name)
-
- last_taxon = taxonomy.root
-
- taxon_names.each do |taxon_name|
- last_taxon = taxonomy.taxons.with_matching_name(taxon_name).where(parent: last_taxon).first || taxonomy.taxons.create!(name: taxon_name, parent: last_taxon)
- end
-
- last_taxon
- end
-
def prepare_option_value_variants
return [] if options.empty?
@@ -263,6 +232,18 @@ module Spree
product.update(metafields_attributes: nested_attrs) unless nested_attrs.empty?
end
+ def handle_categories(product)
+ Spree::Imports::CreateCategoriesJob.perform_later(product.id, store.id, prepare_taxon_pretty_names)
+ end
+
+ def prepare_taxon_pretty_names
+ [
+ attributes['category1'],
+ attributes['category2'],
+ attributes['category3']
+ ].compact_blank.map(&:strip).uniq
+ end
+
def to_spree_status(status)
case status.strip.downcase
when 'active'
diff --git a/spree/core/app/services/spree/sample_data/loader.rb b/spree/core/app/services/spree/sample_data/loader.rb
index 2ae61f2be4..2ea08cd75c 100644
--- a/spree/core/app/services/spree/sample_data/loader.rb
+++ b/spree/core/app/services/spree/sample_data/loader.rb
@@ -24,6 +24,9 @@ module Spree
puts 'Loading sample products...'
load_products
+ puts 'Loading sample categories...'
+ load_categories
+
puts 'Loading sample product translations...'
load_product_translations
@@ -67,6 +70,22 @@ module Spree
Spree::SampleData::ImportRunner.call(csv_path: csv_path, import_class: Spree::Imports::Products)
end
+ def load_categories
+ store = Spree::Store.default
+ csv_path = sample_data_path.join('products.csv')
+
+ require 'csv'
+ ::CSV.foreach(csv_path, headers: true) do |row|
+ product = store.products.find_by(slug: row['slug'])
+ next unless product
+
+ categories = [row['category1'], row['category2'], row['category3']].compact_blank
+ next if categories.empty?
+
+ Spree::Imports::CreateCategoriesJob.perform_now(product.id, store.id, categories)
+ end
+ end
+
def load_product_translations
csv_path = sample_data_path.join('product_translations.csv')
return unless csv_path.exist?
diff --git a/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
new file mode 100644
index 0000000000..18c8b91179
--- /dev/null
+++ b/spree/core/spec/jobs/spree/imports/create_categories_job_spec.rb
@@ -0,0 +1,78 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Imports::CreateCategoriesJob, type: :job do
+ let(:store) { @default_store }
+ let!(:product) { create(:product, stores: [store]) }
+
+ describe '#perform' do
+ it 'creates taxonomies and taxons and assigns them to the product' do
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing -> Shirts', 'Brands -> Nike'])
+
+ product.reload
+ expect(product.taxons.map(&:pretty_name)).to contain_exactly(
+ 'Men -> Clothing -> Shirts',
+ 'Brands -> Nike'
+ )
+
+ men_taxonomy = store.taxonomies.find_by(name: 'Men')
+ expect(men_taxonomy).to be_present
+ expect(men_taxonomy.taxons.find_by(name: 'Clothing')).to be_present
+ expect(men_taxonomy.taxons.find_by(name: 'Shirts')).to be_present
+
+ brands_taxonomy = store.taxonomies.find_by(name: 'Brands')
+ expect(brands_taxonomy).to be_present
+ expect(brands_taxonomy.taxons.find_by(name: 'Nike')).to be_present
+ end
+
+ it 'skips invalid category paths' do
+ described_class.perform_now(product.id, store.id, ['Men -> -> Shirts', ' -> ', ' '])
+
+ expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly('Men -> Shirts')
+ end
+
+ it 'is idempotent' do
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing'])
+
+ expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly('Men -> Clothing')
+ end
+
+ context 'when taxonomies and taxons already exist' do
+ let!(:men_taxonomy) { create(:taxonomy, name: 'Men', store: store) }
+ let!(:clothing_taxon) { create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
+ let!(:shirts_taxon) { create(:taxon, name: 'Shirts', taxonomy: men_taxonomy, parent: clothing_taxon) }
+
+ it 'reuses existing taxonomies and taxons' do
+ expect {
+ described_class.perform_now(product.id, store.id, ['Men -> Clothing -> Shirts'])
+ }.not_to change { Spree::Taxon.count }
+
+ expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
+ 'Men -> Clothing -> Shirts'
+ )
+ end
+
+ it 'matches taxonomies and taxons by case insensitive name' do
+
+ described_class.perform_now(product.id, store.id, ['men -> clothing -> shirts'])
+
+ expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
+ 'Men -> Clothing -> Shirts'
+ )
+ end
+
+ context 'when given an empty list' do
+ before do
+ product.taxons = [shirts_taxon]
+ product.save!
+ end
+
+ it 'clears existing taxons' do
+ expect {
+ described_class.perform_now(product.id, store.id, [])
+ }.to change { product.reload.taxons.count }.from(1).to(0)
+ end
+ end
+ end
+ end
+end
diff --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.rb
index 14ce90f7a8..55d6cb0c40 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
@@ -346,8 +346,7 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
end
context 'with taxons' do
- let!(:product) { create(:product, slug: 'denim-shirt', name: 'Denim Shirt', stores: [store], taxons: taxons) }
- let(:taxons) { [] }
+ let!(:product) { create(:product, slug: 'denim-shirt', name: 'Denim Shirt', stores: [store]) }
let(:row_data) do
csv_row_hash(
@@ -358,54 +357,15 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
)
end
- it 'assigns taxons to the product' do
- expect { subject.process! }.to change { Spree::Taxon.count }.by(8) # 3 root taxons (from 3 taxonomies) + 5 leaf taxons
-
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
- 'Men -> Clothing -> Shirts',
- 'Brands -> Awesome Brand',
- 'Collections -> Summer -> Shirts'
- )
- end
-
- context 'when the taxons already exist' do
- let(:men_taxonomy) { store.taxonomies.find_by(name: 'Men') || create(:taxonomy, name: 'Men', store: store) }
- let!(:clothing_taxon) { create(:taxon, name: 'clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
- let!(:shirts_category_taxon) { create(:taxon, name: 'Shirts', taxonomy: men_taxonomy, parent: clothing_taxon) }
-
- let(:brands_taxonomy) { store.taxonomies.find_by(name: 'Brands') || create(:taxonomy, name: 'Brands', store: store) }
- let!(:awesome_brand_taxon) { create(:taxon, name: 'Awesome brand', taxonomy: brands_taxonomy, parent: brands_taxonomy.root) }
-
- let(:collections_taxonomy) { store.taxonomies.find_by(name: 'Collections') || create(:taxonomy, name: 'Collections', store: store) }
- let!(:summer_taxon) { create(:taxon, name: 'Summer', taxonomy: collections_taxonomy, parent: collections_taxonomy.root) }
- let!(:shirts_collection_taxon) { create(:taxon, name: 'Shirts', taxonomy: collections_taxonomy, parent: summer_taxon) }
-
- let(:row_data) do
- csv_row_hash(
- 'slug' => 'denim-shirt',
- 'category1' => 'men -> Clothing -> Shirts',
- 'category2' => 'brands -> awesome brand',
- 'category3' => 'Collections -> Summer -> Shirts'
- )
- end
-
- it 'assigns the existing taxons to the product' do
- expect { subject.process! }.to change { Spree::Taxon.count }.by(0)
-
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
- 'Men -> clothing -> Shirts',
- 'Brands -> Awesome brand',
- 'Collections -> Summer -> Shirts'
- )
- end
+ it 'enqueues CreateCategoriesJob with category paths' do
+ expect {
+ subject.process!
+ }.to have_enqueued_job(Spree::Imports::CreateCategoriesJob)
+ .with(product.id, store.id, ['Men -> Clothing -> Shirts', 'Brands -> Awesome Brand', 'Collections -> Summer -> Shirts'])
+ .on_queue(Spree.queues.imports)
end
- context 'when taxons are not provided' do
- let(:men_taxonomy) { store.taxonomies.find_by(name: 'Men') || create(:taxonomy, name: 'Men', store: store) }
- let!(:clothing_taxon) { create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
-
- let(:taxons) { [clothing_taxon] }
-
+ context 'when categories are blank' do
let(:row_data) do
csv_row_hash(
'slug' => 'denim-shirt',
@@ -415,37 +375,15 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
)
end
- it 'assigns no taxons to the product' do
- expect { subject.process! }.to change { Spree::Taxon.count }.by(0)
- expect(product.reload.taxons).to be_empty
- end
- end
-
- context 'when taxons format is invalid' do
- let(:row_data) do
- csv_row_hash(
- 'slug' => 'denim-shirt',
- 'category1' => 'Men -> -> Shirts',
- 'category2' => ' -> ',
- 'category3' => ' '
- )
- end
-
- it 'skips invalid taxons' do
- expect { subject.process! }.to change { Spree::Taxon.count }.by(2) # 1 root taxon (from 1 taxonomy) + 1 leaf taxon
-
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
- 'Men -> Shirts'
- )
+ it 'enqueues CreateCategoriesJob with empty list to clear taxons' do
+ expect {
+ subject.process!
+ }.to have_enqueued_job(Spree::Imports::CreateCategoriesJob)
+ .with(product.id, store.id, [])
end
end
- context 'when importing a variant row with no taxons' do
- let(:men_taxonomy) { store.taxonomies.find_by(name: 'Men') || create(:taxonomy, name: 'Men', store: store) }
- let!(:clothing_taxon) { create(:taxon, name: 'Clothing', taxonomy: men_taxonomy, parent: men_taxonomy.root) }
-
- let(:taxons) { [clothing_taxon] }
-
+ context 'when importing a variant row' do
let(:row_data) do
csv_row_hash(
'slug' => 'denim-shirt',
@@ -462,14 +400,10 @@ RSpec.describe Spree::Imports::RowProcessors::ProductVariant, type: :service do
)
end
- it 'keeps the product taxons' do
- expect(variant).to be_persisted
- expect(variant.sku).to eq 'DENIM-SHIRT-XS-BLUE'
- expect(variant.product).to eq(product)
-
- expect(product.reload.taxons.map(&:pretty_name)).to contain_exactly(
- 'Men -> Clothing'
- )
+ it 'does not enqueue CreateCategoriesJob' do
+ expect {
+ subject.process!
+ }.not_to have_enqueued_job(Spree::Imports::CreateCategoriesJob)
end
end
end