FEATURE: store thumbnail algorithm version in optimized image table

FEATURE: store thumbnail algorithm version in optimized image table

Previously we had no idea what algorithm generated thumbnails, this starts tracking the version.

We also bumped up the version to force all optimized images to be generated. This is important cause we recently introduced pngquant which results in much smaller images.

diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 5e5e8e4..5cc2484 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -7,7 +7,7 @@ class OptimizedImage < ActiveRecord::Base
   belongs_to :upload
 
   # BUMP UP if optimized image algorithm changes
-  VERSION = 1
+  VERSION = 2
 
   def self.lock(upload_id, width, height)
     @hostname ||= `hostname`.strip rescue "unknown"
@@ -43,7 +43,7 @@ class OptimizedImage < ActiveRecord::Base
     thumbnail = find_by(upload_id: upload.id, width: width, height: height)
 
     # correct bad thumbnail if needed
-    if thumbnail && thumbnail.url.blank?
+    if thumbnail && (thumbnail.url.blank? || thumbnail.version != VERSION)
       thumbnail.destroy!
       thumbnail = nil
     end
@@ -94,7 +94,8 @@ class OptimizedImage < ActiveRecord::Base
             width: width,
             height: height,
             url: "",
-            filesize: File.size(temp_path)
+            filesize: File.size(temp_path),
+            version: VERSION
           )
 
           # store the optimized image and update its url
diff --git a/db/migrate/20190103051737_add_version_to_optimized_images.rb b/db/migrate/20190103051737_add_version_to_optimized_images.rb
new file mode 100644
index 0000000..88d2fb9
--- /dev/null
+++ b/db/migrate/20190103051737_add_version_to_optimized_images.rb
@@ -0,0 +1,5 @@
+class AddVersionToOptimizedImages < ActiveRecord::Migration[5.2]
+  def change
+    add_column :optimized_images, :version, :integer
+  end
+end
diff --git a/spec/models/optimized_image_spec.rb b/spec/models/optimized_image_spec.rb
index 35e95cd..4c832a8 100644
--- a/spec/models/optimized_image_spec.rb
+++ b/spec/models/optimized_image_spec.rb
@@ -201,6 +201,32 @@ describe OptimizedImage do
 
   describe ".create_for" do
 
+    context "versioning" do
+      let(:filename) { 'logo.png' }
+      let(:file) { file_from_fixtures(filename) }
+
+      it "is able to update optimized images on version change" do
+        upload = UploadCreator.new(file, filename).create_for(Discourse.system_user.id)
+        optimized = OptimizedImage.create_for(upload, 10, 10)
+
+        expect(optimized.version).to eq(OptimizedImage::VERSION)
+
+        optimized_again = OptimizedImage.create_for(upload, 10, 10)
+        expect(optimized_again.id).to eq(optimized.id)
+
+        optimized.update_columns(version: nil)
+        old_id = optimized.id
+
+        optimized_new = OptimizedImage.create_for(upload, 10, 10)
+
+        expect(optimized_new.id).not_to eq(old_id)
+
+        # cleanup (which transaction rollback may miss)
+        optimized_new.destroy
+        upload.destroy
+      end
+    end
+
     it "is able to 'optimize' an svg" do
       # we don't really optimize anything, we simply copy
       # but at least this confirms this actually works

GitHub
sha: 570877da

1 Like

Fix the build.

This commit has been mentioned on Discourse Meta. There might be relevant details there: