Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@antun
Created October 2, 2012 04:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save antun/3816211 to your computer and use it in GitHub Desktop.
Save antun/3816211 to your computer and use it in GitHub Desktop.
Rails migration for switching from file_column to paperclip.
require 'fileutils'
class MigrateUsersToPaperclip < ActiveRecord::Migration
def up
# Rename the old "mugshot" column to "old_file_name", since User.mugshot will now try to do Paperclip stuff
rename_column :users, :mugshot, :old_file_name
# Rename asset directories to pluralize
File.rename("public/system/user","public/system/users")
File.rename("public/system/users/mugshot","public/system/users/mugshots")
User.all.each do |u|
unless u.old_file_name.blank? && u.old_file_name != '/mugshots/original/missing.png'
# If an image was set by file_column, copy the relevant parameters to the new Paperclip columns
# IMPORTANT: Set config.active_record.whitelist_attributes = false in config/application.rb before running this script
u.update_attributes(
:mugshot_file_name => u.old_file_name,
:mugshot_content_type => 'image/jpeg',
:mugshot_updated_at => Time.now.utc,
:mugshot_file_size => File.stat("public/system/users/mugshots/#{u.id}/#{u.old_file_name}").size
)
# Now rename the image file to add the original_ prefix
File.rename("public/system/users/mugshots/#{u.id}/#{u.old_file_name}","public/system/users/mugshots/#{u.id}/original_#{u.old_file_name}")
# Also need to rename the various files sizes.
# File column stores them like this:
# public/system/users/mugshots/2/thumb/antun_jump.jpg
#
# Paperclip stores them like this:
# public/system/users/mugshots/2/thumb_antun_jump.jpg
u.mugshot.styles.each do |s|
style_name = s[0]
FileUtils.mv("public/system/users/mugshots/#{u.id}/#{style_name}/#{u.old_file_name}", "public/system/users/mugshots/#{u.id}/#{style_name}_#{u.old_file_name}")
end
end
end
# And finally remove the old_file_name column, which we don't need any more
remove_column :users, :old_file_name
end
def down
# Rename the old "mugshot" column to "old_file_name", since User.mugshot will now try to do Paperclip stuff
if column_exists? :users, :old_file_name
then
# Previous migration exited with error, so just restore that temporary column.
rename_column :users, :old_file_name, :mugshot
elsif column_exists? :users, :mugshot
then
# Previous migration exited with error, so just restore that temporary column.
# Do nothing.
else
# Previous migration was completed, so need to restore the column altogether.
# However, we can't create a :mugshot column yet, because at this point,
# Paperclip uses :mugshot, and will complain if you try to update_attributes
# on it.
add_column :users, :mugshot_temp, :string
User.all.each do |u|
unless u.mugshot_file_name.blank?
u.update_attributes(
:mugshot_temp => u.mugshot_file_name,
:mugshot_file_name => nil,
:mugshot_content_type => nil,
:mugshot_updated_at => nil,
:mugshot_file_size => nil
)
end
end
rename_column :users, :mugshot_temp, :mugshot
end
# Rename asset directories to singularize
if File.directory? "public/system/users/mugshots"
then
File.rename("public/system/users/mugshots","public/system/users/mugshot")
end
if File.directory? "public/system/users"
then
File.rename("public/system/users","public/system/user")
end
# Use temporary name for column so that we don't trip-up over default paperclip functionality
rename_column :users, :mugshot, :mugshot_temp
# Rename size assets back to file_column style:
# public/system/users/mugshots/2/thumb/antun_jump.jpg
#
# Paperclip stores them like this:
# public/system/users/mugshots/2/thumb_antun_jump.jpg
User.all.each do |u|
unless u.mugshot_temp.blank?
File.rename("public/system/user/mugshot/#{u.id}/original_#{u.mugshot_temp}", "public/system/user/mugshot/#{u.id}/#{u.mugshot_temp}")
u.mugshot.styles.each do |s|
style_name = s[0]
FileUtils.mkpath("public/system/user/mugshot/#{u.id}/#{style_name}")
FileUtils.mv("public/system/user/mugshot/#{u.id}/#{style_name}_#{u.mugshot_temp}", "public/system/user/mugshot/#{u.id}/#{style_name}/#{u.mugshot_temp}")
end
end
end
rename_column :users, :mugshot_temp, :mugshot
end
end
@botandrose
Copy link

I think you mean || not && on line 14. Thanks for this!

@osnysantos
Copy link

What about change content_type to:

MIME::Types.type_for(u.old_file_name).first.content_type ?

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