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
18
bin/move_paperclip_to_activestorage
Executable file
18
bin/move_paperclip_to_activestorage
Executable file
|
|
@ -0,0 +1,18 @@
|
|||
#!bin/rails runner
|
||||
|
||||
# Move images from Paperclip path to ActiveRecord path
|
||||
|
||||
ActiveStorage::Attachment.find_each do |attachment|
|
||||
name = attachment.name
|
||||
|
||||
source = attachment.record.send(name).path
|
||||
dest_dir = File.join(
|
||||
"storage",
|
||||
attachment.blob.key.first(2),
|
||||
attachment.blob.key.first(4).last(2))
|
||||
dest = File.join(dest_dir, attachment.blob.key)
|
||||
|
||||
FileUtils.mkdir_p(dest_dir)
|
||||
puts "Moving #{source} to #{dest}"
|
||||
FileUtils.cp(source, dest)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue