Like so many people, when moving from Rails 2 to Rails 3, I found myself migrating from using FileColumn (i.e. file_column) to Paperclip, for image-upload handling in rails. I found an excellent migration guide written by Mark Cornick.
The general process is:
- Install Paperclip.
- Generate a migration to add the Paperclip-controlled image to your model. This adds a few Paperclip columns to the model’s table.
- Update your model code to use Paperclip.
- Update your views to use Paperclip’s method for writing the images, and Paperclip’s method for writing the fields for uploading the images.
- Write a migration that does the leg-work of moving your images from FileColumn to Paperclip.
- Uninstall FileColumn.
Step #5 is the most complex bit. There’s a few things that your migration has to do:
- Rename/restructure directories where FileColumn stored images to the new Paperclip way. e.g. directory names are singular in FileColumn, plural in Paperclip.
- Rename/move actual images to the new Paperclip-expected locations. e.g. different image styles (e.g. original, thumbnail, etc.) differ between FileColumn and Paperclip.
- Populate the new Paperclip table columns with data about the images.
Mark shared a Gist that showed did all these, except for a couple of issues: It didn’t handle varying image styles (e.g. original, thumbnail, etc.), and it wasn’t a reversible migration. Using Mark’s migration as a starting point, I added support for styles, and made it reversible*. You can see my fork as a Gist. I hope someone finds it useful.
* The “reversible” comes with caveats: I’ve tried to write it in such a way that it will recover from a failed forward migration, but testing rolling back from every possible point of failure would be onerous. It works reliably if the whole forward migration succeeds, and the resulting DB contents and file structure match the beginning point. Empty Paperclip directories are not deleted after rolling back. You should back-up your database before trying it.