shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Migration guide from ActiveStorage to Shrine

rept opened this issue · comments

Hi,

In the documentation guides I can find information about migrating from CarrierWave or Paperclip, but I don't see any information about migrating from ActiveStorage to Shrine.

Is there any information available? Have people done this and is there a way to keep the existing files (images) in S3 and query the data over to Shrine?

There definitely ought to be a way to migrate keeping S3 locations the same, because shrine stores individual key paths for each object.

I'm not sure if anyone has done it yet though to mark off all the parts, and I don't think any documentation exists. Shrine actually pre-dates ActiveStorage, so at the time those other docs were written ActiveStorage might not yet have been a concern. I agree this would be very useful. To spend some time figuring out the best way to migrate from AS, and then documenting it.

I don't see too many complications. One could simply map the following columns into a file/image/avatar_data column, and perhaps update all active record calls to Shrine method calls. Not sure the best way to do this though.

create_table "active_storage_blobs", force: :cascade do |t|
    t.string "key", null: false
    t.string "filename", null: false
    t.string "content_type"
    t.text "metadata"
    t.bigint "byte_size", null: false
    t.string "checksum", null: false
    t.datetime "created_at", null: false
    t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
  end

I recently did this and it was pretty simple. I created the database column and set shrine up as per documentation. I also have the ActiveStorage properly set up as well. I then created the following instance method in my model:

def migrate_image_from_active_storage
  if self.image.attached?
    self.image.open do |i|
      self.article_image = File.open(i)
      self.save
     end
   end
end

You could execute this from an individual instance or loop through the entire database and execute on each instance. Later you can then remove ActiveStorage. Maybe not the best method but it worked for me with 1600+ records pretty quickly. However, this is not an issue with the gem and should better be directed to the discourse group or maybe stackoverflow.

I agree with @jaredjackson, so I'm closing this one. I'd gladly accept a pull request that adds an Active Storage guide.