Skip to content
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

PHPLIB-1143: Add search index operations to Collection class #1097

Merged
merged 5 commits into from
Aug 31, 2023

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jun 8, 2023

Fix PHPLIB-1143

Spec files sync from mongodb/specifications@267a54d
Previously introduced by mongodb/specifications@ae35908

  • Create methods createSearchIndex, createSearchIndexes, dropSearchIndex, updateSearchIndex, listSearchIndexes
  • Create operation classes
  • Add unit tests on new Operation classes
  • Add functional tests for error using unified test json.
  • Add functional test that requires a MONGODB_URI variable on Atlas. Currently only M10+ are supported.

src/Collection.php Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Operation/DropIndexes.php Show resolved Hide resolved
src/Operation/DropSearchIndex.php Outdated Show resolved Hide resolved
src/Operation/ListSearchIndexes.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPLIB-1143 branch 4 times, most recently from 233c594 to 964e7fc Compare June 19, 2023 23:33
Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

I think I've figured out how spec works: all the Atlas Search index command get a server error because they are not available on local servers. The spec checks the command that is sent to the server with expectedEvents. Therefore, I don't validate some inputs client-side to let the server return an error. This is more future-proof as some options that are invalid in 7.0 could be allowed in a future release.

src/Collection.php Outdated Show resolved Hide resolved
src/Operation/ListSearchIndexes.php Outdated Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from alcaeus June 20, 2023 10:14
@GromNaN GromNaN marked this pull request as ready for review June 20, 2023 10:15
src/Operation/CreateSearchIndexes.php Outdated Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
tests/Operation/BulkWriteTest.php Show resolved Hide resolved
tests/Operation/DropSearchIndexTest.php Outdated Show resolved Hide resolved
tests/Operation/ListSearchIndexesTest.php Outdated Show resolved Hide resolved
tests/Operation/UpdateSearchIndexTest.php Outdated Show resolved Hide resolved
tests/UnifiedSpecTests/Util.php Show resolved Hide resolved
Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Thank you @jmikola for the huge review. I made a lot of changes according to your comments.

src/Operation/ListSearchIndexes.php Outdated Show resolved Hide resolved
src/Operation/ListSearchIndexes.php Outdated Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Model/SearchIndexInput.php Outdated Show resolved Hide resolved
tests/Collection/SearchIndexFunctionalTest.php Outdated Show resolved Hide resolved
tests/Collection/SearchIndexFunctionalTest.php Outdated Show resolved Hide resolved
tests/Operation/BulkWriteTest.php Show resolved Hide resolved
tests/Operation/ListSearchIndexesTest.php Outdated Show resolved Hide resolved
tests/UnifiedSpecTests/Util.php Show resolved Hide resolved
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
* @see ListSearchIndexes::__construct() for supported aggregation and list options
*/
public function listSearchIndexes(?string $name = null, array $options = []): Iterator
Copy link
Member

Choose a reason for hiding this comment

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

How do you feel about folding name into $options to simplify this API?

Surveying existing API methods, there are only a few cases where we explicitly list optional parameters in the signature:

  • Query filter for read operations (e.g. countDocuments(), find())
  • Pipeline for change steams (watch())
  • Database name for rename()

Filters and the pipeline are commonly provided to their operations, and the database name was an interesting case that we discussed at length in #840.

The name option seems more comparable to the filter option on Database::listCollections.

Long term, I think we'd probably want to have a conversation about migrating from options arrays to named parameters. That's something to consider in 2.0 when we'll surely require PHP 8+. For now, I'd want to be conservative about what optional parameters showing up outside of our existing options arrays.

/cc @alcaeus in case you have an opinion here

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I've harmonized Collection::listSearchIndexes($options) with Database::listCollections($options) and Collection::listIndexes($options).

Do you want to report this signature change to the spec owner?

Copy link
Member

Choose a reason for hiding this comment

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

Do you want to report this signature change to the spec owner?

I think it makes sense to follow up with the spec authors about this. The current signature in the spec is:

listSearchIndexes(name: Optional<String>, aggregationOptions: Optional<AggregationOptions>, listIndexOptions: Optional<ListSearchIndexOptions>): Cursor<Document>;

ListSearchIndexOptions isn't defined in the spec, but I see no reason for name to be broken out as a positional argument. If the database enumeration spec is relevant here (see: Filters), I should mention that there's no signature defined in that spec. It just loosely talks about various options for the listDatabases command.

Having said that, moving $name to $options in PHPLIB should not require any changes in the spec. The specs are seldom prescriptive about how to solicit options, so the only important bit here is that name is still an option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Discussed with the spec author: mongodb/specifications#1442 (comment)

src/Operation/CreateSearchIndexes.php Show resolved Hide resolved
src/Operation/DropSearchIndex.php Show resolved Hide resolved
src/Operation/ListSearchIndexes.php Show resolved Hide resolved
tests/Collection/SearchIndexFunctionalTest.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPLIB-1143 branch 3 times, most recently from 6500e8f to 4ccc852 Compare June 27, 2023 15:40
@GromNaN
Copy link
Member Author

GromNaN commented Jun 27, 2023

@GromNaN GromNaN requested a review from jmikola June 27, 2023 16:34
@alcaeus alcaeus marked this pull request as draft July 6, 2023 13:45
@GromNaN GromNaN marked this pull request as ready for review July 7, 2023 08:58
@GromNaN GromNaN force-pushed the PHPLIB-1143 branch 2 times, most recently from 8e5e678 to a14eaf1 Compare July 18, 2023 14:29
Copy link
Member Author

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Implemented e2e tests from mongodb/specifications#1442

src/Collection.php Outdated Show resolved Hide resolved
@GromNaN GromNaN force-pushed the PHPLIB-1143 branch 3 times, most recently from 0d3d54a to 9b6d5a1 Compare July 24, 2023 15:00
@GromNaN
Copy link
Member Author

GromNaN commented Aug 18, 2023

mongodb/specifications#1442 have been merged and the spec tests files are up to date.

Last thing is to run tests in CI against Atlas PHPLIB-1168.

@GromNaN GromNaN force-pushed the PHPLIB-1143 branch 4 times, most recently from b779ebf to 5b64391 Compare August 18, 2023 16:12
tests/UnifiedSpecTests/UnifiedSpecTest.php Outdated Show resolved Hide resolved
tests/UnifiedSpecTests/UnifiedSpecTest.php Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Outdated Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Outdated Show resolved Hide resolved
src/Operation/CreateSearchIndexes.php Outdated Show resolved Hide resolved
src/Model/SearchIndexInput.php Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
src/Collection.php Show resolved Hide resolved
src/Operation/DropSearchIndex.php Show resolved Hide resolved
src/Operation/DropSearchIndex.php Outdated Show resolved Hide resolved
src/Operation/UpdateSearchIndex.php Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Outdated Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Show resolved Hide resolved
tests/SpecTests/SearchIndexSpecTest.php Outdated Show resolved Hide resolved
tests/UnifiedSpecTests/Operation.php Outdated Show resolved Hide resolved
src/Collection.php Outdated Show resolved Hide resolved
@GromNaN GromNaN requested a review from jmikola August 31, 2023 13:00
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.

I suggest reverting the introduction of assertDocumentsMatch to the base FunctionalTestCast class and sticking with one of the existing BSON assertions in the base TestCase class.

LGTM after that.


public function provideInvalidIndexSpecificationTypes(): array
{
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return $this->wrapValuesForDataProvider($this->getInvalidDocumentValues());
return $this->wrapValuesForDataProvider($this->getInvalidArrayValues());

I think this can be updated now that you're using is_array() to validate the list elements. The above code is consistent with provideInvalidIndexSpecificationTypes() in CreateIndexesTest.php.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.


$this->assertCount(1, $indexes);
$this->assertSame($name, $indexes[0]->name);
$this->assertDocumentsMatch($mapping, $indexes[0]->latestDefinition);
Copy link
Member

Choose a reason for hiding this comment

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

Apologies for not specifying, but I was suggesting assertMatchesDocument() or assertSameDocument() from the base TestCase class instead of comparing JSON strings here.

assertDocumentsMatch() is unique to the legacy spec test runner (note that its DocumentsMatchConstraint class differs from the unified test runner's Matches constraint), and I'd be hesitant to move the legacy test runner logic into the main test suite, as the ultimate goal is to delete all of that code once each spec can be ported over to the unified format.

Is it feasible to use one of the two existing TestCase methods here? assertMatchesDocument() allows extra fields in the actual document, while assertSameDocument() does not -- so you can decide which is most appropriate. If so, you can then revert the introduction of DocumentsMatchConstraint to FunctionalTestCase.

Copy link
Member Author

Choose a reason for hiding this comment

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

Reverted 6df4fd7

* @param array|object $expectedDocument
* @param array|object $actualDocument
*/
public static function assertDocumentsMatch($expectedDocument, $actualDocument, string $message = ''): void
Copy link
Member

Choose a reason for hiding this comment

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

I explained in another thread why I'd rather not introduce legacy spec test classes into the main test suite, but even if this was to stay this would be a method to add on the base TestCase class since it doesn't depend on a server connection.

The base TestCase is also where the existing BSON assertions are.

@GromNaN GromNaN merged commit f6ce211 into mongodb:master Aug 31, 2023
12 checks passed
@GromNaN GromNaN deleted the PHPLIB-1143 branch August 31, 2023 17:38
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