Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add timestamp columns to optimized_images table (#10199)

David Taylor3d65678a13b0
diff --git a/app/models/optimized_image.rb b/app/models/optimized_image.rb
index 98402079cc8..589cc5f1562 100644
--- a/app/models/optimized_image.rb
+++ b/app/models/optimized_image.rb
@@ -360,16 +360,18 @@ end
#
# Table name: optimized_images
#
-# id :integer not null, primary key
-# sha1 :string(40) not null
-# extension :string(10) not null
-# width :integer not null
-# height :integer not null
-# upload_id :integer not null
-# url :string not null
-# filesize :integer
-# etag :string
-# version :integer
+# id :integer not null, primary key
+# sha1 :string(40) not null
+# extension :string(10) not null
+# width :integer not null
+# height :integer not null
+# upload_id :integer not null
+# url :string not null
+# filesize :integer
+# etag :string
+# version :integer
+# created_at :datetime not null
+# updated_at :datetime not null
#
# Indexes
#
diff --git a/db/migrate/20200709094846_add_timestamps_to_optimized_images.rb b/db/migrate/20200709094846_add_timestamps_to_optimized_images.rb
new file mode 100644
index 00000000000..32f2a1349b2
--- /dev/null
+++ b/db/migrate/20200709094846_add_timestamps_to_optimized_images.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+class AddTimestampsToOptimizedImages < ActiveRecord::Migration[6.0]
+ def change
+ add_column :optimized_images, :created_at, :datetime, null: true
+ add_column :optimized_images, :updated_at, :datetime, null: true
+
+ # Start by stealing created/updated at from the uploads table
+ # Not perfect, but a good approximation
+ execute <<~SQL
+ UPDATE optimized_images
+ SET created_at = uploads.created_at,
+ updated_at = uploads.created_at
+ FROM uploads
+ WHERE uploads.id = optimized_images.upload_id
+ SQL
+
+ # Integrity is not enforced, we might have optimized images
+ # with no uploads
+ execute <<~SQL
+ UPDATE optimized_images
+ SET created_at = NOW(),
+ updated_at = NOW()
+ WHERE created_at IS NULL
+ SQL
+
+ execute <<~SQL
+ ALTER TABLE optimized_images ALTER COLUMN created_at SET NOT NULL;
+ SQL
+
+ execute <<~SQL
+ ALTER TABLE optimized_images ALTER COLUMN updated_at SET NOT NULL;
+ SQL
+ end
+end
diff --git a/lib/s3_inventory.rb b/lib/s3_inventory.rb
index e23623096ab..de273ceb2ec 100644
--- a/lib/s3_inventory.rb
+++ b/lib/s3_inventory.rb
@@ -61,7 +61,7 @@ class S3Inventory
WHERE #{model.table_name}.etag IS NULL AND
#{model.table_name}.url = #{table_name}.url")
- uploads = (model == Upload) ? model.by_users.where("updated_at < ?", inventory_date) : model
+ uploads = model.by_users.where("updated_at < ?", inventory_date)
missing_uploads = uploads
.joins("LEFT JOIN #{table_name} ON #{table_name}.etag = #{model.table_name}.etag")
.where("#{table_name}.etag IS NULL AND #{model.table_name}.etag IS NOT NULL")