From b244905cd2f8d23e97cb6f64c41da078651af29a Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Sun, 17 Oct 2021 19:19:24 +0100 Subject: [PATCH] Add a rake task to migrate trace data to ActiveStorage --- app/models/trace.rb | 20 ++++++++++++++++++++ lib/tasks/migrate_traces_to_storage.rake | 9 +++++++++ 2 files changed, 29 insertions(+) create mode 100644 lib/tasks/migrate_traces_to_storage.rake 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 -- 2.39.5