Skip to content

Commit cf89434

Browse files
authored
[java-shell] Fix createIndexes. Core did not pass dbOptions as a last argument (#446)
* [java-shell] extract "name" property in result of createIndex test to avoid fails on different MongoDB versions * [java-shell] fix createIndexes. Unify usages of createIndexes method
1 parent 553392d commit cf89434

File tree

10 files changed

+25
-11
lines changed

10 files changed

+25
-11
lines changed

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/JavaServiceProvider.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,11 @@ internal class JavaServiceProvider(private val client: MongoClient,
537537
}
538538

539539
@HostAccess.Export
540-
override fun createIndexes(database: String, collection: String, indexSpecs: Value?): Value = promise<Any?> {
540+
override fun createIndexes(database: String, collection: String, indexSpecs: Value?, options: Value?, dbOptions: Value?): Value = promise<Any?> {
541541
val indexSpecs = toList(indexSpecs, "indexSpecs") ?: emptyList()
542+
val dbOptions = toDocument(dbOptions, "dbOptions")
542543
if (indexSpecs.any { it !is Document }) throw IllegalArgumentException("Index specs must be a list of documents. Got $indexSpecs")
543-
getDatabase(database, null).flatMap { db ->
544+
getDatabase(database, dbOptions).flatMap { db ->
544545
val convertedIndexes = indexSpecs.map { spec ->
545546
convert(IndexModel(Document()), indexModelConverters, indexModelDefaultConverter, spec as Document)
546547
}

packages/java-shell/src/main/kotlin/com/mongodb/mongosh/service/WritableServiceProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal interface WritableServiceProvider {
2525
fun save(database: String, collection: String, document: Value, options: Value?, dbOptions: Value?): Value
2626
fun remove(database: String, collection: String, query: Value, options: Value?, dbOptions: Value?): Value
2727
fun convertToCapped(database: String, collection: String, size: Number, options: Value?): Value
28-
fun createIndexes(database: String, collection: String, indexSpecs: Value?): Value
28+
fun createIndexes(database: String, collection: String, indexSpecs: Value?, options: Value?, dbOptions: Value?): Value
2929
fun dropIndexes(database: String, collection: String, indexes: Value?, options: Value?): Value
3030
fun reIndex(database: String, collection: String, options: Value?, dbOptions: Value?): Value
3131
fun dropCollection(database: String, collection: String, options: Value?): Value

packages/java-shell/src/test/kotlin/com/mongodb/mongosh/CollectionTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class CollectionTest : ShellTestCase() {
2222
@Test fun testCountWithQuery() = test()
2323
@Test fun testCountWithUnknownOption() = test()
2424
@Test fun testCreateIndex() = test()
25+
@Test fun testCreateIndexes() = test()
2526
@Test fun testDeleteMany() = test()
2627
@Test fun testDeleteOne() = test()
2728
@Test fun testDistinct() = test()

packages/java-shell/src/test/resources/collection/createIndex.expected.1.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[ "category_1" ]
2-
[ { "v": 2, "key": { "_id": 1 }, "name": "_id_", "ns": "admin.coll" }, { "v": 2, "key": { "category": 1 }, "name": "category_1", "ns": "admin.coll", "collation": { "locale": "fr", "caseLevel": false, "caseFirst": "off", "strength": 3, "numericOrdering": false, "alternate": "non-ignorable", "maxVariable": "punct", "normalization": false, "backwards": false, "version": "57.1" } } ]
2+
"category_1"

packages/java-shell/src/test/resources/collection/createIndex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ db.coll.insertOne({category: "cat2", v: 2});
55
db.coll.insertOne({category: "cat2", v: 3});
66
// command
77
db.coll.createIndex({category: 1}, {collation: {locale: "fr"}});
8-
// command
8+
// command getArrayItem=1 extractProperty=name
99
db.coll.getIndexes();
1010
// clear
1111
db.coll.drop();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[ "category_1", "title_1" ]
2+
"category_1"
3+
"title_1"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// before
2+
db.coll.deleteMany({});
3+
db.coll.insertMany([{category: "cat1", v: 1}, {category: "cat2", title: "t3", v: 2}, {category: "cat2", title: "t4", v: 3}]);
4+
// command
5+
db.coll.createIndexes([{"category": 1}, {"title": 1}], {collation: {locale: "fr", strength: 2}});
6+
// command getArrayItem=1 extractProperty=name
7+
db.coll.getIndexes();
8+
// command getArrayItem=2 extractProperty=name
9+
db.coll.getIndexes();
10+
// clear
11+
db.coll.drop();

packages/shell-api/src/collection.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,7 +1181,7 @@ describe('Collection', () => {
11811181
let internalSession: StubbedInstance<ServiceProviderSession>;
11821182
const exceptions = {
11831183
renameCollection: { a: ['name'] },
1184-
createIndexes: { a: [[]] },
1184+
createIndexes: { a: [[]], i: 4 },
11851185
runCommand: { a: ['coll', {} ], m: 'runCommandWithCheck', i: 2 },
11861186
findOne: { m: 'find' },
11871187
insert: { m: 'insertMany' },

packages/shell-api/src/collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ export default class Collection extends ShellApiClass {
973973

974974
this._emitCollectionApiCall('createIndexes', { specs });
975975

976-
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, specs, this._database._baseOptions);
976+
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, specs, {}, this._database._baseOptions);
977977
}
978978

979979
/**
@@ -999,7 +999,7 @@ export default class Collection extends ShellApiClass {
999999
this._emitCollectionApiCall('createIndex', { keys, options });
10001000

10011001
const spec = { ...this._database._baseOptions, ...options, key: keys };
1002-
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec]);
1002+
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], {}, this._database._baseOptions);
10031003
}
10041004

10051005
/**
@@ -1025,7 +1025,7 @@ export default class Collection extends ShellApiClass {
10251025
this._emitCollectionApiCall('ensureIndex', { keys, options });
10261026

10271027
const spec = { ...this._database._baseOptions, ...options, key: keys };
1028-
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec]);
1028+
return await this._mongo._serviceProvider.createIndexes(this._database._name, this._name, [spec], {}, this._database._baseOptions);
10291029
}
10301030

10311031
/**

0 commit comments

Comments
 (0)