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(core): isolate per-category failures in the collection migration task

Łukasz Adamczyk2d84c662125c
diff --git a/spree/core/lib/tasks/categories_and_collections.rake b/spree/core/lib/tasks/categories_and_collections.rake
index 9c665a5cb2..2fcf86bef9 100644
--- a/spree/core/lib/tasks/categories_and_collections.rake
+++ b/spree/core/lib/tasks/categories_and_collections.rake
@@ -23,6 +23,7 @@ namespace :spree do
puts "Migrating #{automatic_ids.size} automatic categories to collections..."
migrated = 0
skipped = 0
+ failed = 0
Spree::Category.unscoped.where(id: automatic_ids).find_each do |category|
store_id = category.store_id || category.store&.id
@@ -40,85 +41,93 @@ namespace :spree do
next
end
- ApplicationRecord.transaction do
- collection = Spree::Collection.new(
- name: category[:name],
- permalink: category[:permalink],
- automatic: true,
- rules_match_policy: category.rules_match_policy,
- sort_order: category.sort_order,
- position: category.position,
- store_id: store_id,
- meta_title: category[:meta_title],
- meta_description: category[:meta_description],
- meta_keywords: category[:meta_keywords],
- metadata: category.metadata
- )
- # Membership is copied verbatim below, so don't rebuild it from the rules.
- collection.marked_for_regenerate_products = false
- collection.save!(validate: false)
-
- # Table-backend translations (collection_translations has no pretty_name;
- # description is ActionText, moved separately below).
- Spree::Category::Translation.where(spree_category_id: category.id).find_each do |translation|
- Spree::Collection::Translation.create!(
- spree_collection_id: collection.id,
- locale: translation.locale,
- name: translation.name,
- permalink: translation.permalink,
- meta_title: translation.meta_title,
- meta_description: translation.meta_description,
- meta_keywords: translation.meta_keywords
+ begin
+ ApplicationRecord.transaction do
+ collection = Spree::Collection.new(
+ name: category[:name],
+ permalink: category[:permalink],
+ automatic: true,
+ rules_match_policy: category.rules_match_policy,
+ sort_order: category.sort_order,
+ position: category.position,
+ store_id: store_id,
+ meta_title: category[:meta_title],
+ meta_description: category[:meta_description],
+ meta_keywords: category[:meta_keywords],
+ metadata: category.metadata
)
+ # Membership is copied verbatim below, so don't rebuild it from the rules.
+ collection.marked_for_regenerate_products = false
+ collection.save!(validate: false)
+
+ # Table-backend translations (collection_translations has no pretty_name;
+ # description is ActionText, moved separately below).
+ Spree::Category::Translation.where(spree_category_id: category.id).find_each do |translation|
+ Spree::Collection::Translation.create!(
+ spree_collection_id: collection.id,
+ locale: translation.locale,
+ name: translation.name,
+ permalink: translation.permalink,
+ meta_title: translation.meta_title,
+ meta_description: translation.meta_description,
+ meta_keywords: translation.meta_keywords
+ )
+ end
+
+ # Move (not copy) the polymorphic records that `category.destroy!` would
+ # otherwise purge/destroy: ActionText description, images, custom fields, slugs.
+ move_polymorphic.call(ActionText::RichText, :record_type, :record_id, category, collection)
+ move_polymorphic.call(ActiveStorage::Attachment, :record_type, :record_id, category, collection, name: %w[image square_image])
+ move_polymorphic.call(Spree::Metafield, :resource_type, :resource_id, category, collection)
+ move_polymorphic.call(FriendlyId::Slug, :sluggable_type, :sluggable_id, category, collection)
+
+ # Rules -> collection_rules (remap the STI type; insert_all bypasses the
+ # regenerate-on-create callback since membership is copied directly).
+ rule_rows = Spree::TaxonRule.where(taxon_id: category.id).map do |rule|
+ {
+ collection_id: collection.id,
+ type: rule.type.sub('Spree::TaxonRules::', 'Spree::CollectionRules::'),
+ value: rule.value,
+ match_policy: rule.match_policy,
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ Spree::CollectionRule.insert_all(rule_rows) if rule_rows.any?
+
+ # Materialized membership: product_categories -> product_collections.
+ membership_rows = Spree::ProductCategory.where(category_id: category.id).map do |product_category|
+ {
+ collection_id: collection.id,
+ product_id: product_category.product_id,
+ position: product_category.position,
+ created_at: Time.current,
+ updated_at: Time.current
+ }
+ end
+ product_ids = membership_rows.map { |row| row[:product_id] }.uniq
+ Spree::ProductCollection.insert_all(membership_rows) if membership_rows.any?
+
+ # insert_all bypasses the counter caches — recompute them.
+ Spree::Collection.reset_counters(collection.id, :product_collections)
+ product_ids.each { |id| Spree::Product.reset_counters(id, :product_collections) }
+
+ # Drop the automatic category (its product_categories + taxon_rules cascade).
+ category.destroy!
end
-
- # Move (not copy) the polymorphic records that `category.destroy!` would
- # otherwise purge/destroy: ActionText description, images, custom fields, slugs.
- move_polymorphic.call(ActionText::RichText, :record_type, :record_id, category, collection)
- move_polymorphic.call(ActiveStorage::Attachment, :record_type, :record_id, category, collection, name: %w[image square_image])
- move_polymorphic.call(Spree::Metafield, :resource_type, :resource_id, category, collection)
- move_polymorphic.call(FriendlyId::Slug, :sluggable_type, :sluggable_id, category, collection)
-
- # Rules -> collection_rules (remap the STI type; insert_all bypasses the
- # regenerate-on-create callback since membership is copied directly).
- rule_rows = Spree::TaxonRule.where(taxon_id: category.id).map do |rule|
- {
- collection_id: collection.id,
- type: rule.type.sub('Spree::TaxonRules::', 'Spree::CollectionRules::'),
- value: rule.value,
- match_policy: rule.match_policy,
- created_at: Time.current,
- updated_at: Time.current
- }
- end
- Spree::CollectionRule.insert_all(rule_rows) if rule_rows.any?
-
- # Materialized membership: product_categories -> product_collections.
- membership_rows = Spree::ProductCategory.where(category_id: category.id).map do |product_category|
- {
- collection_id: collection.id,
- product_id: product_category.product_id,
- position: product_category.position,
- created_at: Time.current,
- updated_at: Time.current
- }
- end
- product_ids = membership_rows.map { |row| row[:product_id] }.uniq
- Spree::ProductCollection.insert_all(membership_rows) if membership_rows.any?
-
- # insert_all bypasses the counter caches — recompute them.
- Spree::Collection.reset_counters(collection.id, :product_collections)
- product_ids.each { |id| Spree::Product.reset_counters(id, :product_collections) }
-
- # Drop the automatic category (its product_categories + taxon_rules cascade).
- category.destroy!
+ rescue => e
+ # Isolate failures (e.g. a permalink collision) to the offending category —
+ # its transaction rolls back and the run continues. Re-run to retry (idempotent).
+ warn " FAILED category ##{category.id} (#{category[:name].inspect}): #{e.class}: #{e.message}"
+ failed += 1
+ next
end
migrated += 1
print '.'
end
- puts "\n migrated #{migrated}, skipped #{skipped}. (Manual categories stay as categories.)"
+ puts "\n migrated #{migrated}, skipped #{skipped}, failed #{failed}. (Manual categories stay as categories.)"
# Sever the taxonomy structure from the surviving (manual) categories — in 6.0
# categories are store-owned; Taxonomy + the taxonomy_id column drop in 6.1.