Add `created_before` and `updated_before` scopes to `MediaAttachment` (#28869)

main
Matt Jankowski 2024-01-24 05:32:54 -05:00 committed by GitHub
parent a11a2fb052
commit 7a1f087659
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 8 deletions

View File

@ -27,11 +27,17 @@ class Vacuum::MediaAttachmentsVacuum
end end
def media_attachments_past_retention_period def media_attachments_past_retention_period
MediaAttachment.remote.cached.where(MediaAttachment.arel_table[:created_at].lt(@retention_period.ago)).where(MediaAttachment.arel_table[:updated_at].lt(@retention_period.ago)) MediaAttachment
.remote
.cached
.created_before(@retention_period.ago)
.updated_before(@retention_period.ago)
end end
def orphaned_media_attachments def orphaned_media_attachments
MediaAttachment.unattached.where(MediaAttachment.arel_table[:created_at].lt(TTL.ago)) MediaAttachment
.unattached
.created_before(TTL.ago)
end end
def retention_period? def retention_period?

View File

@ -204,12 +204,14 @@ class MediaAttachment < ApplicationRecord
validates :file, presence: true, if: :local? validates :file, presence: true, if: :local?
validates :thumbnail, absence: true, if: -> { local? && !audio_or_video? } validates :thumbnail, absence: true, if: -> { local? && !audio_or_video? }
scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) } scope :attached, -> { where.not(status_id: nil).or(where.not(scheduled_status_id: nil)) }
scope :cached, -> { remote.where.not(file_file_name: nil) } scope :cached, -> { remote.where.not(file_file_name: nil) }
scope :local, -> { where(remote_url: '') } scope :created_before, ->(value) { where(arel_table[:created_at].lt(value)) }
scope :ordered, -> { order(id: :asc) } scope :local, -> { where(remote_url: '') }
scope :remote, -> { where.not(remote_url: '') } scope :ordered, -> { order(id: :asc) }
scope :remote, -> { where.not(remote_url: '') }
scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) } scope :unattached, -> { where(status_id: nil, scheduled_status_id: nil) }
scope :updated_before, ->(value) { where(arel_table[:updated_at].lt(value)) }
attr_accessor :skip_download attr_accessor :skip_download

View File

@ -120,7 +120,7 @@ module Mastodon::CLI
say('Beginning removal of now-orphaned media attachments to free up disk space...') say('Beginning removal of now-orphaned media attachments to free up disk space...')
scope = MediaAttachment.unattached.where('created_at < ?', options[:days].pred.days.ago) scope = MediaAttachment.unattached.created_before(options[:days].pred.days.ago)
processed = 0 processed = 0
removed = 0 removed = 0
progress = create_progress_bar(scope.count) progress = create_progress_bar(scope.count)