tools: add task to renew blob signed_ids

This commit is contained in:
simon lehericey 2023-06-19 23:32:01 +02:00
parent 4eaef72a22
commit 6dd6ddd8c6

37
lib/tasks/blobs.rake Normal file
View file

@ -0,0 +1,37 @@
namespace :blobs do
desc <<~EOD
given a file blob_ids.json with contains { blob_ids: [...] },
download and reattached champ piece_justificative_file to create new blob
and delete the old one, and thus, invalides old signed_ids.
bin/rails 'blobs:renew_signed_ids[blob_ids.json]'
EOD
task :renew_signed_ids, [:file_path] => :environment do |_t, args|
blob_ids = JSON.parse(File.read(args[:file_path]))['blob_ids']
blobs = ActiveStorage::Blob.find(blob_ids)
blobs.each do |blob|
blob.open do |file|
blob.attachments.each do |attachment|
if [attachment.record_type, attachment.name] != ['Champ', 'piece_justificative_file']
raise "not a piece justificative #{attachment.id}"
end
champ = attachment.record
champ_updated_at = champ.updated_at
dossier = champ.dossier
dossier_updated_at = dossier.updated_at
file.rewind
# badly, it updates champ and dossier updated_at attributes
champ.piece_justificative_file.attach(io: file, filename: blob.filename, content_type: blob.content_type)
# it destroys the blob as well
attachment.destroy
champ.update_column(:updated_at, champ_updated_at)
dossier.update_column(:updated_at, dossier_updated_at)
end
end
end
end
end