VichUploaderBundle icon indicating copy to clipboard operation
VichUploaderBundle copied to clipboard

Cleanup command

Open Nemo64 opened this issue 4 years ago • 4 comments

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

In a relation database it is quiet easy to implicitly delete a row throw foreign key constraints without doctrine noticing.

I want to propose a cleanup command that goes through all mapped entities and all existing files and tries to delete all files that are orphaned.

I could implement this for doctrine orm but I'm not fluent in the other storage engines.

I also see that this could become challenging if the amount of files goes above ~65.535 and even before that, it probably should work in chunks to avoid loading everything into ram.

Nemo64 avatar May 03 '20 11:05 Nemo64

I think that getting orphaned Doctrine objects does not belong here. You should execute such task elsewhere, while deleting a mapped object should already delete related file.

garak avatar May 03 '20 13:05 garak

Maybe I described it the wrong way.

I'm not talking about orphaned doctrine objects, I'm talking about orphaned files. If you have a relation defined with @JoinColumn(onDelete="CASCADE"). then the database can delete rows without doctrine knowing about it and therefore without this bundle deleting the corresponding files. And there might also be external sources deleting rows, maybe even accidents or manual cleanup. That's why I would like an easy way to check if the the database and the filesystem are in sync.

Nemo64 avatar May 03 '20 13:05 Nemo64

Oh well, in that case it looks fine to me. Waiting for your PR

garak avatar May 03 '20 14:05 garak

Doctrine has cascade={'remove'} which cascades on the Doctrine level instead on the DB level: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations

Might still make sense have a command to check data consistency though.

UlrichThomasGabor avatar Sep 05 '22 09:09 UlrichThomasGabor