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.

Fix DB migration for MySQl - remove partial indices (#12485)

Łukasz Adamczykf321e8a601b0
diff --git a/core/app/models/spree/coupon_code.rb b/core/app/models/spree/coupon_code.rb
index 25e70ceb69..b861bba964 100644
--- a/core/app/models/spree/coupon_code.rb
+++ b/core/app/models/spree/coupon_code.rb
@@ -14,7 +14,7 @@ module Spree
belongs_to :promotion, class_name: 'Spree::Promotion', touch: true
belongs_to :order, class_name: 'Spree::Order'
- validates :code, presence: true, uniqueness: { scope: spree_base_uniqueness_scope }
+ validates :code, presence: true, uniqueness: { scope: spree_base_uniqueness_scope, conditions: -> { where(deleted_at: nil) } }
validates :state, :promotion, presence: true
self.whitelisted_ransackable_attributes = %w[state code]
diff --git a/core/app/models/spree/page.rb b/core/app/models/spree/page.rb
index 80b54fbf9d..001582b7d5 100644
--- a/core/app/models/spree/page.rb
+++ b/core/app/models/spree/page.rb
@@ -22,6 +22,7 @@ module Spree
#
validates :name, presence: true
validates :pageable, presence: true
+ validates :slug, uniqueness: { scope: :pageable, conditions: -> { where(deleted_at: nil).where.not(slug: nil) } }
#
# Callbacks
diff --git a/core/app/models/spree/post.rb b/core/app/models/spree/post.rb
index 699f90fe5f..0e1723985c 100644
--- a/core/app/models/spree/post.rb
+++ b/core/app/models/spree/post.rb
@@ -45,7 +45,7 @@ module Spree
# Validations
#
validates :title, :store, :author, presence: true
- validates :slug, presence: true, uniqueness: { scope: :store_id }
+ validates :slug, presence: true, uniqueness: { scope: :store_id, conditions: -> { where(deleted_at: nil) } }
validates :meta_title, length: { maximum: 160 }, allow_blank: true
validates :meta_description, length: { maximum: 320 }, allow_blank: true
validates :image, content_type: Rails.application.config.active_storage.web_image_content_types
diff --git a/core/db/migrate/20241004181911_create_spree_coupon_codes.rb b/core/db/migrate/20241004181911_create_spree_coupon_codes.rb
index 39b21d8635..229df81a16 100644
--- a/core/db/migrate/20241004181911_create_spree_coupon_codes.rb
+++ b/core/db/migrate/20241004181911_create_spree_coupon_codes.rb
@@ -3,7 +3,7 @@ class CreateSpreeCouponCodes < ActiveRecord::Migration[6.1]
return if table_exists?(:spree_coupon_codes)
create_table :spree_coupon_codes do |t|
- t.string :code, index: { unique: true, where: 'deleted_at IS NULL' }
+ t.string :code, index: { unique: true }
t.references :promotion, index: true
t.references :order, index: true
t.integer :state, default: 0, null: false, index: true
diff --git a/core/db/migrate/20250120094216_create_page_builder_models.rb b/core/db/migrate/20250120094216_create_page_builder_models.rb
index 8ab0c2b06f..3f0068de8d 100644
--- a/core/db/migrate/20250120094216_create_page_builder_models.rb
+++ b/core/db/migrate/20250120094216_create_page_builder_models.rb
@@ -14,7 +14,6 @@ class CreatePageBuilderModels < ActiveRecord::Migration[6.1]
t.datetime :deleted_at
t.index ['deleted_at'], name: 'index_spree_themes_on_deleted_at'
- t.index ['store_id', 'default'], name: 'index_spree_themes_on_store_id_and_default', unique: true, where: "((deleted_at IS NULL) AND (\"default\" = true))"
end
create_table :spree_pages do |t|
@@ -32,7 +31,6 @@ class CreatePageBuilderModels < ActiveRecord::Migration[6.1]
t.datetime :deleted_at
t.index ['pageable_id', 'name'], name: 'index_spree_pages_on_pageable_id_and_name'
- t.index ['pageable_id', 'pageable_type', 'slug'], name: 'index_spree_pages_on_pageable_id_and_pageable_type_and_slug', unique: true, where: '((deleted_at IS NULL) AND (slug IS NOT NULL))'
t.index ['pageable_id', 'pageable_type', 'type'], name: 'index_spree_pages_on_pageable_id_and_pageable_type_and_type'
t.index ['pageable_id', 'pageable_type'], name: 'index_spree_pages_on_pageable_id_and_pageable_type'
end
diff --git a/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb b/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb
index 85b0135894..a6d7a67c6f 100644
--- a/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb
+++ b/core/db/migrate/20250121160028_create_spree_posts_and_spree_post_categories.rb
@@ -26,7 +26,6 @@ class CreateSpreePostsAndSpreePostCategories < ActiveRecord::Migration[6.1]
t.timestamps
t.datetime :deleted_at
- t.index ['slug', 'store_id'], name: 'index_spree_posts_on_slug_and_store_id', unique: true, where: '(deleted_at IS NULL)'
t.index ['title'], name: 'index_spree_posts_on_title'
end
end
diff --git a/core/db/migrate/20250305121352_remove_page_builder_indices.rb b/core/db/migrate/20250305121352_remove_page_builder_indices.rb
new file mode 100644
index 0000000000..d28b8808f2
--- /dev/null
+++ b/core/db/migrate/20250305121352_remove_page_builder_indices.rb
@@ -0,0 +1,11 @@
+class RemovePageBuilderIndices < ActiveRecord::Migration[7.2]
+ def change
+ if index_name_exists?(:spree_pages, 'index_spree_pages_on_pageable_id_and_pageable_type_and_slug')
+ remove_index :spree_pages, name: 'index_spree_pages_on_pageable_id_and_pageable_type_and_slug'
+ end
+
+ if index_name_exists?(:spree_themes, 'index_spree_themes_on_store_id_and_default')
+ remove_index :spree_themes, name: 'index_spree_themes_on_store_id_and_default'
+ end
+ end
+end
diff --git a/core/db/migrate/20250305121657_remove_spree_posts_indices.rb b/core/db/migrate/20250305121657_remove_spree_posts_indices.rb
new file mode 100644
index 0000000000..d40db1c3d6
--- /dev/null
+++ b/core/db/migrate/20250305121657_remove_spree_posts_indices.rb
@@ -0,0 +1,7 @@
+class RemoveSpreePostsIndices < ActiveRecord::Migration[7.2]
+ def change
+ if index_name_exists?(:spree_posts, 'index_spree_posts_on_slug_and_store_id')
+ remove_index :spree_posts, name: 'index_spree_posts_on_slug_and_store_id'
+ end
+ end
+end
diff --git a/core/spec/models/spree/coupon_code_spec.rb b/core/spec/models/spree/coupon_code_spec.rb
new file mode 100644
index 0000000000..da254e7e75
--- /dev/null
+++ b/core/spec/models/spree/coupon_code_spec.rb
@@ -0,0 +1,26 @@
+require 'spec_helper'
+
+RSpec.describe Spree::CouponCode, type: :model do
+ let!(:promotion) { create(:promotion, multi_codes: true, number_of_codes: 1) }
+ let(:coupon_code) { create(:coupon_code, promotion: promotion) }
+
+ describe 'validations' do
+ describe 'code' do
+ it 'validates presence' do
+ coupon_code.code = nil
+ expect(coupon_code).not_to be_valid
+ end
+
+ it 'validates uniqueness' do
+ other_coupon_code = promotion.coupon_codes.last
+
+ coupon_code.code = other_coupon_code.code
+ expect(coupon_code).not_to be_valid
+ expect(coupon_code.errors.full_messages).to include('Code has already been taken')
+
+ other_coupon_code.destroy
+ expect(coupon_code).to be_valid
+ end
+ end
+ end
+end
diff --git a/core/spec/models/spree/page_spec.rb b/core/spec/models/spree/page_spec.rb
index ef5b250a35..f32645ae8a 100644
--- a/core/spec/models/spree/page_spec.rb
+++ b/core/spec/models/spree/page_spec.rb
@@ -5,6 +5,20 @@ describe Spree::Page, type: :model do
let(:theme) { store.default_theme }
let(:page) { theme.pages.first }
+ describe 'validations' do
+ describe 'slug' do
+ it 'validates uniqueness' do
+ other_page = create(:page, slug: 'test-slug')
+
+ page.slug = other_page.slug
+ expect(page).not_to be_valid
+ expect(page.errors.full_messages).to include('Slug has already been taken')
+
+ other_page.destroy
+ end
+ end
+ end
+
context 'slugs' do
let(:custom_page) { create(:page, pageable: theme, type: 'Spree::Pages::Custom', name: 'Landing Page') }
diff --git a/core/spec/models/spree/post_spec.rb b/core/spec/models/spree/post_spec.rb
index a63675da68..f630910d9e 100644
--- a/core/spec/models/spree/post_spec.rb
+++ b/core/spec/models/spree/post_spec.rb
@@ -19,5 +19,18 @@ RSpec.describe Spree::Post, type: :model do
expect(post).not_to be_valid
end
end
+
+ describe 'slug' do
+ it 'validates uniqueness' do
+ other_post = create(:post, slug: 'test-slug')
+
+ post.slug = other_post.slug
+ expect(post).not_to be_valid
+ expect(post.errors.full_messages).to include('Slug has already been taken')
+
+ other_post.destroy
+ expect(post).to be_valid
+ end
+ end
end
end