demarches-normaliennes/lib/tasks/2019_05_29_migrate_commentaire_pj.rake

39 lines
1.3 KiB
Ruby
Raw Normal View History

2019-06-25 17:16:45 +02:00
namespace :'2019_05_29_migrate_commentaire_pj' do
task run: :environment do
commentaires = Commentaire.where
.not(file: nil)
.left_joins(:piece_jointe_attachment)
.where('active_storage_attachments.id IS NULL')
.order(:created_at)
limit = ENV['LIMIT']
if limit
commentaires.limit!(limit.to_i)
end
progress = ProgressReport.new(commentaires.count)
commentaires.find_each do |commentaire|
if commentaire.file.present?
dossier = Dossier.unscope(where: :hidden_at).find(commentaire.dossier_id)
2019-06-25 17:16:45 +02:00
uri = URI.parse(URI.escape(commentaire.file_url))
response = Typhoeus.get(uri)
if response.success?
filename = commentaire.file.filename || commentaire.file_identifier
updated_at = commentaire.updated_at
dossier_updated_at = dossier.updated_at
2019-06-25 17:16:45 +02:00
commentaire.piece_jointe.attach(
io: StringIO.new(response.body),
filename: filename,
content_type: commentaire.file.content_type,
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
)
commentaire.update_column(:updated_at, updated_at)
dossier.update_column(:updated_at, dossier_updated_at)
2019-06-25 17:16:45 +02:00
end
end
progress.inc
end
progress.finish
end
end