]> git.openstreetmap.org Git - rails.git/commitdiff
Add a rake task to migrate trace data to ActiveStorage
authorTom Hughes <tom@compton.nu>
Sun, 17 Oct 2021 18:19:24 +0000 (19:19 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 16 Dec 2021 18:45:31 +0000 (18:45 +0000)
app/models/trace.rb
lib/tasks/migrate_traces_to_storage.rake [new file with mode: 0644]

index 17f8ebc1e12d70779f58bf66d5df7ff9e1cd0690..bdafdd9d5c9116f4340ddd0af108fa86abbf549a 100644 (file)
@@ -300,6 +300,26 @@ class Trace < ApplicationRecord
     end
   end
 
     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)
   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 (file)
index 0000000..11909ff
--- /dev/null
@@ -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