Migrations and helpers to move from Paperclip to ActiveStorage
To migrate the application as of April 2020: • leave models/screenshot.rb with “has_attached_file” • bundle exec rake db:migrate • bin/move_paperclip_to_activestorage • set models/screenshot.rb to “has_one_attached”
This commit is contained in:
parent
a0203d32af
commit
8adf682a0b
6 changed files with 239 additions and 0 deletions
34
lib/tasks/paperclip_to_activestorage.rake
Normal file
34
lib/tasks/paperclip_to_activestorage.rake
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
namespace :debshots do
|
||||
desc "Convert images from Paperclip schema to ActiveStorage schema"
|
||||
|
||||
task paperclip_to_activestorage: :environment do
|
||||
klass = Screenshot
|
||||
attachment = 'image'
|
||||
name_field = :"#{attachment}_file_name"
|
||||
|
||||
klass.where.not(name_field => nil).find_each do |instance|
|
||||
# This step helps us catch any attachments we might have uploaded that
|
||||
# don't have an explicit file extension in the filename
|
||||
|
||||
puts "----------"
|
||||
filename = instance.send("#{attachment}_file_name")
|
||||
puts filename
|
||||
|
||||
next if filename.blank?
|
||||
|
||||
id = instance.id
|
||||
id_partition = ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
|
||||
path = "storage/#{klass.table_name}/#{attachment.pluralize}/#{id_partition}/original/#{filename}"
|
||||
puts path
|
||||
|
||||
#url = "https://nyc3.digitaloceanspaces.com/gorails/#{klass.table_name}/#{attachment.pluralize}/#{id_partition}/original/#{filename}"
|
||||
|
||||
instance.send(attachment.to_sym).attach(
|
||||
# io: open(url),
|
||||
io: File.open(path),
|
||||
filename: instance.send(name_field),
|
||||
content_type: instance.send(:"#{attachment}_content_type")
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue