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

Persist deduplicated filename with ActiveRecord #2677

Closed

Conversation

krasnoukhov
Copy link

@krasnoukhov krasnoukhov commented Jul 24, 2023

I noticed an issue that when using ActiveRecord and trying to update the mounted file with a different file of the same name it is deduplicated logically but that change is not persisted, resulting in current_path or url pointing to the "old" filename which does not exist (cause it was deleted). So deduplication works but it's not persisted. There was a spec for this case but in spec record was not reloaded (dirty) so it passed. Spec failure before code changes:

Failures:

  1) CarrierWave::ActiveRecord#mount_uploader removing old files normally should give a different name to new file and remove the old file
     Failure/Error: expect(@event.image.current_path).to eq public_path('uploads/old(2).jpeg')
     
       expected: "/Users/krasnoukhov/Projects/Simplepractice/carrierwave/spec/public/uploads/old(2).jpeg"
            got: "/Users/krasnoukhov/Projects/Simplepractice/carrierwave/spec/public/uploads/old.jpeg"
     
       (compared using ==)
     # ./spec/orm/activerecord_spec.rb:779:in `block (4 levels) in <top (required)>'

Changing to before_save makes write_identifier also kick in before record is persisted which makes issue go away. Not sure if before_save can have other complications, but it seems like it's working... The alternative would be to move this deduplicate call to happen before save:

uploader.deduplicate(existing_paths)

Another alternative would be to perform another save after store.

@krasnoukhov
Copy link
Author

I tried this in our app and it does not seem to be a solution since we rely on model.id for filename so store must happen after save...

@krasnoukhov
Copy link
Author

Ok I guess there's no way to call deduplicate at the other time. We need to call it at the store point because this is when everything is ready to check for duplication. But yes, deduplication can result in dirty value which needs to be persisted. So I've added a persistence mechanism and AR integration. Let me know your thoughts.

@mshibuya
Copy link
Member

You're totally right, I'm really sorry that one of key features in the release 3.0 is completely broken.

But the #persist_uploader idea doesn't seem to be ideal, as it will incur an additional RDB access. Let me try to think about other solutions.

@krasnoukhov
Copy link
Author

Closing in favor of #2679

@krasnoukhov krasnoukhov deleted the fix-dedup branch July 31, 2023 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants