diff options
author | Gregory Wlodarek <gregory.wlodarek@mongodb.com> | 2019-02-26 09:11:32 -0500 |
---|---|---|
committer | Gregory Wlodarek <gregory.wlodarek@mongodb.com> | 2019-03-20 08:26:08 -0400 |
commit | 7948bd50e4f43e0285fac0a11170871db81fff7c (patch) | |
tree | bf7187da5a18d9cfc06abb18abad55851d08b731 | |
parent | d8d3d3ab1cef37bcc6f65182463b83eaf4669e17 (diff) | |
download | mongo-7948bd50e4f43e0285fac0a11170871db81fff7c.tar.gz |
SERVER-33149 createIndexes fails to report an error when index is not created with the specified name
-rw-r--r-- | jstests/core/create_index_same_spec_different_name.js | 17 | ||||
-rw-r--r-- | jstests/core/fts_index.js | 6 | ||||
-rw-r--r-- | jstests/core/indexes_multiple_commands.js | 10 | ||||
-rw-r--r-- | src/mongo/db/catalog/index_catalog_impl.cpp | 7 | ||||
-rw-r--r-- | src/mongo/dbtests/indexupdatetests.cpp | 37 |
5 files changed, 68 insertions, 9 deletions
diff --git a/jstests/core/create_index_same_spec_different_name.js b/jstests/core/create_index_same_spec_different_name.js new file mode 100644 index 00000000000..660a2a714fc --- /dev/null +++ b/jstests/core/create_index_same_spec_different_name.js @@ -0,0 +1,17 @@ +/** + * Ensures that creating an index with the same key but different name returns the + * 'IndexOptionsConflict' error. + */ +(function() { + 'use strict'; + + const coll = "create_index_same_spec_different_name"; + db.coll.drop(); + + assert.commandWorked( + db.runCommand({createIndexes: coll, indexes: [{key: {x: 1}, name: "x_1"}]})); + + assert.commandFailedWithCode( + db.runCommand({createIndexes: coll, indexes: [{key: {x: 1}, name: "x_2"}]}), + ErrorCodes.IndexOptionsConflict); +}()); diff --git a/jstests/core/fts_index.js b/jstests/core/fts_index.js index fff31d98ab8..13dbcdc2647 100644 --- a/jstests/core/fts_index.js +++ b/jstests/core/fts_index.js @@ -123,7 +123,11 @@ assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1})); assert.eq(2, coll.getIndexes().length); assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {background: true})); assert.eq(2, coll.getIndexes().length); -assert.commandWorked(coll.ensureIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {b: 1}})); +assert.commandFailedWithCode(coll.ensureIndex({a: 1, b: 1, c: "text"}), + ErrorCodes.CannotCreateIndex); +assert.commandFailedWithCode( + coll.ensureIndex({a: 1, _fts: "text", _ftsx: 1, c: 1}, {weights: {b: 1}}), + ErrorCodes.IndexOptionsConflict); assert.eq(2, coll.getIndexes().length); assert.commandWorked(coll.ensureIndex({a: 1, b: "text", c: 1}, {default_language: "english"})); assert.eq(2, coll.getIndexes().length); diff --git a/jstests/core/indexes_multiple_commands.js b/jstests/core/indexes_multiple_commands.js index fe27c3a93a2..60bc2b69173 100644 --- a/jstests/core/indexes_multiple_commands.js +++ b/jstests/core/indexes_multiple_commands.js @@ -28,6 +28,11 @@ if (usingWriteCommands) { cmdResult = cmd(); + if (numIndexes == 0) { + assert.commandFailedWithCode(cmdResult, ErrorCodes.IndexOptionsConflict); + return; + } + assert.commandWorked(cmdResult); var isShardedNS = cmdResult.hasOwnProperty('raw'); if (isShardedNS) { @@ -39,6 +44,11 @@ } else { var nIndexesBefore = coll.getIndexes().length; cmdResult = cmd(); + if (numIndexes == 0) { + assert.commandFailedWithCode(cmdResult, ErrorCodes.IndexOptionsConflict); + return; + } + assert.commandWorked(cmdResult); var nIndexesAfter = coll.getIndexes().length; assert.eq(nIndexesAfter - nIndexesBefore, numIndexes, tojson(coll.getIndexes())); diff --git a/src/mongo/db/catalog/index_catalog_impl.cpp b/src/mongo/db/catalog/index_catalog_impl.cpp index 7f7d98b17c5..d43a7242df5 100644 --- a/src/mongo/db/catalog/index_catalog_impl.cpp +++ b/src/mongo/db/catalog/index_catalog_impl.cpp @@ -678,7 +678,7 @@ Status IndexCatalogImpl::_doesSpecConflictWithExisting(OperationContext* opCtx, const IndexDescriptor* desc = findIndexByKeyPatternAndCollationSpec(opCtx, key, collation, findInProgressIndexes); if (desc) { - LOG(2) << "index already exists with diff name " << name << " pattern: " << key + LOG(2) << "Index already exists with a different name: " << name << " pattern: " << key << " collation: " << collation; IndexDescriptor temp(_collection, _getAccessMethodName(key), spec); @@ -688,8 +688,9 @@ Status IndexCatalogImpl::_doesSpecConflictWithExisting(OperationContext* opCtx, << " already exists with different options: " << desc->infoObj()); - return Status(ErrorCodes::IndexAlreadyExists, - str::stream() << "index already exists with different name: " << name); + return Status(ErrorCodes::IndexOptionsConflict, + str::stream() << "Index with name: " << name + << " already exists with a different name"); } } diff --git a/src/mongo/dbtests/indexupdatetests.cpp b/src/mongo/dbtests/indexupdatetests.cpp index 489448435aa..e967bff83bc 100644 --- a/src/mongo/dbtests/indexupdatetests.cpp +++ b/src/mongo/dbtests/indexupdatetests.cpp @@ -414,14 +414,40 @@ public: } }; -class SameSpecSameOptionDifferentOrder : public ComplexIndex { +class SameSpecDifferentNameDifferentOrder : public ComplexIndex { public: void run() { - // Exactly the same specs with the existing one, only - // specified in a different order than the original. + // Exactly the same specs with the existing one, only specified in a different order than + // the original. This will throw an IndexOptionsConflict as the index already exists under + // another name. + ASSERT_EQUALS(ErrorCodes::IndexOptionsConflict, + createIndex("unittests", + BSON("name" + << "super2" + << "ns" + << _ns + << "expireAfterSeconds" + << 3600 + << "sparse" + << true + << "unique" + << 1 + << "key" + << BSON("superIdx" + << "2d") + << "v" + << static_cast<int>(kIndexVersion)))); + } +}; + +class SameSpecSameNameDifferentOrder : public ComplexIndex { +public: + void run() { + // Exactly the same specs with the existing one, only specified in a different order than + // the original, but with the same name. ASSERT_OK(createIndex("unittests", BSON("name" - << "super2" + << "super" << "ns" << _ns << "expireAfterSeconds" @@ -705,7 +731,8 @@ public: add<SameSpecDifferentOption>(); add<SameSpecSameOptions>(); add<DifferentSpecSameName>(); - add<SameSpecSameOptionDifferentOrder>(); + add<SameSpecDifferentNameDifferentOrder>(); + add<SameSpecSameNameDifferentOrder>(); add<SameSpecDifferentUnique>(); add<SameSpecDifferentSparse>(); add<SameSpecDifferentTTL>(); |