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.

Detach a deleted file's attachments only after the delete commits

Damian Legawiec97cbae30d899
diff --git a/spree/core/app/services/spree/media_deletion.rb b/spree/core/app/services/spree/media_deletion.rb
index d9ef7cb4e2..a27c4c1e4c 100644
--- a/spree/core/app/services/spree/media_deletion.rb
+++ b/spree/core/app/services/spree/media_deletion.rb
@@ -18,12 +18,25 @@ module Spree
def call(media:)
blob_ids = [media.attachment_blob&.id, media.poster_blob&.id].compact
+ # Detaching is collected inside the transaction and enqueued after it
+ # commits: purge_later hands the file to a worker that may run before
+ # the transaction ends, so a rollback would leave the rows intact with
+ # their file already gone.
+ detachable = nil
+
ApplicationRecord.transaction do
destroy_placements(media, blob_ids)
- detach_plain_attachments(media, blob_ids)
+ detachable = plain_attachments(media, blob_ids)
media.destroy!
end
+ # ActiveRecord::Rollback is swallowed by the transaction block, so a
+ # surviving row is how a rolled-back delete reports itself. Detaching
+ # then would strip the file from records the delete did not remove.
+ return failure(media) if media.persisted?
+
+ detachable.each(&:purge_later)
+
success(media)
end
@@ -43,13 +56,16 @@ module Spree
.find_each(&:destroy!)
end
- def detach_plain_attachments(media, blob_ids)
- ActiveStorage::Attachment.where(blob_id: blob_ids).find_each do |attachment|
+ # The plain attachments this store holds on the file — a category image
+ # slot, a store logo. Media rows are handled by destroy_placements.
+ #
+ # @return [Array<ActiveStorage::Attachment>]
+ def plain_attachments(media, blob_ids)
+ ActiveStorage::Attachment.where(blob_id: blob_ids).select do |attachment|
owner = attachment.record
- next if owner.blank? || owner.is_a?(Spree::Media)
- next unless owned_by_store?(owner, media.store_id)
+ next false if owner.blank? || owner.is_a?(Spree::Media)
- attachment.purge_later
+ owned_by_store?(owner, media.store_id)
end
end
diff --git a/spree/core/spec/services/spree/media_deletion_spec.rb b/spree/core/spec/services/spree/media_deletion_spec.rb
index eea24aad27..34b0e47fe7 100644
--- a/spree/core/spec/services/spree/media_deletion_spec.rb
+++ b/spree/core/spec/services/spree/media_deletion_spec.rb
@@ -51,6 +51,22 @@ describe Spree::MediaDeletion do
expect(ActiveStorage::Blob.exists?(blob.id)).to be(true)
end
+ # purge_later hands the file to a worker that can run before the transaction
+ # ends, so enqueueing inside it would destroy the file of a delete that then
+ # rolled back.
+ it 'does not detach anything when the deletion fails' do
+ category = create(:category)
+ category.image.attach(media.attachment.blob)
+ category.save!
+ allow(media).to receive(:destroy!).and_raise(ActiveRecord::Rollback)
+
+ perform_enqueued_jobs do
+ described_class.call(media: media) rescue nil
+ end
+
+ expect(category.reload.image).to be_attached
+ end
+
it 'leaves unrelated files alone' do
unrelated = create(:image, viewable: create(:product))