From: Tom Hughes Date: Sun, 17 Oct 2021 18:19:24 +0000 (+0100) Subject: Add a rake task to migrate trace data to ActiveStorage X-Git-Tag: live~1299^2 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/b244905cd2f8d23e97cb6f64c41da078651af29a Add a rake task to migrate trace data to ActiveStorage --- diff --git a/app/models/trace.rb b/app/models/trace.rb index 17f8ebc1e..bdafdd9d5 100644 --- a/app/models/trace.rb +++ b/app/models/trace.rb @@ -300,6 +300,26 @@ class Trace < ApplicationRecord end end + def migrate_to_storage! + file.attach(:io => File.open(trace_name), + :filename => name, + :content_type => content_type(trace_name), + :identify => false) + + if inserted + image.attach(:io => File.open(large_picture_name), + :filename => "#{id}.gif", + :content_type => "image/gif") + icon.attach(:io => File.open(icon_picture_name), + :filename => "#{id}_icon.gif", + :content_type => "image/gif") + end + + save! + + remove_files + end + private def content_type(file) diff --git a/lib/tasks/migrate_traces_to_storage.rake b/lib/tasks/migrate_traces_to_storage.rake new file mode 100644 index 000000000..11909ff21 --- /dev/null +++ b/lib/tasks/migrate_traces_to_storage.rake @@ -0,0 +1,9 @@ +namespace :traces do + desc "Migrate trace files to ActiveStorage" + task :migrate_to_storage => :environment do + Trace + .with_attached_file + .where(:file_attachment => { :id => nil }) + .find_each(&:migrate_to_storage!) + end +end