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.

Add unique indexes to option types, option values and properties (#12983)

Łukasz Adamczykffe5b7d5129a
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 5d06fa7f13..b46964d785 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -3,6 +3,7 @@ module Spree
include Spree::FilterParam
include Spree::Metadata
include Spree::ParameterizableName
+ include Spree::UniqueName
include Spree::DisplayOn
include Spree::TranslatableResource
if defined?(Spree::Webhooks::HasWebhooks)
diff --git a/core/db/migrate/20250730102644_add_missing_unique_indexes_to_spree_option_types_and_values.rb b/core/db/migrate/20250730102644_add_missing_unique_indexes_to_spree_option_types_and_values.rb
new file mode 100644
index 0000000000..bcc0af7191
--- /dev/null
+++ b/core/db/migrate/20250730102644_add_missing_unique_indexes_to_spree_option_types_and_values.rb
@@ -0,0 +1,37 @@
+class AddMissingUniqueIndexesToSpreeOptionTypesAndValues < ActiveRecord::Migration[7.2]
+ def change
+ # Rename duplicated option types
+ duplicates = Spree::OptionType.select(:name).group(:name).having('COUNT(*) > 1').reorder('').pluck(:name)
+
+ duplicates.each do |duplicate_name|
+ option_types = Spree::OptionType.where(name: duplicate_name)
+
+ option_types.each_with_index do |option_type, index|
+ next if index == 0 # Keep the first one unchanged
+
+ new_name = "#{duplicate_name}_#{index}"
+ option_type.update_columns(name: new_name, updated_at: Time.current)
+ end
+ end
+
+ # Handle duplicates in spree_option_values
+ duplicates = Spree::OptionValue.select(:option_type_id, :name).group(:option_type_id, :name).having('COUNT(*) > 1').reorder('')
+
+ duplicates.each do |dup|
+ option_values = Spree::OptionValue.where(option_type_id: dup.option_type_id, name: dup.name)
+
+ option_values.each_with_index do |option_value, index|
+ next if index == 0 # Keep the first one unchanged
+
+ new_name = "#{option_value.name}_#{index}"
+ option_value.update_columns(name: new_name, updated_at: Time.current)
+ end
+ end
+
+ # Add indexes
+ remove_index :spree_option_types, :name, if_exists: true
+ add_index :spree_option_types, :name, unique: true, if_not_exists: true
+
+ add_index :spree_option_values, %i[option_type_id name], unique: true, if_not_exists: true
+ end
+end
diff --git a/core/db/migrate/20250730154601_add_unique_index_on_spree_properties_name.rb b/core/db/migrate/20250730154601_add_unique_index_on_spree_properties_name.rb
new file mode 100644
index 0000000000..af707a6010
--- /dev/null
+++ b/core/db/migrate/20250730154601_add_unique_index_on_spree_properties_name.rb
@@ -0,0 +1,21 @@
+class AddUniqueIndexOnSpreePropertiesName < ActiveRecord::Migration[7.2]
+ def change
+ # Rename duplicated properties
+ duplicates = Spree::Property.select(:name).group(:name).having('COUNT(*) > 1').reorder('').pluck(:name)
+
+ duplicates.each do |duplicate_name|
+ properties = Spree::Property.where(name: duplicate_name)
+
+ properties.each_with_index do |property, index|
+ next if index == 0 # Keep the first one unchanged
+
+ new_name = "#{duplicate_name}_#{index}"
+ property.update_columns(name: new_name, updated_at: Time.current)
+ end
+ end
+
+ # Add indexes
+ remove_index :spree_properties, :name, if_exists: true
+ add_index :spree_properties, :name, unique: true, if_not_exists: true
+ end
+end