Add a rake task to migrate trace data to ActiveStorage

This commit is contained in:
Tom Hughes 2021-10-17 19:19:24 +01:00
parent 0410596908
commit b244905cd2
2 changed files with 29 additions and 0 deletions

View file

@ -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)

View file

@ -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