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

This allows us to filter by created/updated date when comparing to an S3 inventory.
This commit is contained in:
David Taylor 2020-07-14 11:50:33 +01:00 committed by GitHub
parent 94a2a70462
commit 3d65678a13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 11 deletions

View File

@ -370,6 +370,8 @@ end
# filesize :integer # filesize :integer
# etag :string # etag :string
# version :integer # version :integer
# created_at :datetime not null
# updated_at :datetime not null
# #
# Indexes # Indexes
# #

View File

@ -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

View File

@ -61,7 +61,7 @@ class S3Inventory
WHERE #{model.table_name}.etag IS NULL AND WHERE #{model.table_name}.etag IS NULL AND
#{model.table_name}.url = #{table_name}.url") #{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 missing_uploads = uploads
.joins("LEFT JOIN #{table_name} ON #{table_name}.etag = #{model.table_name}.etag") .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") .where("#{table_name}.etag IS NULL AND #{model.table_name}.etag IS NOT NULL")