|
| 1 | +class ConvertToActiveStorage < ActiveRecord::Migration[5.2] |
| 2 | + require 'open-uri' |
| 3 | + |
| 4 | + def up |
| 5 | + ActiveRecord::Base.connection.raw_connection.prepare("active_storage_blob_statement", <<-SQL) |
| 6 | + INSERT INTO active_storage_blobs ( |
| 7 | + key, filename, content_type, metadata, byte_size, checksum, created_at |
| 8 | + ) VALUES ($1, $2, $3, '{}', $4, $5, $6) |
| 9 | + SQL |
| 10 | + |
| 11 | + ActiveRecord::Base.connection.raw_connection.prepare("active_storage_attachment_statement", <<-SQL) |
| 12 | + INSERT INTO active_storage_attachments ( |
| 13 | + name, record_type, record_id, blob_id, created_at |
| 14 | + ) VALUES ($1, $2, $3, $4, $5) |
| 15 | + SQL |
| 16 | + |
| 17 | + Rails.application.eager_load! |
| 18 | + models = ActiveRecord::Base.descendants.reject(&:abstract_class?) |
| 19 | + |
| 20 | + transaction do |
| 21 | + models.each do |model| |
| 22 | + attachments = model.column_names.map do |c| |
| 23 | + if c =~ /(.+)_file_name$/ |
| 24 | + $1 |
| 25 | + end |
| 26 | + end.compact |
| 27 | + |
| 28 | + if attachments.blank? |
| 29 | + next |
| 30 | + end |
| 31 | + |
| 32 | + model.find_each.each do |instance| |
| 33 | + attachments.each do |attachment| |
| 34 | + if instance.send(attachment).path.blank? |
| 35 | + next |
| 36 | + end |
| 37 | + |
| 38 | + make_active_storage_records(instance, attachment, model) |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + def down |
| 46 | + raise ActiveRecord::IrreversibleMigration |
| 47 | + end |
| 48 | + |
| 49 | + private |
| 50 | + |
| 51 | + def make_active_storage_records(instance, attachment, model) |
| 52 | + blob_key = key() |
| 53 | + filename = instance.send("#{attachment}_file_name") |
| 54 | + content_type = instance.send("#{attachment}_content_type") |
| 55 | + file_size = instance.send("#{attachment}_file_size") |
| 56 | + file_checksum = checksum(instance.send(attachment)) |
| 57 | + created_at = instance.updated_at.iso8601 |
| 58 | + |
| 59 | + blob_values = [blob_key, filename, content_type, file_size, file_checksum, created_at] |
| 60 | + |
| 61 | + ActiveRecord::Base.connection.raw_connection.exec_prepared( |
| 62 | + "active_storage_blob_statement", |
| 63 | + blob_values |
| 64 | + ) |
| 65 | + |
| 66 | + last_blob_id = ActiveStorage::Blob.last.id |
| 67 | + |
| 68 | + blob_name = attachment |
| 69 | + record_type = model.name |
| 70 | + record_id = instance.id |
| 71 | + |
| 72 | + attachment_values = [blob_name, record_type, record_id, last_blob_id, created_at] |
| 73 | + ActiveRecord::Base.connection.raw_connection.exec_prepared( |
| 74 | + "active_storage_attachment_statement", |
| 75 | + attachment_values |
| 76 | + ) |
| 77 | + end |
| 78 | + |
| 79 | + def key |
| 80 | + SecureRandom.uuid |
| 81 | + end |
| 82 | + |
| 83 | + def checksum(attachment) |
| 84 | + url = attachment.url |
| 85 | + Digest::MD5.base64digest(Net::HTTP.get(URI(url))) |
| 86 | + end |
| 87 | +end |
0 commit comments