FIX: `Jobs::CleanUpUploads` fails when value of upload data_type is an empty string.

FIX: Jobs::CleanUpUploads fails when value of upload data_type is an empty string.

From eecd1a7d8c41f23bff02808b3a574059dde7964b Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Fri, 30 Nov 2018 10:46:39 +0800
Subject: [PATCH] FIX: `Jobs::CleanUpUploads` fails when value of upload
 data_type is an empty string.


diff --git a/app/jobs/scheduled/clean_up_uploads.rb b/app/jobs/scheduled/clean_up_uploads.rb
index e23a520..3c4f37c 100644
--- a/app/jobs/scheduled/clean_up_uploads.rb
+++ b/app/jobs/scheduled/clean_up_uploads.rb
@@ -48,7 +48,7 @@ module Jobs
         .where("uploads.created_at < ?", grace_period.hour.ago)
         .joins(<<~SQL)
           LEFT JOIN site_settings ss
-          ON ss.value::integer = uploads.id
+          ON NULLIF(ss.value, '')::integer = uploads.id
           AND ss.data_type = #{SiteSettings::TypeSupervisor.types[:upload].to_i}
         SQL
         .joins("LEFT JOIN post_uploads pu ON pu.upload_id = uploads.id")
diff --git a/spec/jobs/clean_up_uploads_spec.rb b/spec/jobs/clean_up_uploads_spec.rb
index b8957b6..fe0aa1c 100644
--- a/spec/jobs/clean_up_uploads_spec.rb
+++ b/spec/jobs/clean_up_uploads_spec.rb
@@ -48,6 +48,7 @@ describe Jobs::CleanUpUploads do
     begin
       original_provider = SiteSetting.provider
       SiteSetting.provider = SiteSettings::DbProvider.new(SiteSetting)
+      SiteSetting.clean_orphan_uploads_grace_period_hours = 1
 
       logo_upload = fabricate_upload
       logo_small_upload = fabricate_upload
@@ -85,6 +86,11 @@ describe Jobs::CleanUpUploads do
         favicon_upload,
         apple_touch_icon_upload
       ].each { |record| expect(Upload.exists?(id: record.id)).to eq(true) }
+
+      fabricate_upload
+      SiteSetting.opengraph_image = ''
+
+      Jobs::CleanUpUploads.new.execute(nil)
     ensure
       SiteSetting.delete_all
       SiteSetting.provider = original_provider

GitHub

2 Likes