-
Notifications
You must be signed in to change notification settings - Fork 79
[java-shell] Fix createIndexes. Core did not pass dbOptions as a last argument #446
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
[java-shell] Fix createIndexes. Core did not pass dbOptions as a last argument #446
Conversation
packages/shell-api/src/collection.ts
Outdated
|
||
const spec = { ...this._database._baseOptions, ...options, key: keys }; | ||
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec]); | ||
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], this._database._baseOptions); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aherlihy Should this be
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], this._database._baseOptions); | |
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], options, this._database._baseOptions); |
? Or should the options really be passed as part of the index spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like node.js driver expects only session
in options
and other properties in spec.
So maybe more refactoring and unification is required in core
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely add more docs/tests since this is confusing, but there is a bug in the original code. That being said Ludmilla's more correct for this one, although we need to tweak the entire method a bit (which can happen in a different ticket).
The shell API allows users to pass index-specific options via the options
argument, which are described here: https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/index.html#options-for-all-index-types
The node driver it seems, can take those indexes-specific options as part of the index spec or as a second argument. Right now it looks like we are adding it to the spec (which is why the last two args of ServiceProvider.createIndexes
were skipped).
But the session option cannot be set via the index spec and instead needs to be sent via options
, so we had a bug where we were sending the session
option via the index spec which would get ignored.
Note there are no DatabaseOptions
to be sent at all so the last argument to service provider.createIndexes
should still be empty or {}. (this._database._baseOptions
are command options for all commands associated with that database, and not actual DatabaseOptions
).
I think a better alternative for everyone is to try separating out the options from the index spec. So the method would look like:
@returnsPromise
async createIndex(
keys: Document,
options: Document = {}
): Promise<Document> {
assertArgsDefined(keys);
if (typeof options !== 'object' || Array.isArray(options)) {
throw new MongoshInvalidInputError('The "options" argument must be an object.');
}
this._emitCollectionApiCall('createIndex', { keys, options });
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [{ key: keys }], { ...this._database._baseOptions, ...options});
}
but this change should come with tests and such so I can make it separately: https://jira.mongodb.org/browse/MONGOSH-478
Sorry for the super long response!
… avoid fails on different MongoDB versions
7da439f
to
76c821d
Compare
@addaleax, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think at the very least this won’t break anything, so it should be fine :)
I cannot see why tests failed because I don't have mongodb account |
This is from the |
I fixed the test |
Note that it contains a small change in core code in
collection.ts
.dbOptions
there were not passed as last argument toserviceProvider.createIndexes
that causedArity error - expected: 3 actual: 4