Skip to content

New rake task for changing attachment options #1119

wants to merge 4 commits into from

2 participants

cjc25 commented Dec 29, 2012

I found myself facing the not-uncommon problem of wanting to move my paperclip attachments to Amazon S3, as well as the perhaps less common problem of needing to change the hash_secret. After some Googling without finding anything that felt quite right, I tried to make a solution flexible enough for any kind of option change. It's nevertheless wise not to accept new uploads while making this kind of migration!

The new rake task paperclip:change_options takes the same environment variables as the refresh functions: CLASS and an optional ATTACHMENT. It reads the options in config/new_paperclip_options.yml as an ERB YAML file, just like database.yml, and merges them onto the attachment's existing options. A temporary attachment instance is then created and saved with the modified options, but the original attachment and model are unaffected.

Once the task has been run, you can update the actual has_attached_file options to match new_paperclip_options.yml. After deploying, paperclip will find the new files instead. In case of typos in the options or meteor strike, reversion is also painless since the original files are not affected at all.

I did not see any tests for the other rake tasks, so I wasn't sure what to do to test this, but I'd be happy to add a commit with specs if you can point me in the right direction. I did migrate my own app's user content to S3 with a new hash_secret using this without any trouble.

cjc25 added some commits Nov 28, 2012
@cjc25 cjc25 Create new rake task for changing file options.
Create the rake task paperclip:change_options. This task is invoked the same way
as the refresh tasks (rake CLASS=... paperclip:change_options). It reads a file
config/new_paperclip_options.yml as an ERB YAML file and saves new attachments
for CLASS, including all styles, using the merge of the new options onto the
original options. Existing attachments and models are unaffected.

The purpose of this task is to move existing files to the location/format/styles
needed for a planned change to attachments. You can copy existing files to a new
location using this rake task, then deploy a release which sets the new options
in the model's has_attached_file, and the copied files will be found by

Using the task also simplifies changing hash_secrets.
@cjc25 cjc25 Clean up formatting in paperclip:change_options 9e7b44d
@cjc25 cjc25 Fix code style to match other rake tasks 3ed1efc
@cjc25 cjc25 Do not affect :hash paths when changing options.
:updated_at was changed when saving the files to a new location, so by default
paths including :hash would be changed.
djcp commented Feb 22, 2013

Thanks, it's a great idea but I'm loath to merge untested code. If you can figure out a way to test this - perhaps by extracting to a class and writing unit tests - we'd be more likely to merge. For now, though, it might just make sense to have this live in a gist somewhere, extracted into a standalone rake task.

@djcp djcp closed this Feb 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.