2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-30 00:17:35 -04:00
|
|
|
# This takes all the system avatars out of the upload system, saving us a huge
|
|
|
|
# amount of space on backups
|
2017-08-31 00:06:56 -04:00
|
|
|
class RemoveSystemAvatarsFromUserAvatars < ActiveRecord::Migration[4.2]
|
2014-05-30 00:17:35 -04:00
|
|
|
def up
|
|
|
|
execute "UPDATE users SET uploaded_avatar_id = NULL WHERE uploaded_avatar_id IN (
|
|
|
|
SELECT system_upload_id FROM user_avatars
|
|
|
|
)"
|
|
|
|
|
|
|
|
# normally we dont reach into the object model, but we have to here.
|
|
|
|
# otherwise we will wait a real long time for uploads to go away
|
|
|
|
skip = -1
|
|
|
|
while skip = destroy_system_avatar_batch(skip)
|
|
|
|
puts "Destroyed up to id: #{skip}"
|
|
|
|
end
|
|
|
|
|
|
|
|
remove_column :user_avatars, :system_upload_id
|
|
|
|
remove_column :user_avatars, :system_avatar_version
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_system_avatar_batch(skip)
|
|
|
|
initial = skip
|
|
|
|
|
|
|
|
Upload
|
|
|
|
.where("id IN (SELECT system_upload_id FROM user_avatars) AND id > ?", skip)
|
|
|
|
.order(:id)
|
|
|
|
.limit(500)
|
|
|
|
.each do |upload|
|
|
|
|
skip = upload.id
|
|
|
|
begin
|
|
|
|
upload.destroy
|
|
|
|
rescue StandardError
|
|
|
|
Rails.logger.warn "Could not destroy system avatar #{upload.id}"
|
2023-01-09 06:59:41 -05:00
|
|
|
end
|
2014-05-30 00:17:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
skip == initial ? nil : skip
|
|
|
|
rescue StandardError
|
|
|
|
Rails.logger.warn "Could not destroy system avatars, skipping"
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|