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 products/variants counter cache rake tasks

Damian Legawiec322e015d90fc
diff --git a/core/lib/tasks/products.rake b/core/lib/tasks/products.rake
index 9056ca79cc..2daa75a3aa 100644
--- a/core/lib/tasks/products.rake
+++ b/core/lib/tasks/products.rake
@@ -1,30 +1,20 @@
namespace :spree do
namespace :products do
- desc 'Reset variant_count counter cache on products'
- task reset_variant_count: :environment do |_t, _args|
- puts 'Resetting variant_count counter cache...'
- Spree::Product.in_batches.update_all(
- "variant_count = (SELECT COUNT(*) FROM spree_variants WHERE spree_variants.product_id = spree_products.id AND spree_variants.is_master = false AND spree_variants.deleted_at IS NULL)"
- )
- puts 'Done!'
- end
+ desc 'Reset counter caches (variant_count, classification_count, total_image_count) on products'
+ task reset_counter_caches: :environment do |_t, _args|
+ puts 'Resetting product counter caches...'
- desc 'Reset classification_count counter cache on products'
- task reset_classification_count: :environment do |_t, _args|
- puts 'Resetting classification_count counter cache...'
- Spree::Product.in_batches.update_all(
- "classification_count = (SELECT COUNT(*) FROM spree_products_taxons WHERE spree_products_taxons.product_id = spree_products.id)"
- )
- puts 'Done!'
- end
+ Spree::Product.find_each do |product|
+ product.update_columns(
+ variant_count: product.variants.count,
+ classification_count: product.classifications.count,
+ total_image_count: product.variant_images.count,
+ updated_at: Time.current
+ )
+ print '.'
+ end
- desc 'Reset total_image_count counter cache on products'
- task reset_total_image_count: :environment do |_t, _args|
- puts 'Resetting total_image_count counter cache...'
- Spree::Product.in_batches.update_all(
- "total_image_count = (SELECT COUNT(*) FROM spree_assets INNER JOIN spree_variants ON spree_assets.viewable_id = spree_variants.id AND spree_assets.viewable_type = 'Spree::Variant' WHERE spree_variants.product_id = spree_products.id AND spree_assets.type = 'Spree::Image')"
- )
- puts 'Done!'
+ puts "\nDone!"
end
desc 'Enqueue background jobs to populate product metrics for all store products'
diff --git a/core/lib/tasks/variants.rake b/core/lib/tasks/variants.rake
index 9519bf833b..ad3b43569d 100644
--- a/core/lib/tasks/variants.rake
+++ b/core/lib/tasks/variants.rake
@@ -1,12 +1,18 @@
namespace :spree do
namespace :variants do
desc 'Reset image_count counter cache on variants'
- task reset_image_count: :environment do |_t, _args|
- puts 'Resetting image_count counter cache...'
- Spree::Variant.in_batches.update_all(
- "image_count = (SELECT COUNT(*) FROM spree_assets WHERE spree_assets.viewable_id = spree_variants.id AND spree_assets.viewable_type = 'Spree::Variant' AND spree_assets.type = 'Spree::Image')"
- )
- puts 'Done!'
+ task reset_counter_caches: :environment do |_t, _args|
+ puts 'Resetting variant counter caches...'
+
+ Spree::Variant.find_each do |variant|
+ variant.update_columns(
+ image_count: variant.images.count,
+ updated_at: Time.current
+ )
+ print '.'
+ end
+
+ puts "\nDone!"
end
end
end