Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Location docs suggest using unique immutable storage location #705

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions doc/changing_location.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: changing-location
title: Migrating File Locations
---

This guide shows how to migrate the location of uploaded files on the same
This guide shows how to migrate the location of uploaded files on the same
storage in production, with zero downtime.

Let's assume we have a `Photo` model with an `image` file attachment:
Expand Down Expand Up @@ -31,14 +31,26 @@ to work with the previously stored urls because the files have not been migrated
```rb
class ImageUploader < Shrine
def generate_location(io, **options)
# change location generation
# change location generation, eg....
[
options[:record] && options[:record].class.name.underscore,
option[:record] && options[:record].id,
super
].compact.join("/")
end
end
```

We can now deploy this change to production so new file uploads will be stored in
We can now deploy this change to production so new file uploads will be stored in
the new location.

As seen above, we can call `super` to get the include the default location, which uses ruby
`SecureRandom.hex` to have a unique immutable storage location. While it isn't
strictly required to have a unique immutable storage location, it makes many
things work smoother when different content will get a different storage location,
and is recommended. One approach is using fixed directory/prefix as above.


## 2. Move existing files

To move existing files to new location, run the following script. It fetches
Expand Down
Loading