Skip to content

Instantly share code, notes, and snippets.

@shadowmaru
Created April 12, 2010 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadowmaru/363894 to your computer and use it in GitHub Desktop.
Save shadowmaru/363894 to your computer and use it in GitHub Desktop.
class MigrateProductsToPaperclip < ActiveRecord::Migration
def self.up
# Rename the old "image" column to "old_file_name", since Product.image will now try to do Paperclip stuff
rename_column :products, :image, :old_file_name
# Rename asset directories to pluralize
File.rename("public/system/product","public/system/products")
File.rename("public/system/products/image","public/system/products/images")
Product.all.each do |p|
unless p.old_file_name.blank?
# If an image was set by file_column, copy the relevant parameters to the new Paperclip columns
p.update_attributes(
:image_file_name => p.old_file_name,
:image_content_type => 'image/jpeg',
:image_updated_at => Time.now.utc,
:image_file_size => File.stat("public/system/products/images/#{p.id}/#{p.old_file_name}").size)
# Now rename the image file to add the original_ prefix
File.rename("public/system/products/images/#{p.id}/#{p.old_file_name}","public/system/products/images/#{p.id}/original_#{p.old_file_name}")
end
end
# And finally remove the old_file_name column, which we don't need any more
remove_column :products, :old_file_name
end
def self.down
# laziness, impatience, hubris
raise IrreversibleMigration
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment