summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data2
-rw-r--r--src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go21
-rw-r--r--src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go94
-rw-r--r--src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go1
4 files changed, 111 insertions, 7 deletions
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data
index 1f067e87b2b..a6b680a634b 100644
--- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data
+++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/import.data
@@ -1,5 +1,5 @@
{
- "commit": "2aeced60bec4310f9d87fd0c2dd535945e253126",
+ "commit": "39676d8c639a98d4005e6ca08ba24fa1221cdff3",
"github": "mongodb/mongo-tools.git",
"vendor": "tools",
"branch": "v4.0"
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go
index 7e644f3c047..33bd5e4d769 100644
--- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go
+++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/metadata.go
@@ -181,6 +181,15 @@ func (restore *MongoRestore) CollectionExists(intent *intents.Intent) (bool, err
return exists, nil
}
+// addToKnownCollections will add a collection to the restore.knownCollections cache.
+// This is used to update the cache after a collection has been created during a restore.
+func (restore *MongoRestore) addToKnownCollections(intent *intents.Intent) {
+ restore.knownCollectionsMutex.Lock()
+ defer restore.knownCollectionsMutex.Unlock()
+
+ restore.knownCollections[intent.DB] = append(restore.knownCollections[intent.DB], intent.C)
+}
+
// CreateIndexes takes in an intent and an array of index documents and
// attempts to create them using the createIndexes command. If that command
// fails, we fall back to individual index creation.
@@ -406,20 +415,20 @@ func (restore *MongoRestore) RestoreUsersOrRoles(users, roles *intents.Intent) e
}
// make sure we always drop the temporary collection
- defer func() {
+ defer func(cleanupArg loopArg) {
session, e := restore.SessionProvider.GetSession()
if e != nil {
// logging errors here because this has no way of returning that doesn't mask other errors
- log.Logvf(log.Info, "error establishing connection to drop temporary collection admin.%v: %v", arg.tempCollectionName, e)
+ log.Logvf(log.Info, "error establishing connection to drop temporary collection admin.%v: %v", cleanupArg.tempCollectionName, e)
return
}
defer session.Close()
- log.Logvf(log.DebugHigh, "dropping temporary collection admin.%v", arg.tempCollectionName)
- e = session.DB("admin").C(arg.tempCollectionName).DropCollection()
+ log.Logvf(log.DebugHigh, "dropping temporary collection admin.%v", cleanupArg.tempCollectionName)
+ e = session.DB("admin").C(cleanupArg.tempCollectionName).DropCollection()
if e != nil {
- log.Logvf(log.Info, "error dropping temporary collection admin.%v: %v", arg.tempCollectionName, e)
+ log.Logvf(log.Info, "error dropping temporary collection admin.%v: %v", cleanupArg.tempCollectionName, e)
}
- }()
+ }(arg)
userTargetDB = arg.intent.DB
}
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go
index bba951e4e58..5e23e240e5c 100644
--- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go
+++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/mongorestore_test.go
@@ -250,3 +250,97 @@ func TestMongorestorePreserveUUID(t *testing.T) {
})
}
+
+func TestRestoreUsersOrRoles(t *testing.T) {
+ testtype.VerifyTestType(t, testtype.IntegrationTestType)
+
+ nsOptions := &NSOptions{}
+ provider, toolOpts, err := testutil.GetBareSessionProvider()
+ if err != nil {
+ log.Logvf(log.Always, "error connecting to host: %v", err)
+ os.Exit(util.ExitError)
+ }
+
+ Convey("With a test MongoRestore", t, func() {
+ inputOptions := &InputOptions{}
+ outputOptions := &OutputOptions{
+ NumParallelCollections: 1,
+ NumInsertionWorkers: 1,
+ TempUsersColl: "tempusers",
+ TempRolesColl: "temproles",
+ }
+ restore := MongoRestore{
+ ToolOptions: toolOpts,
+ OutputOptions: outputOptions,
+ InputOptions: inputOptions,
+ NSOptions: nsOptions,
+ SessionProvider: provider,
+ }
+ session, _ := provider.GetSession()
+ defer session.Close()
+
+ db := session.DB("admin")
+
+ Convey("Restoring users and roles should drop tempusers and temproles", func() {
+ restore.TargetDirectory = "testdata/usersdump"
+ err := restore.Restore()
+ So(err, ShouldBeNil)
+
+ adminCollections, err := db.CollectionNames()
+ So(err, ShouldBeNil)
+
+ for _, collName := range adminCollections {
+ So(collName, ShouldNotEqual, "tempusers")
+ So(collName, ShouldNotEqual, "temproles")
+ }
+ })
+ })
+}
+
+func TestKnownCollections(t *testing.T) {
+ testtype.VerifyTestType(t, testtype.IntegrationTestType)
+
+ Convey("With a test MongoRestore", t, func() {
+ inputOptions := &InputOptions{}
+ outputOptions := &OutputOptions{
+ NumParallelCollections: 1,
+ NumInsertionWorkers: 1,
+ }
+ nsOptions := &NSOptions{}
+ provider, toolOpts, err := testutil.GetBareSessionProvider()
+ if err != nil {
+ log.Logvf(log.Always, "error connecting to host: %v", err)
+ os.Exit(util.ExitError)
+ }
+ restore := MongoRestore{
+ ToolOptions: toolOpts,
+ OutputOptions: outputOptions,
+ InputOptions: inputOptions,
+ NSOptions: nsOptions,
+ SessionProvider: provider,
+ }
+ session, _ := provider.GetSession()
+ defer session.Close()
+
+ db := session.DB("test")
+ defer func() {
+ db.C("foo").DropCollection()
+ }()
+
+ Convey("Once collection foo has been restored, it should exist in restore.knownCollections", func() {
+ restore.TargetDirectory = "testdata/foodump"
+ err := restore.Restore()
+ So(err, ShouldBeNil)
+
+ var namespaceExistsInCache bool
+ if cols, ok := restore.knownCollections["test"]; ok {
+ for _, collName := range cols {
+ if collName == "foo" {
+ namespaceExistsInCache = true
+ }
+ }
+ }
+ So(namespaceExistsInCache, ShouldBeTrue)
+ })
+ })
+}
diff --git a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go
index a0175807d37..b02e7530674 100644
--- a/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go
+++ b/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongorestore/restore.go
@@ -197,6 +197,7 @@ func (restore *MongoRestore) RestoreIntent(intent *intents.Intent) error {
if err != nil {
return fmt.Errorf("error creating collection %v: %v", intent.Namespace(), err)
}
+ restore.addToKnownCollections(intent)
} else {
log.Logvf(log.Info, "collection %v already exists - skipping collection create", intent.Namespace())
}