Skip to content

PHPLIB-518: Provide MongoDB\Collection::rename() method to rename a collection #840

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 23 commits into from
Jul 26, 2021

Conversation

tanlisu
Copy link
Contributor

@tanlisu tanlisu commented Jul 7, 2021

@tanlisu tanlisu marked this pull request as ready for review July 9, 2021 16:02

.. code-block:: php

function renameCollection($fromNamespace, $toNamespace, array $options = []): array|object
Copy link
Member

@jmikola jmikola Jul 9, 2021

Choose a reason for hiding this comment

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

A few suggestions:

  • $fromNamespace can be $collectionName. Since we already have the context of a database, it makes sense that only the collection name would be dynamic. Omitting a "from" prefix will allow us to re-use the same include as dropCollection()'s docs.
  • $toNamespace can be $toCollectionNameOrNamespace. I'd propose we accept either as a convenience. If the argument doesn't include a dot, we can prefix the value with the current database name and a dot before passing it along to the RenameCollection operation. Docs for this param can be the same as the Collection::rename() suggestion I shared above.

This handles the general use case where someone may want to rename collections within the same database.

Copy link
Contributor Author

@tanlisu tanlisu Jul 12, 2021

Choose a reason for hiding this comment

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

@jmikola I don't think it works to determine whether a collection name or namespace is being provided by checking whether the argument contains a dot, since the collection names used in the tests contain a dot as well. So I'm not sure how it would be possible to do this actually - any ideas?

Copy link
Member

Choose a reason for hiding this comment

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

Very good point. This would work the other way around (e.g. $databaseNameOrNamespace) because database names cannot contain dots; however, it's ambiguous if we're trying to differentiate a namespace from a collection name.

In that case, I think it makes sense to leave $toNamespace as-is.

$fromNamespace can still be changed to $collectionName for consistency with Database::dropCollection(), and the helper method can build the namespace to send to the Operation internally.

Copy link
Member

Choose a reason for hiding this comment

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

To add on to this, let's change the API of both the helpers and Operation class to no longer take namespace strings as paramaters:

$database->renameCollection($collectionName, $toDatabaseName, $toCollectionName);
$collection->rename($toDatabaseName, $toCollectionName);

The Operation class will then have four required parameters:

  • fromDatabaseName
  • fromCollectionName
  • toDatabaseName
  • toCollectionName

It can then build the namespace internally with string concatenation in the constructor (allowing you to keep the internal private properties that store namespaces).

For the Database helper, fromDatabaseName would be pulled from the database instance's own name. And for the Collection helper, both "from" parameters would be pulled from the collection instance.

@tanlisu tanlisu force-pushed the phplib-518 branch 2 times, most recently from cb6947a to 9fcabfa Compare July 14, 2021 18:17
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Reviewed the code files but skipped the docs on this round. I'll come back to those on Friday and do a local build to check for any RST errors.

tanlisu and others added 2 commits July 21, 2021 11:07
Co-authored-by: Jeremy Mikola <jmikola@gmail.com>

Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Built the documentation locally and didn't see any syntax errors. Left one suggestion to reword, and then some other suggestions for outstanding comments on code docs.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

Tests appear to be passing, which confirms that the earlier issue was unrelated.

LGTM after a few doc block suggestions.

@jmikola
Copy link
Member

jmikola commented Jul 26, 2021

When squashing and merging this, I'd suggest the following description.

PHPLIB-518: RenameCollection operation

Adds Database::renameCollection() and Collection::rename() methods.

Move assertCollectionDoesNotExist() and assertCollectionExists() to FunctionalTestCase

Most of the other commit messages shouldn't be relevant, and this summarizes the actual changes we might want to look back on down the line. You can also rename the PR itself to "PHPLIB-518: RenameCollection operation", which should suggest that title when squashing.

Co-authored-by: Jeremy Mikola <jmikola@gmail.com>
@tanlisu tanlisu merged commit 722679f into mongodb:master Jul 26, 2021
@tanlisu tanlisu deleted the phplib-518 branch July 26, 2021 19:12
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.

3 participants