Skip to content

Instantly share code, notes, and snippets.

@taimoor
Forked from serek/migration.rb
Last active August 29, 2015 14:20
Show Gist options
  • Save taimoor/d11a92f5cfd7ed79001f to your computer and use it in GitHub Desktop.
Save taimoor/d11a92f5cfd7ed79001f to your computer and use it in GitHub Desktop.
migration of Attachment_fu to Paperclip

To make rerocess! work add these lines to your Gemfile. Try to add paperclip specific version compatible cocaine gem version. For my case those were below:

gem "paperclip", "~> 3.0.4" gem 'cocaine', '0.3.1'

Also run this command in your console

gem install rmagick

Then run your migration.

If the reprocess! method of paperclip still not generating thumbnail/styles images. Just creating original images clone. So to resolve this we have to run command [1] below:

rake paperclip:refresh:missing_styles

Above command will generate thumbnails as well as a file at public/system/paperclip_attachments.yml if you want to regenerate thumbnails remove this file and run the above command again.

  1. https://github.com/thoughtbot/paperclip/wiki/Thumbnail-Generation
class ConvertImagesToPaperclip < ActiveRecord::Migration
include PaperclipMigrations
def self.up
# Paperclip
add_column :images, :data_file_name, :string
add_column :images, :data_content_type, :string
add_column :images, :data_file_size, :integer
add_column :images, :data_updated_at, :datetime
# Update table information
Image.reset_column_information
# Delete all attachement_fu image sizes
Image.delete_all("parent_id IS NOT NULL")
remove_column :images, :parent_id
# Migrate data
Image.all.each do |image|
populate_paperclip_from_attachment_fu(image, image, 'data', 'userimages') if image
image.reprocess! if image
end
# After data migration and paperclip reprocessing remove attachement_fu columns
remove_column :images, :filename
remove_column :images, :content_type
remove_column :images, :size
end
def self.down
raise ActiveRecord::IrreversibleMigration, "Can't recover the deleted data (Image)."
end
end
module PaperclipMigrations
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def populate_paperclip_from_attachment_fu(model, attachment, prefix, path_prefix)
unless attachment.filename.nil?
model.send("#{prefix}_file_name=", attachment.filename)
model.send("#{prefix}_content_type=", attachment.content_type)
model.send("#{prefix}_file_size=", attachment.size)
puts "# data_file_name: #{model.send("#{prefix}_file_name")}"
puts "# data_content_type: #{model.send("#{prefix}_content_type")}"
puts "# data_file_size: #{model.send("#{prefix}_file_size")}"
# Get file path from attachment_fu
file_path = ("%08d" % model.id).scan(/..../).join('/')
old_path = File.join(RAILS_ROOT, 'public', path_prefix, file_path, attachment.filename)
new_path = model.send(prefix).path(:original)
new_folder = File.dirname(new_path)
if File.exists?(old_path)
unless File.exists?(new_folder)
FileUtils.mkdir_p(new_folder)
end
puts "Copying #{old_path} to #{new_path}"
system("cp #{old_path} #{new_path}")
model.save
else
puts "No such file: #{old_path}"
end
puts "# -------- END #"
end
end
end
end
@taimoor
Copy link
Author

taimoor commented Apr 27, 2015

RAILS_ROOT has been changed to Rails.root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment