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
|
|
|
|
class RemoveSystemAvatarsFromUserAvatars < ActiveRecord::Migration
|
|
|
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
while skip = destroy_system_avatar_batch(skip) do
|
2014-05-30 00:17:35 -04:00
|
|
|
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)
|
2017-07-27 21:20:09 -04:00
|
|
|
.order(:id)
|
|
|
|
.limit(500)
|
|
|
|
.each do |upload|
|
2014-05-30 00:17:35 -04:00
|
|
|
skip = upload.id
|
|
|
|
begin
|
|
|
|
upload.destroy
|
|
|
|
rescue
|
|
|
|
Rails.logger.warn "Could not destroy system avatar #{upload.id}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
skip == initial ? nil : skip
|
|
|
|
rescue
|
|
|
|
Rails.logger.warn "Could not destroy system avatars, skipping"
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
raise ActiveRecord::IrreversibleMigration
|
|
|
|
end
|
|
|
|
end
|