Skip to content

PHPLIB-1568 Add GridFS\Bucket::deleteByName(filename) and renameByName(filename, newFilename) #1504

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

Merged
merged 5 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,20 @@
</MixedArgumentTypeCoercion>
</file>
<file src="src/GridFS/CollectionWrapper.php">
<InvalidNullableReturnType>
<code><![CDATA[int]]></code>
<code><![CDATA[int]]></code>
</InvalidNullableReturnType>
<MixedAssignment>
<code><![CDATA[$ids[]]]></code>
</MixedAssignment>
<NullableReturnStatement>
<code><![CDATA[$count]]></code>
<code><![CDATA[$this->filesCollection->updateMany(
['filename' => $filename],
['$set' => ['filename' => $newFilename]],
)->getMatchedCount()]]></code>
</NullableReturnStatement>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boths additions will be removed in v2.x, thanks to #1454

</file>
<file src="src/GridFS/ReadableStream.php">
<MixedArgument>
Expand Down
35 changes: 35 additions & 0 deletions src/GridFS/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,23 @@ public function delete(mixed $id)
}
}

/**
* Delete all the revisions of a file name from the GridFS bucket.
*
* @param string $filename Filename
*
* @throws FileNotFoundException if no file could be selected
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function deleteByName(string $filename): void
{
$count = $this->collectionWrapper->deleteFileAndChunksByFilename($filename);

if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}

/**
* Writes the contents of a GridFS file to a writable stream.
*
Expand Down Expand Up @@ -648,6 +665,24 @@ public function rename(mixed $id, string $newFilename)
}
}

/**
* Renames all the revisions of a file name in the GridFS bucket.
*
* @param string $filename Filename
* @param string $newFilename New filename
*
* @throws FileNotFoundException if no file could be selected
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function renameByName(string $filename, string $newFilename): void
{
$count = $this->collectionWrapper->updateFilenameForFilename($filename, $newFilename);

if ($count === 0) {
throw FileNotFoundException::byFilename($filename);
}
}

/**
* Writes the contents of a readable stream to a GridFS file.
*
Expand Down
7 changes: 2 additions & 5 deletions src/GridFS/CollectionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function deleteChunksByFilesId(mixed $id): void
/**
* Delete all GridFS files and chunks for a given filename.
*/
public function deleteFileAndChunksByFilename(string $filename): ?int
public function deleteFileAndChunksByFilename(string $filename): int
{
/** @var iterable<array{_id: mixed}> $files */
$files = $this->findFiles(['filename' => $filename], [
Expand Down Expand Up @@ -150,9 +150,6 @@ public function findChunksByFileId(mixed $id, int $fromChunk = 0)
*/
public function findFileByFilenameAndRevision(string $filename, int $revision): ?object
{
$filename = $filename;
$revision = $revision;
Comment on lines -153 to -154
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I had already removed that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted that this remained when 1b29a6d introduced type hints on the method.


if ($revision < 0) {
$skip = abs($revision) - 1;
$sortOrder = -1;
Expand Down Expand Up @@ -266,7 +263,7 @@ public function insertFile(array|object $file): void
/**
* Updates the filename field in the file document for all the files with a given filename.
*/
public function updateFilenameForFilename(string $filename, string $newFilename): ?int
public function updateFilenameForFilename(string $filename, string $newFilename): int
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class is internal, we can change the return type. This is always an int, an exception is thrown in case of error.

{
return $this->filesCollection->updateMany(
['filename' => $filename],
Expand Down
46 changes: 46 additions & 0 deletions tests/GridFS/BucketFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ public function testDeleteStillRemovesChunksIfFileDoesNotExist($input, $expected
$this->assertCollectionCount($this->chunksCollection, 0);
}

public function testDeleteByName(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this Pr block on spec changes for DRIVERS-2807 and DRIVERS-2808? Also, will the PR(s) for those tickets include spec tests to be synced?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spec tests added in mongodb/specifications#1702

{
$this->bucket->uploadFromStream('filename', self::createStream('foobar1'));
$this->bucket->uploadFromStream('filename', self::createStream('foobar2'));
$this->bucket->uploadFromStream('filename', self::createStream('foobar3'));

$this->bucket->uploadFromStream('other', self::createStream('foobar'));

$this->assertCollectionCount($this->filesCollection, 4);
$this->assertCollectionCount($this->chunksCollection, 4);

$this->bucket->deleteByName('filename');

$this->assertCollectionCount($this->filesCollection, 1);
$this->assertCollectionCount($this->chunksCollection, 1);

$this->bucket->deleteByName('other');

$this->assertCollectionCount($this->filesCollection, 0);
$this->assertCollectionCount($this->chunksCollection, 0);
}

public function testDeleteByNameShouldRequireFileToExist(): void
{
$this->expectException(FileNotFoundException::class);
$this->bucket->deleteByName('nonexistent-name');
}

public function testDownloadingFileWithMissingChunk(): void
{
$id = $this->bucket->uploadFromStream('filename', self::createStream('foobar'));
Expand Down Expand Up @@ -723,6 +751,24 @@ public function testRenameShouldRequireFileToExist(): void
$this->bucket->rename('nonexistent-id', 'b');
}

public function testRenameByName(): void
{
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
$this->bucket->uploadFromStream('filename', self::createStream('foo'));
$this->bucket->uploadFromStream('filename', self::createStream('foo'));

$this->bucket->renameByName('filename', 'newname');

$this->assertNull($this->bucket->findOne(['filename' => 'filename']), 'No file has the old name');
$this->assertStreamContents('foo', $this->bucket->openDownloadStreamByName('newname'));
}

public function testRenameByNameShouldRequireFileToExist(): void
{
$this->expectException(FileNotFoundException::class);
$this->bucket->renameByName('nonexistent-name', 'b');
}

public function testUploadFromStream(): void
{
$options = [
Expand Down
14 changes: 14 additions & 0 deletions tests/UnifiedSpecTests/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ private function executeForBucket(Bucket $bucket)

return $bucket->delete($args['id']);

case 'deleteByName':
assertArrayHasKey('filename', $args);
assertIsString($args['filename']);

return $bucket->deleteByName($args['filename']);

case 'downloadByName':
assertArrayHasKey('filename', $args);
assertIsString($args['filename']);
Expand All @@ -812,6 +818,14 @@ private function executeForBucket(Bucket $bucket)

return null;

case 'renameByName':
assertArrayHasKey('filename', $args);
assertArrayHasKey('newFilename', $args);
assertIsString($args['filename']);
assertIsString($args['newFilename']);

return $bucket->renameByName($args['filename'], $args['newFilename']);

case 'uploadWithId':
assertArrayHasKey('id', $args);
$args['_id'] = $args['id'];
Expand Down
2 changes: 2 additions & 0 deletions tests/UnifiedSpecTests/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,11 @@ final class Util
],
Bucket::class => [
'delete' => ['id'],
'deleteByName' => ['filename'],
'downloadByName' => ['filename', 'revision'],
'download' => ['id'],
'rename' => ['id', 'newFilename'],
'renameByName' => ['filename', 'newFilename'],
'uploadWithId' => ['id', 'filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
'upload' => ['filename', 'source', 'chunkSizeBytes', 'disableMD5', 'contentType', 'metadata'],
],
Expand Down