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

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Oct 29, 2024

Fix PHPLIB-1568

GridFS\Bucket::deleteByName($filename)

Use the CollectionWrapper::deleteFileAndChunksByFilename() introduced for the need of the StreamWrapper::unlink().

Implementation:

  1. find file ids by file name
db.files.find({filename: <filename>}, {projection: {_id: 1}})
  1. delete files by ids
db.files.deleteMany({_id: {$in: <ids>}})
  1. delete chunks by file ids
db.chunks.deleteMany({files_id: {$in: <ids>}})

If a DB error occurs during 1, nothing happened.
If a DB error occurs during 2 or 3, some chunks may become orphan and we cannot retry as the mapping chunks-id-to-file-name is missing.

There could be an issue if the list of ids is too big. In that case, we would have to work by batches.

GridFS\Bucket::renameByName($filename)

Use the CollectionWrapper::updateFilenameForFilename() introduced for the need of StreamWrapper::rename()

Implementation:

  1. update files with the file name, to set the new filename
db.files.updateMany({filename: <filename>}, {$set: {filename: <newFilename>}})

@GromNaN GromNaN marked this pull request as ready for review October 29, 2024 16:26
@GromNaN GromNaN requested a review from a team as a code owner October 29, 2024 16:26
@GromNaN GromNaN requested review from alcaeus and jmikola October 29, 2024 16:26
@@ -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.

['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

Comment on lines -153 to -154
$filename = $filename;
$revision = $revision;
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.

@GromNaN GromNaN changed the title PHPLIB-1568 Add GridFS\Bucket::deleteByName(filename) PHPLIB-1568 Add GridFS\Bucket::deleteByName(filename) and renameByName(filename, newFilename) Oct 29, 2024
Comment on lines -153 to -154
$filename = $filename;
$revision = $revision;
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.

@@ -160,6 +160,41 @@ 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

@GromNaN GromNaN force-pushed the PHPLIB-1568 branch 3 times, most recently from b9d5dc0 to a955d57 Compare October 30, 2024 14:14
@GromNaN GromNaN enabled auto-merge (squash) October 31, 2024 17:43
@GromNaN GromNaN merged commit 7781e75 into mongodb:v1.x Oct 31, 2024
31 checks passed
@GromNaN GromNaN deleted the PHPLIB-1568 branch October 31, 2024 19:36
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.

2 participants