From 839a54b97bb808baa86db4966d597fd400c73336 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Fri, 22 Mar 2019 16:46:13 +0800 Subject: [PATCH] FIX: Destroy `OptimizedImage` record even if `Upload` record is invalid. --- app/models/optimized_image.rb | 2 +- spec/models/optimized_image_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb index 74a45ad97b6..726f0e34a32 100644 --- a/app/models/optimized_image.rb +++ b/app/models/optimized_image.rb @@ -130,7 +130,7 @@ class OptimizedImage < ActiveRecord::Base def destroy OptimizedImage.transaction do - Discourse.store.remove_optimized_image(self) + Discourse.store.remove_optimized_image(self) if self.upload super end end diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb index 4a525401610..d3094631e1f 100644 --- a/spec/models/optimized_image_spec.rb +++ b/spec/models/optimized_image_spec.rb @@ -330,6 +330,18 @@ describe OptimizedImage do end + describe '#destroy' do + describe 'when upload_id is no longer valid' do + it 'should still destroy the record' do + image = Fabricate(:optimized_image) + image.upload.delete + image.reload.destroy + + expect(OptimizedImage.exists?(id: image.id)).to eq(false) + end + end + end + end class FakeInternalStore