Skip to content

Commit

Permalink
Add a rake task to migrate trace data to ActiveStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Feb 13, 2022
1 parent 0cfeb43 commit 73e4347
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions app/models/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,26 @@ def import
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)
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/migrate_traces_to_storage.rake
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 73e4347

Please sign in to comment.