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 backfill_product_tag_tenants rake task (#14366)

Kacper Mękarskif5ec6cd0e5ab
diff --git a/spree/core/lib/tasks/product_tag_tenants.rake b/spree/core/lib/tasks/product_tag_tenants.rake
index 1202e52770..ea5ecf3af3 100644
--- a/spree/core/lib/tasks/product_tag_tenants.rake
+++ b/spree/core/lib/tasks/product_tag_tenants.rake
@@ -14,11 +14,9 @@ namespace :spree do
task backfill_product_tag_tenants: :environment do
taggings = ActsAsTaggableOn::Tagging.arel_table.name
products = Spree::Product.table_name
-
updated = ActsAsTaggableOn::Tagging.
where(taggable_type: 'Spree::Product', tenant: nil).
- joins("INNER JOIN #{products} ON #{products}.id = #{taggings}.taggable_id").
- update_all("tenant = #{products}.store_id")
+ update_all("tenant = (SELECT store_id FROM #{products} WHERE #{products}.id = #{taggings}.taggable_id)")
puts " Backfilled tenant on #{updated} product tagging(s)."
end
diff --git a/spree/core/spec/lib/tasks/product_tag_tenants_spec.rb b/spree/core/spec/lib/tasks/product_tag_tenants_spec.rb
new file mode 100644
index 0000000000..9b3e64efec
--- /dev/null
+++ b/spree/core/spec/lib/tasks/product_tag_tenants_spec.rb
@@ -0,0 +1,56 @@
+require 'spec_helper'
+require 'rake'
+
+describe 'spree:upgrade:backfill_product_tag_tenants' do
+ subject { Rake::Task[task_name] }
+
+ let(:task_name) { 'spree:upgrade:backfill_product_tag_tenants' }
+
+ before(:all) do
+ Rake::Task.define_task(:environment)
+ load Spree::Core::Engine.root.join('lib', 'tasks', 'product_tag_tenants.rake')
+ end
+
+ before { subject.reenable }
+
+ let!(:store) { Spree::Store.default || create(:store, default: true) }
+ let!(:product) { create(:product, stores: [store], tag_list: ['eco']) }
+
+ def tagging_for(taggable)
+ ActsAsTaggableOn::Tagging.find_by(taggable: taggable, context: 'tags')
+ end
+
+ def downgrade!(tagging)
+ tagging.update_columns(tenant: nil)
+ tagging
+ end
+
+ context 'product tagging created before the tenant backfill' do
+ let!(:tagging) { downgrade!(tagging_for(product)) }
+
+ it "backfills tenant from the product's store_id" do
+ expect { subject.invoke }.to change { tagging.reload.tenant }.from(nil).to(store.id.to_s)
+ end
+
+ it 'is idempotent' do
+ subject.invoke
+ subject.reenable
+ expect { subject.invoke }.not_to change { tagging.reload.tenant }
+ end
+ end
+
+ context 'orphaned tagging whose product no longer exists' do
+ let(:tag) { ActsAsTaggableOn::Tag.create!(name: 'orphan') }
+ let!(:orphan) do
+ tagging = ActsAsTaggableOn::Tagging.new(
+ tag: tag, taggable_type: 'Spree::Product', taggable_id: 0, context: 'tags', tenant: nil
+ )
+ tagging.save!(validate: false) # taggable presence is validated; forge a row pointing at a deleted product
+ tagging
+ end
+
+ it 'leaves tenant nil (the store_id subquery resolves to NULL)' do
+ expect { subject.invoke }.not_to change { orphan.reload.tenant }.from(nil)
+ end
+ end
+end