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.

feat(core): add taxons→categories/collections data migration task

Łukasz Adamczyk1a7e79963558
diff --git a/spree/core/lib/tasks/categories_and_collections.rake b/spree/core/lib/tasks/categories_and_collections.rake
new file mode 100644
index 0000000000..36031b6ee6
--- /dev/null
+++ b/spree/core/lib/tasks/categories_and_collections.rake
@@ -0,0 +1,164 @@
+namespace :spree do
+ # Migrates rule-based (automatic) categories to Spree::Collection and rewrites the
+ # class-name strings that Phase 3 renamed. Idempotent (each automatic category is
+ # migrated + deleted in its own transaction, so a re-run skips what's already gone;
+ # the string backfills only touch rows still holding the old names). Run AFTER
+ # spree:taxons:backfill_store_id so every category has a store.
+ desc 'Migrate automatic taxons to Collections and backfill renamed class-name strings (6.0)'
+ task migrate_taxons_to_categories_and_collections: :environment do
+ # Polymorphic/STI rows on a category can hold either the pre-6.0 name or the
+ # post-Phase-3 name, depending on whether they were touched between deploy and
+ # this run — match both when moving a category's rows onto its new collection.
+ category_type_strings = ['Spree::Taxon', 'Spree::Category'].freeze
+
+ move_polymorphic = lambda do |klass, type_col, id_col, category, collection, extra = {}|
+ klass.where(extra.merge(type_col => category_type_strings, id_col => category.id)).
+ update_all(type_col => 'Spree::Collection', id_col => collection.id)
+ end
+
+ automatic_ids = Spree::Category.unscoped.where(automatic: true).ids
+ automatic_ids |= Spree::TaxonRule.distinct.pluck(:taxon_id) if defined?(Spree::TaxonRule)
+ automatic_ids.compact!
+
+ puts "Migrating #{automatic_ids.size} automatic categories to collections..."
+ migrated = 0
+ skipped = 0
+
+ Spree::Category.unscoped.where(id: automatic_ids).find_each do |category|
+ store_id = category.store_id || category.store&.id
+
+ if store_id.nil?
+ warn " skip category ##{category.id} (#{category[:name].inspect}): no store"
+ skipped += 1
+ next
+ end
+
+ # Automatic categories are rule-based leaves; refuse to destroy a subtree.
+ if category.children.exists?
+ warn " skip category ##{category.id} (#{category[:name].inspect}): has child categories"
+ skipped += 1
+ 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,
+ hide_from_nav: category.hide_from_nav,
+ position: category.position,
+ store_id: store_id,
+ meta_title: category[:meta_title],
+ meta_description: category[:meta_description],
+ meta_keywords: category[:meta_keywords],
+ public_metadata: category.public_metadata,
+ private_metadata: category.private_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
+
+ migrated += 1
+ print '.'
+ end
+
+ puts "\n migrated #{migrated}, skipped #{skipped}. (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.
+ puts 'Severing taxonomy links from surviving categories...'
+
+ # Backfill store_id from the taxonomy first so nulling taxonomy_id never orphans a
+ # category (belt-and-suspenders alongside spree:taxons:backfill_store_id).
+ Spree::Taxonomy.find_each do |taxonomy|
+ Spree::Category.unscoped.where(store_id: nil, taxonomy_id: taxonomy.id).
+ update_all(store_id: taxonomy.store_id)
+ end
+
+ # Drop legacy taxonomy roots (parentless, taxonomy-backed containers) left with no
+ # children after the automatic migration; non-empty roots stay as top-level categories.
+ parent_ids = Spree::Category.unscoped.where.not(parent_id: nil).distinct.pluck(:parent_id)
+ childless_root_ids = Spree::Category.unscoped.where.not(taxonomy_id: nil).
+ where(parent_id: nil).where.not(id: parent_ids).ids
+ Spree::Category.unscoped.where(id: childless_root_ids).find_each(&:destroy!)
+ puts " dropped #{childless_root_ids.size} childless taxonomy roots."
+
+ # Null the taxonomy link on everything that remains — categories are store-owned now.
+ severed = Spree::Category.unscoped.where.not(taxonomy_id: nil).update_all(taxonomy_id: nil)
+ puts " severed taxonomy_id on #{severed} categories."
+
+ # Backfill the class-name strings Phase 3 renamed, for the surviving (manual)
+ # category rows that still hold the pre-6.0 names.
+ puts 'Backfilling renamed class-name strings...'
+ [
+ [Spree::Metafield, :resource_type, 'Spree::Taxon', 'Spree::Category'],
+ [Spree::MetafieldDefinition, :resource_type, 'Spree::Taxon', 'Spree::Category'],
+ [ActiveStorage::Attachment, :record_type, 'Spree::Taxon', 'Spree::Category'],
+ [FriendlyId::Slug, :sluggable_type, 'Spree::Taxon', 'Spree::Category'],
+ [Spree::PromotionRule, :type, 'Spree::Promotion::Rules::Taxon', 'Spree::Promotion::Rules::Category']
+ ].each do |model, column, from, to|
+ count = model.where(column => from).update_all(column => to)
+ puts " #{model.table_name}.#{column}: #{count} rows #{from} -> #{to}"
+ end
+
+ puts 'Done!'
+ end
+end
diff --git a/spree/core/spec/lib/tasks/categories_and_collections_spec.rb b/spree/core/spec/lib/tasks/categories_and_collections_spec.rb
new file mode 100644
index 0000000000..1fcce8f9ba
--- /dev/null
+++ b/spree/core/spec/lib/tasks/categories_and_collections_spec.rb
@@ -0,0 +1,110 @@
+require 'spec_helper'
+require 'rake'
+
+describe 'spree:migrate_taxons_to_categories_and_collections' do
+ subject { Rake::Task[task_name] }
+
+ let(:task_name) { 'spree:migrate_taxons_to_categories_and_collections' }
+
+ before(:all) do
+ Rake::Task.define_task(:environment)
+ load Spree::Core::Engine.root.join('lib', 'tasks', 'categories_and_collections.rake')
+ end
+
+ before { subject.reenable }
+
+ let!(:store) { Spree::Store.default || create(:store, default: true) }
+
+ describe 'automatic categories -> collections' do
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:category) do
+ create(:automatic_taxon, taxonomy: taxonomy, store: store, name: 'On Sale',
+ sort_order: 'price asc', rules_match_policy: 'all')
+ end
+ let!(:rule) { create(:tag_taxon_rule, taxon: category, value: 'sale', match_policy: 'is_equal_to') }
+ let!(:product) { create(:product, stores: [store]) }
+ let!(:membership) { Spree::ProductCategory.create!(category: category, product: product, position: 1) }
+
+ it 'creates a Collection mirroring the category and deletes the category' do
+ permalink = category[:permalink]
+
+ subject.invoke
+
+ collection = Spree::Collection.find_by(store: store, permalink: permalink)
+ expect(collection).to be_present
+ expect(collection).to be_automatic
+ expect(collection.name).to eq('On Sale')
+ expect(collection.rules_match_policy).to eq('all')
+ expect(collection.sort_order).to eq('price asc')
+
+ expect(collection.rules.map(&:type)).to eq(['Spree::CollectionRules::Tag'])
+ expect(collection.rules.first).to have_attributes(value: 'sale', match_policy: 'is_equal_to')
+
+ expect(collection.products).to contain_exactly(product)
+ expect(collection.reload.products_count).to eq(1)
+ expect(product.reload.collections_count).to eq(1)
+
+ expect(Spree::Category.unscoped.exists?(category.id)).to be(false)
+ end
+ end
+
+ describe 'manual categories' do
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:category) { create(:taxon, taxonomy: taxonomy, store: store) }
+
+ it 'keeps them as store-owned categories, severs taxonomy_id, creates no collection' do
+ root = taxonomy.root
+
+ subject.invoke
+
+ expect(Spree::Category.unscoped.exists?(category.id)).to be(true)
+ expect(category.reload.taxonomy_id).to be_nil
+ # the root has a child, so it stays — as a top-level store-owned category
+ expect(Spree::Category.unscoped.exists?(root.id)).to be(true)
+ expect(root.reload.taxonomy_id).to be_nil
+ expect(Spree::Collection.where(store: store)).to be_empty
+ end
+ end
+
+ describe 'taxonomy roots left childless' do
+ let!(:taxonomy) { create(:taxonomy, store: store) }
+
+ it 'drops a taxonomy root with no children' do
+ root = taxonomy.root
+
+ subject.invoke
+
+ expect(Spree::Category.unscoped.exists?(root.id)).to be(false)
+ end
+ end
+
+ describe 'backfilling renamed class-name strings' do
+ let!(:metafield) do
+ create(:metafield).tap { |m| m.update_column(:resource_type, 'Spree::Taxon') }
+ end
+ let!(:promotion_rule) do
+ create(:promotion_rule_taxon).tap { |r| r.update_column(:type, 'Spree::Promotion::Rules::Taxon') }
+ end
+
+ it 'rewrites Spree::Taxon* strings to Spree::Category*' do
+ subject.invoke
+
+ expect(metafield.reload.resource_type).to eq('Spree::Category')
+ expect(Spree::PromotionRule.where(id: promotion_rule.id).pick(:type)).to eq('Spree::Promotion::Rules::Category')
+ end
+ end
+
+ describe 'idempotency' do
+ let(:taxonomy) { create(:taxonomy, store: store) }
+ let!(:category) { create(:automatic_taxon, taxonomy: taxonomy, store: store) }
+ let!(:rule) { create(:tag_taxon_rule, taxon: category, value: 'sale', match_policy: 'is_equal_to') }
+
+ it 'is safe to run twice without duplicating collections' do
+ subject.invoke
+ subject.reenable
+
+ expect { subject.invoke }.not_to raise_error
+ expect(Spree::Collection.where(store: store).count).to eq(1)
+ end
+ end
+end