diff options
author | Benety Goh <benety@mongodb.com> | 2021-11-03 22:31:26 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-11-04 02:54:25 +0000 |
commit | 4a5690123c82851503acac889d1c8e7537522972 (patch) | |
tree | e3c1141b6729d9c72f86d4d1db83943b7b6b7398 | |
parent | d462d6532a5511c5a3c03f23e3d7c044068e8637 (diff) | |
download | mongo-4a5690123c82851503acac889d1c8e7537522972.tar.gz |
SERVER-61158 Convert a non-unique index to a unique index via the collMod command
-rw-r--r-- | jstests/core/collmod_convert_to_unique.js | 8 | ||||
-rw-r--r-- | src/mongo/db/catalog/coll_mod_index.cpp | 6 |
2 files changed, 12 insertions, 2 deletions
diff --git a/jstests/core/collmod_convert_to_unique.js b/jstests/core/collmod_convert_to_unique.js index 280f571c005..27bd11b0344 100644 --- a/jstests/core/collmod_convert_to_unique.js +++ b/jstests/core/collmod_convert_to_unique.js @@ -7,6 +7,10 @@ * assumes_no_implicit_collection_creation_after_drop, # common tag in collMod tests. * requires_fcv_52, * requires_non_retryable_commands, # common tag in collMod tests. + * # TODO(SERVER-61181): Fix validation errors under ephemeralForTest. + * incompatible_with_eft, + * # TODO(SERVER-61182): Fix WiredTigerKVEngine::alterIdentMetadata() under inMemory. + * requires_persistence, * ] */ @@ -72,8 +76,8 @@ if (db.getMongo().isMongos()) { } // Look up index details in listIndexes output. -assert.eq(countUnique({a: 1}), 0, 'index should not be unique yet: ' + tojson(coll.getIndexes())); +assert.eq(countUnique({a: 1}), 1, 'index should be unique now: ' + tojson(coll.getIndexes())); // Test uniqueness constraint. -assert.commandWorked(coll.insert({_id: 100, a: 100})); +assert.commandFailedWithCode(coll.insert({_id: 100, a: 100}), ErrorCodes.DuplicateKey); })(); diff --git a/src/mongo/db/catalog/coll_mod_index.cpp b/src/mongo/db/catalog/coll_mod_index.cpp index 9fbf29bab63..31207f38d3e 100644 --- a/src/mongo/db/catalog/coll_mod_index.cpp +++ b/src/mongo/db/catalog/coll_mod_index.cpp @@ -150,6 +150,7 @@ void _processCollModIndexRequestUnique(OperationContext* opCtx, return; } *newUnique = indexUnique; + autoColl->getWritableCollection()->updateUniqueSetting(opCtx, idx->indexName()); } } // namespace @@ -206,6 +207,11 @@ void processCollModIndexRequest(OperationContext* opCtx, // This matches the default for IndexCatalog::refreshEntry(). auto flags = CreateIndexEntryFlags::kIsReady; + // Update data format version in storage engine metadata for index. + if (indexUnique) { + flags = CreateIndexEntryFlags::kIsReady | CreateIndexEntryFlags::kUpdateMetadata; + } + // Notify the index catalog that the definition of this index changed. This will invalidate the // local idx pointer. On rollback of this WUOW, the local var idx pointer will be valid again. autoColl->getWritableCollection()->getIndexCatalog()->refreshEntry( |