Skip to content

Commit

Permalink
Ensure file deletion also deletes chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Jun 8, 2018
1 parent 2771e59 commit 9556321
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use MongoDB\Driver\Cursor;
use MongoDB\Driver\Exception\Exception as DriverException;
use MongoDB\Driver\Exception\WriteException;
use MongoDB\GridFS\Bucket;
use function array_combine;
use function array_fill;
use function array_intersect_key;
Expand Down Expand Up @@ -93,6 +94,9 @@ class DocumentPersister
*/
private $collection;

/** @var Bucket|null */
private $bucket;

/**
* Array of queued inserts for the persister to insert.
*
Expand Down Expand Up @@ -148,6 +152,12 @@ public function __construct(
$this->class = $class;
$this->collection = $dm->getDocumentCollection($class->name);
$this->cp = $this->uow->getCollectionPersister();

if (! $class->isFile) {
return;
}

$this->bucket = $dm->getDocumentBucket($class->name);
}

/**
Expand Down Expand Up @@ -451,6 +461,15 @@ public function update($document, array $options = [])
*/
public function delete($document, array $options = [])
{
if ($this->bucket instanceof Bucket) {
$documentIdentifier = $this->uow->getDocumentIdentifier($document);
$databaseIdentifier = $this->class->getDatabaseIdentifierValue($documentIdentifier);

$this->bucket->delete($databaseIdentifier);

return;
}

$query = $this->getQueryForDocument($document);

if ($this->class->isLockable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ public function testReadingFileAllowsUpdatingMetadata(): void
self::assertInstanceOf(User::class, $file->getMetadata()->getOwner());
}

public function testDeletingFileAlsoDropsChunks(): void
{
$file = $this->uploadFile(__FILE__);

$this->dm->remove($file);
$this->dm->flush();

$bucket = $this->dm->getDocumentBucket(File::class);

self::assertSame(0, $bucket->getFilesCollection()->count());
self::assertSame(0, $bucket->getChunksCollection()->count());
}

private function getRepository(): GridFSRepository
{
return $this->dm->getRepository(File::class);
Expand Down

0 comments on commit 9556321

Please sign in to comment.