From 9bc30836f60a888c44de09c55a45c278b215a02b Mon Sep 17 00:00:00 2001 From: Benety Goh Date: Thu, 23 Mar 2017 13:55:22 -0400 Subject: [PATCH] SERVER-27834 FeatureCompatibilityVersion creates the admin.system.version collection if missing. This avoids relying on the implicit collection behavior in IndexBuilder. --- .../db/commands/feature_compatibility_version.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/mongo/db/commands/feature_compatibility_version.cpp b/src/mongo/db/commands/feature_compatibility_version.cpp index e636c8f9987cc..4e9ed583696de 100644 --- a/src/mongo/db/commands/feature_compatibility_version.cpp +++ b/src/mongo/db/commands/feature_compatibility_version.cpp @@ -220,6 +220,13 @@ void FeatureCompatibilityVersion::set(OperationContext* opCtx, StringData versio repl::ReplicationCoordinator::get(opCtx->getServiceContext()) ->canAcceptWritesFor(opCtx, nss)); + // If the "admin.system.version" collection has not been created yet, explicitly create + // it to hold the v=2 index. + if (!autoDB.getDb()->getCollection(nss)) { + uassertStatusOK( + repl::StorageInterface::get(opCtx)->createCollection(opCtx, nss, {})); + } + IndexBuilder builder(k32IncompatibleIndexSpec, false); auto status = builder.buildInForeground(opCtx, autoDB.getDb()); uassertStatusOK(status); @@ -304,6 +311,12 @@ void FeatureCompatibilityVersion::setIfCleanStartup(OperationContext* opCtx, { AutoGetOrCreateDb autoDB(opCtx, nss.db(), MODE_X); + // We reached this point because the only database that exists on the server is "local" + // and we have just created an empty "admin" database. + // Therefore, it is safe to create the "admin.system.version" collection. + invariant(autoDB.justCreated()); + uassertStatusOK(storageInterface->createCollection(opCtx, nss, {})); + IndexBuilder builder(k32IncompatibleIndexSpec, false); auto status = builder.buildInForeground(opCtx, autoDB.getDb()); uassertStatusOK(status);