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.

Move StockLocation#create_stock_items to a background queue (#11291)

seb40f78936fd61
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index ca7c3566be..ec98f89e76 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -74,6 +74,15 @@ RSpec.configure do |config|
create(:store, default: true)
end
+ # Force jobs to be executed in a synchronous way (see http://archive.today/xcb1E)
+ config.around do |example|
+ (ActiveJob::Base.descendants << ActiveJob::Base).each(&:disable_test_adapter)
+ ActiveJob::Base.queue_adapter = :inline
+ example.run
+ (ActiveJob::Base.descendants << ActiveJob::Base).each { |a| a.enable_test_adapter(ActiveJob::QueueAdapters::TestAdapter.new) }
+ ActiveJob::Base.queue_adapter = :test
+ end
+
config.order = :random
Kernel.srand config.seed
end
diff --git a/backend/spec/spec_helper.rb b/backend/spec/spec_helper.rb
index b7dfc0f669..125040b3f3 100644
--- a/backend/spec/spec_helper.rb
+++ b/backend/spec/spec_helper.rb
@@ -67,6 +67,8 @@ RSpec.configure do |config|
config.before :suite do
Capybara.match = :smart
DatabaseCleaner.clean_with :truncation
+ # Force jobs to be executed in a synchronous way
+ ActiveJob::Base.queue_adapter = :inline
end
config.before do
diff --git a/core/app/jobs/spree/base_job.rb b/core/app/jobs/spree/base_job.rb
new file mode 100644
index 0000000000..4e59602156
--- /dev/null
+++ b/core/app/jobs/spree/base_job.rb
@@ -0,0 +1,4 @@
+module Spree
+ class BaseJob < ApplicationJob
+ end
+end
diff --git a/core/app/jobs/spree/stock_locations/stock_items/create_job.rb b/core/app/jobs/spree/stock_locations/stock_items/create_job.rb
new file mode 100644
index 0000000000..28781b4147
--- /dev/null
+++ b/core/app/jobs/spree/stock_locations/stock_items/create_job.rb
@@ -0,0 +1,13 @@
+module Spree
+ module StockLocations
+ module StockItems
+ class CreateJob < Spree::BaseJob
+ queue_as :spree_stock_location_stock_items
+
+ def perform(stock_location)
+ Spree::StockLocations::StockItems::Create.call(stock_location: stock_location)
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/models/spree/stock_location.rb b/core/app/models/spree/stock_location.rb
index c815e5fbf1..21b62fcd62 100644
--- a/core/app/models/spree/stock_location.rb
+++ b/core/app/models/spree/stock_location.rb
@@ -108,27 +108,7 @@ module Spree
private
def create_stock_items
- variants_scope = Spree::Variant
-
- if self.class.method_defined?(:insert_all) && self.class.method_defined?(:touch_all)
- prepared_stock_items = variants_scope.ids.map do |variant_id|
- Hash[
- 'stock_location_id', id,
- 'variant_id', variant_id,
- 'backorderable', backorderable_default,
- 'created_at', Time.current,
- 'updated_at', Time.current
- ]
- end
- if prepared_stock_items.any?
- stock_items.insert_all(prepared_stock_items)
- variants_scope.touch_all
- end
- else
- variants_scope.find_each do |variant|
- propagate_variant(variant)
- end
- end
+ Spree::StockLocations::StockItems::CreateJob.perform_later(self)
end
def ensure_one_default
diff --git a/core/app/services/spree/stock_locations/stock_items/create.rb b/core/app/services/spree/stock_locations/stock_items/create.rb
new file mode 100644
index 0000000000..a3c9dc6945
--- /dev/null
+++ b/core/app/services/spree/stock_locations/stock_items/create.rb
@@ -0,0 +1,31 @@
+module Spree
+ module StockLocations
+ module StockItems
+ class Create
+ prepend Spree::ServiceModule::Base
+
+ def call(stock_location:, variants_scope: Spree::Variant)
+ if Rails::VERSION::MAJOR >= 6
+ prepared_stock_items = variants_scope.ids.map do |variant_id|
+ Hash[
+ 'stock_location_id', stock_location.id,
+ 'variant_id', variant_id,
+ 'backorderable', stock_location.backorderable_default,
+ 'created_at', Time.current,
+ 'updated_at', Time.current
+ ]
+ end.uniq { |stock_item| stock_item.values_at('stock_location_id', 'variant_id', 'backorderable') }
+ if prepared_stock_items.any?
+ stock_location.stock_items.insert_all(prepared_stock_items)
+ variants_scope.touch_all
+ end
+ else
+ variants_scope.find_each do |variant|
+ stock_location.propagate_variant(variant)
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/core/db/migrate/20210921090344_add_unique_stock_item_stock_location_variant_deleted_at_index.rb b/core/db/migrate/20210921090344_add_unique_stock_item_stock_location_variant_deleted_at_index.rb
new file mode 100644
index 0000000000..39ce808306
--- /dev/null
+++ b/core/db/migrate/20210921090344_add_unique_stock_item_stock_location_variant_deleted_at_index.rb
@@ -0,0 +1,5 @@
+class AddUniqueStockItemStockLocationVariantDeletedAtIndex < ActiveRecord::Migration[5.2]
+ def change
+ add_index :spree_stock_items, [:stock_location_id, :variant_id, :deleted_at], name: 'stock_item_by_loc_var_id_deleted_at', unique: true
+ end
+end
diff --git a/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb b/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb
new file mode 100644
index 0000000000..a19890e8ac
--- /dev/null
+++ b/core/spec/jobs/spree/stock_locations/stock_items/create_job_spec.rb
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+module Spree
+ describe StockLocations::StockItems::CreateJob, :inline_jobs do
+ let!(:stock_location) { create :stock_location }
+
+ it 'enqueues the creation of the stock location stock items' do
+ expect { described_class.perform_later(stock_location) }.to(
+ have_enqueued_job.on_queue('spree_stock_location_stock_items')
+ )
+ end
+ end
+end
diff --git a/core/spec/models/spree/stock_location_spec.rb b/core/spec/models/spree/stock_location_spec.rb
index d258c8eebf..b167a384be 100644
--- a/core/spec/models/spree/stock_location_spec.rb
+++ b/core/spec/models/spree/stock_location_spec.rb
@@ -7,8 +7,22 @@ module Spree
let(:stock_item) { subject.stock_items.order(:id).first }
let(:variant) { stock_item.variant }
- it 'creates stock_items for all variants' do
- expect(subject.stock_items.count).to eq Variant.count
+ context 'handling the stock items creation after create' do
+ let!(:variant) { create(:variant) }
+
+ before { StockLocation.destroy_all }
+
+ it 'creates stock_items for all variants' do
+ expect do
+ create(:stock_location)
+ end.to(
+ change { Variant.count }.by(0).and(
+ change { StockLocation.count }.from(0).to(1).and(
+ change { StockItem.count }.from(0).to(2)
+ )
+ )
+ )
+ end
end
it 'validates uniqueness' do
@@ -68,7 +82,10 @@ module Spree
before { subject.propagate_all_variants = true }
specify do
- expect(subject).to receive(:create_stock_items)
+ expect(subject).to receive(:create_stock_items).and_call_original
+ expect(Spree::StockLocations::StockItems::CreateJob).to(
+ receive(:perform_later).once.with(subject)
+ )
subject.save!
end
end
@@ -77,7 +94,7 @@ module Spree
before { subject.propagate_all_variants = false }
specify do
- expect(subject).not_to receive(:propagate_variant)
+ expect(subject).not_to receive(:create_stock_items)
subject.save!
end
end
diff --git a/core/spec/services/spree/stock_locations/stock_items/create_spec.rb b/core/spec/services/spree/stock_locations/stock_items/create_spec.rb
new file mode 100644
index 0000000000..dc61c220ea
--- /dev/null
+++ b/core/spec/services/spree/stock_locations/stock_items/create_spec.rb
@@ -0,0 +1,99 @@
+require 'spec_helper'
+
+module Spree
+ describe StockLocations::StockItems::Create do
+ subject { described_class }
+
+ let!(:stock_location) { create(:stock_location_with_items) }
+ let!(:unrelated_variant) { create(:variant) }
+ let(:result) { subject.call(stock_location: stock_location) }
+ let(:stock_location_class) { stock_location.class }
+
+ describe '#call' do
+ context 'Rails < 6', if: Rails::VERSION::MAJOR < 6 do
+ context 'with variants to propagate' do
+ before { stock_location.stock_items.delete_all }
+
+ it 'propagates the variants' do
+ expect { result }.to change { stock_location.stock_items.count }.from(0).to(4)
+ end
+ end
+
+ context 'without variants to propagate' do
+ before { Variant.destroy_all }
+
+ it 'does not propagate stock location variants' do
+ expect(stock_location).not_to receive(:propagate_variant)
+ result
+ end
+ end
+ end
+
+ context 'Rails >= 6', if: Rails::VERSION::MAJOR >= 6 do
+ before do
+ # Delete all stock_location.stock_items to start counting from 0.
+ stock_location.stock_items.unscope(:where).delete_all
+ end
+
+ let(:time_current) { Time.local(1990) }
+
+ context 'with prepared stock items' do
+ context 'with duplicate stock items by stock_location_id and variant' do
+ it 'inserts the stock location stock items without duplicates' do
+ # Make a call of Spree::Variant to obtain the ids, return duplicated records.
+ expect(Spree::Variant).to receive(:ids).and_return([1, 1])
+ expect { result }.to change { stock_location.stock_items.count }.from(0).to(1)
+ end
+ end
+
+ context 'without duplicate stock items to insert' do
+ let(:created_stock_item) { stock_location.stock_items.order(:id).last }
+ let(:created_stock_item_attrs) do
+ created_stock_item.attributes.values_at(
+ 'stock_location_id', 'variant_id', 'backorderable', 'created_at', 'updated_at'
+ )
+ end
+
+ it 'inserts the stock location stock items' do
+ expect { result }.to change { stock_location.stock_items.count }.from(0).to(4)
+ end
+
+ it 'sets the stock location data necessary for the inserted stock items' do
+ Timecop.freeze(time_current)
+ result
+ expect(created_stock_item_attrs).to(
+ eq([
+ stock_location.id,
+ unrelated_variant.id,
+ stock_location.backorderable_default,
+ time_current,
+ time_current
+ ])
+ )
+ Timecop.return
+ end
+
+ it 'invalidates the Variant cache' do
+ expect(Spree::Variant).to receive(:touch_all).once
+ result
+ end
+ end
+ end
+
+ context 'without prepared stock items' do
+ before { Spree::Variant.destroy_all }
+
+ it 'does not insert stock items' do
+ expect(stock_location.stock_items).not_to receive(:insert_all)
+ result
+ end
+
+ it 'does not invalidates the Variant cache' do
+ expect(Spree::Variant).not_to receive(:touch_all)
+ result
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/core/spec/spec_helper.rb b/core/spec/spec_helper.rb
index 6e7c863c4f..0623d3f17e 100644
--- a/core/spec/spec_helper.rb
+++ b/core/spec/spec_helper.rb
@@ -71,9 +71,19 @@ RSpec.configure do |config|
config.include Spree::TestingSupport::Preferences
config.include Spree::TestingSupport::Kernel
- # Clean out the database state before the tests run
config.before(:suite) do
+ # Clean out the database state before the tests run
DatabaseCleaner.clean_with(:truncation)
+ # Force jobs to be executed in a synchronous way
+ ActiveJob::Base.queue_adapter = :inline
+ end
+
+ config.before(:each, :inline_jobs) do
+ ActiveJob::Base.queue_adapter = :test
+ end
+
+ config.after(:each, :inline_jobs) do
+ ActiveJob::Base.queue_adapter = :inline
end
config.order = :random