Created
September 10, 2019 09:25
-
-
Save mihaic195/693bd6a50fffb7ff5db556be878a467a to your computer and use it in GitHub Desktop.
Paperclip to ActiveStorage migration rake task
This file contains 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 :paperclip do | |
task migrate: :environment do | |
arr = Array.new | |
ApplicationRecord.descendants.reject(&:abstract_class?).each { |klass| arr.push(klass) } | |
while model = arr.pop | |
begin | |
attachments = model.column_names.filter { |column| column.match?(/(.+)_file_name$/) }.map { |column| column[/(?<name>\w*)_file_name/, :name] } | |
next if attachments.blank? | |
attachments.each do |attachment| | |
query = <<~SQL | |
NOT EXISTS( | |
SELECT * FROM active_storage_blobs AS blobs | |
INNER JOIN active_storage_attachments AS attachments ON attachments.blob_id = blobs.id | |
WHERE record_type = ? AND record_id = #{model.table_name}.id | |
) | |
SQL | |
model.where.not("#{attachment}_file_name" => nil).where(query, model.name).find_each do |instance| | |
instance.instance_variable_set(:@is_migration, true) | |
instance.duplicate_active_storage if instance.respond_to?(:duplicate_active_storage) | |
end | |
end | |
rescue OpenURI::HTTPError, ActiveRecord::StatementInvalid => e | |
Rails.logger.error("Error during ActiveStorage migration: #{e} \n for #{model}") | |
Raven.capture_exception("Error during ActiveStorage migration: #{e} \n for #{model}") | |
next | |
end | |
end | |
end | |
end |
Whoops one other thing you need to have
Rails.application.eager_load!
before you do
ApplicationRecord.descendants
or the result will be an empty array.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, I thought there might be some special thing that made you do it that way. I'll change my code as appropriate.