Add commentaire pj migration task
This commit is contained in:
parent
3cb39c2840
commit
be3283a9a8
2 changed files with 79 additions and 0 deletions
37
lib/tasks/2019_05_29_migrate_commentaire_pj.rake
Normal file
37
lib/tasks/2019_05_29_migrate_commentaire_pj.rake
Normal file
|
@ -0,0 +1,37 @@
|
|||
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?
|
||||
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 = commentaire.dossier.updated_at
|
||||
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)
|
||||
commentaire.dossier.update_column(:updated_at, dossier_updated_at)
|
||||
end
|
||||
end
|
||||
progress.inc
|
||||
end
|
||||
progress.finish
|
||||
end
|
||||
end
|
42
spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb
Normal file
42
spec/lib/tasks/2019_05_29_migrate_commentaire_pj_spec.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
describe '2019_05_29_migrate_commentaire_pj.rake' do
|
||||
let(:rake_task) { Rake::Task['2019_05_29_migrate_commentaire_pj:run'] }
|
||||
|
||||
let!(:commentaires) do
|
||||
create(:commentaire)
|
||||
create(:commentaire, :with_file)
|
||||
create(:commentaire, :with_file)
|
||||
end
|
||||
|
||||
before do
|
||||
Commentaire.all.each do |commentaire|
|
||||
if commentaire.file.present?
|
||||
stub_request(:get, commentaire.file_url)
|
||||
.to_return(status: 200, body: File.read(commentaire.file.path))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
after do
|
||||
ENV['LIMIT'] = nil
|
||||
rake_task.reenable
|
||||
end
|
||||
|
||||
it 'should migrate pj' do
|
||||
comment_updated_at = Commentaire.last.updated_at
|
||||
dossier_updated_at = Commentaire.last.dossier.updated_at
|
||||
expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, false, false])
|
||||
rake_task.invoke
|
||||
expect(Commentaire.where(file: nil).count).to eq(1)
|
||||
expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, true, true])
|
||||
expect(Commentaire.last.updated_at).to eq(comment_updated_at)
|
||||
expect(Commentaire.last.dossier.updated_at).to eq(dossier_updated_at)
|
||||
end
|
||||
|
||||
it 'should migrate pj within limit' do
|
||||
expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, false, false])
|
||||
ENV['LIMIT'] = '1'
|
||||
rake_task.invoke
|
||||
expect(Commentaire.where(file: nil).count).to eq(1)
|
||||
expect(Commentaire.all.map(&:piece_jointe).map(&:attached?)).to eq([false, true, false])
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue