shrinerb / shrine

File Attachment toolkit for Ruby applications

Home Page:https://shrinerb.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug] File not promoted on permanent storage if reload the model inside a transaction

jonathantribouharet opened this issue · comments

Brief Description

Hello,
Inside a transaction, if a create a model then reload it with the find method, the file is not promoted on permanent storage (store) and stay in cache.
It doesn't happen if I call reload on the model or without the transaction.

This bug doesn't seems to be related with #366

Expected behavior

The storage should be store.

Actual behavior

The storage is cache.

Simplest self-contained example code to demonstrate issue

require 'active_record'
require 'shrine'
require 'shrine/storage/file_system'
require 'tmpdir'
require 'down'

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new(File.join(Dir.tmpdir, 'cache')),
  store: Shrine::Storage::FileSystem.new(File.join(Dir.tmpdir, 'uploads'))
}

Shrine.plugin :determine_mime_type
Shrine.plugin :activerecord

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.connection.create_table(:products) { |t| t.json :image_data }

class Product < ActiveRecord::Base
  include Shrine::Attachment.new(:image)
end

product = nil

ActiveRecord::Base.transaction do
  product = Product.create!
  product = Product.find(product.id) # without this line or without the transaction it works

  params = {
    image: Down.download('https://dummyimage.com/600x400/000/fff')
  }

  product.update!(params)
end

puts product.image.storage_key # return `cache` instead of `store`

System configuration

Ruby: 3.2.2
Shrine: 3.5.0
Ruby On Rails: 7.0.7.2