mul53 / activestorage-ipfs

This gem extends the ActiveStorage::Service with an implementation for IPFS
MIT License
19 stars 5 forks source link

Attachment unique constraints fail while uploading duplicate file #10

Open fauno opened 3 years ago

fauno commented 3 years ago

Hi, I'm very interested in using this gem and probably send patches. So far I've found this:

class Site < ActiveRecord::Base
  has_many_attached :files
end

Attaching a file twice throws a constraint error:

site = Site.create
site.files.attach io: File.open('/tmp/somefile.jpg'), filename: 'somefile.jpg')
site.files.attach io: File.open('/tmp/somefile.jpg'), filename: 'somefile.jpg')
=> ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: active_storage_attachments.record_type, active_storage_attachments.record_id, active_storage_attachments.name, active_storage_attachments.blob_id

Should IpfsService catch duplicate Attachments and remove them? Also getting the last Attachment without a query feels weird, wouldn't it be better to change it to something like Attachment.find_by(blob: new_blob)?

I'll prepare a PR after discussing, thanks!

andrew commented 3 years ago

I ran into a similar problem last summer: https://github.com/mul53/activestorage-ipfs/issues/6

fauno commented 3 years ago

I ran into a similar problem last summer: #6

Thanks! I saw it but this one happens after that, when the Attachment gets the existing Blob and just before discarding the new one