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.

Fixed Spree Stock Items unique index (#12012)

Damian Legawiec0e03446377fe
diff --git a/.circleci/config.yml b/.circleci/config.yml
index 48d3f62ec6..d789987d6a 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -17,7 +17,7 @@ defaults: &defaults
type: string
rails_version:
type: string
- default: '~> 7.0.0'
+ default: '~> 7.1.0'
store_artefacts:
type: boolean
default: false
@@ -163,7 +163,6 @@ workflows:
- bundle_ruby_3_3
- tests_postgres:
name: tests_ruby_3_3_rails_7_1_postgres
- rails_version: '~> 7.1.0'
ruby_image: cimg/ruby:3.3.0-browsers
postgres_image: cimg/postgres:16.2
store_artefacts: true
@@ -180,8 +179,7 @@ workflows:
requires:
- bundle_ruby_3_3
- tests_mysql:
- name: tests_ruby_3_3_rails_7_0_mysql
- rails_version: '~> 7.0.0'
+ name: tests_ruby_3_3_rails_7_1_mysql
ruby_image: cimg/ruby:3.3.0-browsers
mysql_image: cimg/mysql:8.0
store_artefacts: true
@@ -195,9 +193,8 @@ workflows:
- bundle_ruby_3_3
- send_test_coverage:
name: send_test_coverage
- rails_version: '~> 7.0.0'
ruby_image: cimg/ruby:3.3.0-browsers
run_file_path: ./bin/tests_coverage.sh
requires:
- - tests_ruby_3_3_rails_7_0_mysql
+ - tests_ruby_3_3_rails_7_1_mysql
diff --git a/api/app/models/spree/webhooks/subscriber.rb b/api/app/models/spree/webhooks/subscriber.rb
index 3179133a31..8dda632906 100644
--- a/api/app/models/spree/webhooks/subscriber.rb
+++ b/api/app/models/spree/webhooks/subscriber.rb
@@ -32,6 +32,8 @@ module Spree
["('*' MEMBER OF(subscriptions) OR ? MEMBER OF(subscriptions))", event]
when 'PostgreSQL'
["subscriptions @> '[\"*\"]' OR subscriptions @> ?", [event].to_json]
+ when 'SQLite'
+ ["subscriptions LIKE '%\"*\"%' OR subscriptions LIKE ?", "%#{event}%"]
end
)
end
diff --git a/core/app/models/spree/stock_item.rb b/core/app/models/spree/stock_item.rb
index 6da13a9bfa..00d311335f 100644
--- a/core/app/models/spree/stock_item.rb
+++ b/core/app/models/spree/stock_item.rb
@@ -14,7 +14,7 @@ module Spree
has_many :stock_movements, inverse_of: :stock_item
validates :stock_location, :variant, presence: true
- validates :variant_id, uniqueness: { scope: %i[stock_location_id deleted_at] }
+ validates :variant_id, uniqueness: { scope: :stock_location_id }, unless: :deleted_at
validates :count_on_hand, numericality: {
greater_than_or_equal_to: 0,
diff --git a/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb b/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb
index 8f8b6fb9ed..2ee632f137 100644
--- a/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb
+++ b/core/db/migrate/20210929090344_create_stock_item_stock_location_id_variant_id_coalesce_deleted_at_unique_index.rb
@@ -1,32 +1,5 @@
class CreateStockItemStockLocationIdVariantIdCoalesceDeletedAtUniqueIndex < ActiveRecord::Migration[5.2]
def change
- remove_index :spree_stock_items, name: :stock_item_by_loc_var_id_deleted_at
- reversible do |dir|
- dir.up do
- execute <<-SQL
- CREATE UNIQUE INDEX stock_item_by_loc_var_id_deleted_at
- ON spree_stock_items(
- stock_location_id,
- variant_id,
- (COALESCE(deleted_at, CAST('1970-01-01' AS #{deleted_at_data_type})))
- );
- SQL
- end
-
- dir.down do
- remove_index :spree_stock_items, name: :stock_item_by_loc_var_id_deleted_at
- end
- end
- end
-
- private
-
- def deleted_at_data_type
- case ActiveRecord::Base.connection.adapter_name
- when 'Mysql2'
- 'DATETIME'
- when 'PostgreSQL'
- 'TIMESTAMP'
- end
+ # this migration was broken and is fixed in the following migration
end
end
diff --git a/core/db/migrate/20240303174340_fix_spree_stock_item_unique_index.rb b/core/db/migrate/20240303174340_fix_spree_stock_item_unique_index.rb
new file mode 100644
index 0000000000..97364f9ab2
--- /dev/null
+++ b/core/db/migrate/20240303174340_fix_spree_stock_item_unique_index.rb
@@ -0,0 +1,35 @@
+class FixSpreeStockItemUniqueIndex < ActiveRecord::Migration[6.1]
+ def change
+ remove_index :spree_stock_items, name: 'stock_item_by_loc_var_id_deleted_at' if index_exists?(:spree_stock_items, [:stock_location_id, :variant_id], name: 'stock_item_by_loc_var_id_deleted_at')
+
+ unless index_exists?(:spree_stock_items, ['variant_id', 'stock_location_id'], name: 'index_spree_stock_items_unique_without_deleted_at')
+ # MySQL doesn't support partial indexes
+ if ActiveRecord::Base.connection.adapter_name == 'Mysql2'
+ reversible do |dir|
+ dir.up do
+ execute <<-SQL
+ CREATE UNIQUE INDEX index_spree_stock_items_unique_without_deleted_at
+ ON spree_stock_items(
+ stock_location_id,
+ variant_id,
+ (COALESCE(deleted_at, CAST('1970-01-01' AS DATETIME)))
+ );
+ SQL
+ end
+
+ dir.down do
+ remove_index :spree_stock_items, name: :index_spree_stock_items_unique_without_deleted_at
+ end
+ end
+ else
+ add_index(
+ :spree_stock_items,
+ ['variant_id', 'stock_location_id'],
+ name: 'index_spree_stock_items_unique_without_deleted_at',
+ unique: true,
+ where: 'deleted_at IS NULL',
+ )
+ end
+ end
+ end
+end
diff --git a/sample/spec/lib/load_sample_spec.rb b/sample/spec/lib/load_sample_spec.rb
index 110aab0025..67749dcf93 100644
--- a/sample/spec/lib/load_sample_spec.rb
+++ b/sample/spec/lib/load_sample_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe 'Load samples' do
- xit 'doesnt raise any error' do
+ it 'doesnt raise any error' do
expect do
Spree::Seeds::All.call
SpreeSample::Engine.load_samples