Last active
May 12, 2018 16:36
-
-
Save takatoshiono/a6c5548f17ff487e88891d8307410760 to your computer and use it in GitHub Desktop.
paperclipからactive storageへのデータ移行タスク ref: http://takatoshiono.hatenablog.com/entry/2018/05/13/013606
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :convert_to_active_storage do | |
desc 'Convert to Activestorage' | |
task :run => :environment do | |
conn = ActiveRecord::Base.connection.raw_connection | |
Cafe.order(:id).each do |cafe| | |
puts "Convert cafe id=#{cafe.id}" | |
blob_params = [ | |
cafe.class.generate_unique_secure_token, | |
cafe.image_file_name, | |
cafe.image_content_type, | |
cafe.image_file_size, | |
checksum(cafe.image), | |
cafe.updated_at.iso8601 | |
] | |
attachment_params = ['image', 'Cafe', cafe.id, cafe.updated_at.iso8601] | |
conn.transaction do | |
puts "blob: #{blob_params}" | |
conn.exec_params(<<-SQL, blob_params) | |
INSERT INTO active_storage_blobs ( | |
key, filename, content_type, metadata, byte_size, checksum, created_at | |
) VALUES ($1, $2, $3, '{}', $4, $5, $6) | |
SQL | |
puts "attachment: #{attachment_params}" | |
conn.exec_params(<<-SQL, attachment_params) | |
INSERT INTO active_storage_attachments ( | |
name, record_type, record_id, blob_id, created_at | |
) VALUES ($1, $2, $3, LASTVAL(), $4) | |
SQL | |
end | |
end | |
end | |
private | |
def checksum(image) | |
body = if Rails.env.development? | |
File.read(image.path) | |
else | |
uri = URI.parse("https:#{image.url}") | |
Net::HTTP.get(uri) | |
end | |
Digest::MD5.base64digest(body) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveStorage::Blob < ActiveRecord::Base | |
end | |
class ActiveStorage::Attachment < ActiveRecord::Base | |
belongs_to :blob, class_name: 'ActiveStorage::Blob' | |
belongs_to :record, polymorphic: true | |
end | |
namespace :copy_images do | |
desc "Copy images to the Active Storage's path" | |
task :run => :environment do | |
ActiveStorage::Attachment.order(:id).each do |attachment| | |
name = attachment.name | |
source = attachment.record.send(name).path | |
if Rails.env.development? | |
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) | |
puts "Copy #{source} to #{dest}" | |
FileUtils.mkdir_p(dest_dir) | |
FileUtils.cp(source, dest) | |
else | |
dest = attachment.blob.key | |
puts "Copy #{source} to #{dest}" | |
s3 = Aws::S3::Client.new(region: ENV.fetch('AWS_REGION')) | |
s3.copy_object( | |
bucket: ENV.fetch('S3_BUCKET_NAME'), | |
copy_source: ENV.fetch('S3_BUCKET_NAME') + source, | |
key: dest | |
) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment