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 Oct 18, 2021
1 parent fd68837 commit 8554ec3
Show file tree
Hide file tree
Showing 2 changed files with 27 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 @@ -302,6 +302,26 @@ def import
gpx
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
7 changes: 7 additions & 0 deletions lib/tasks/migrate_traces_to_storage.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace :traces do
task :migrate_to_storage => :environment do
Trace.with_attached_file.where(:file_attachment => { :id => nil }).find_each do |trace|
trace.migrate_to_storage!
end
end
end

0 comments on commit 8554ec3

Please sign in to comment.