Permalink
Browse files

Implemented CSHARP-286. Added support for dropTarget to RenameCollect…

…ion. Taken with minor changes from a pull request by Dow Liu.
  • Loading branch information...
1 parent aade7d8 commit 92be4a63bb8a4156cd2dd67bd18284ad9f4e415a rstam committed Jul 26, 2011
Showing with 25 additions and 13 deletions.
  1. +19 −8 Driver/Core/MongoDatabase.cs
  2. +5 −5 DriverOnlineTests/Core/MongoDatabaseTests.cs
  3. +1 −0 README.md
@@ -726,21 +726,32 @@ string username
/// Renames a collection on this database.
/// </summary>
/// <param name="oldCollectionName">The old name for the collection.</param>
- /// <param name="newCollectionName">The new name for the collection.</param>
- /// <param name="dropTarget">drop target collection if exist in db</param>
+ /// <param name="newCollectionName">The new name for the collection.</param>
+ /// <returns>A CommandResult.</returns>
+ public virtual CommandResult RenameCollection(
+ string oldCollectionName,
+ string newCollectionName
+ ) {
+ return RenameCollection(oldCollectionName, newCollectionName, false); // dropTarget = false
+ }
+
+ /// <summary>
+ /// Renames a collection on this database.
+ /// </summary>
+ /// <param name="oldCollectionName">The old name for the collection.</param>
+ /// <param name="newCollectionName">The new name for the collection.</param>
+ /// <param name="dropTarget">Whether to drop the target collection first if it already exists.</param>
/// <returns>A CommandResult.</returns>
public virtual CommandResult RenameCollection(
string oldCollectionName,
string newCollectionName,
- bool dropTarget = false
+ bool dropTarget
) {
var command = new CommandDocument {
{ "renameCollection", string.Format("{0}.{1}", name, oldCollectionName) },
- { "to", string.Format("{0}.{1}", name, newCollectionName) }
- };
- if (dropTarget) {
- command.Add("dropTarget", BsonBoolean.True);
- }
+ { "to", string.Format("{0}.{1}", name, newCollectionName) },
+ { "dropTarget", dropTarget, dropTarget } // only added if dropTarget is true
+ };
return server.RunAdminCommand(command);
}
@@ -164,17 +164,17 @@ public class MongoDatabaseTests {
[Test]
public void TestRenameCollectionDropTarget() {
- const string collectionName1 = "testrenamecollection3";
- const string collectionName2 = "testrenamecollection4";
+ const string collectionName1 = "testrenamecollectiondroptarget1";
+ const string collectionName2 = "testrenamecollectiondroptarget2";
Assert.IsFalse(database.CollectionExists(collectionName1));
Assert.IsFalse(database.CollectionExists(collectionName2));
database[collectionName1].Insert(new BsonDocument());
database[collectionName2].Insert(new BsonDocument());
Assert.IsTrue(database.CollectionExists(collectionName1));
- Assert.True(database.CollectionExists(collectionName2));
-
- Assert.Throws(typeof(MongoCommandException), () => database.RenameCollection(collectionName1, collectionName2));
+ Assert.IsTrue(database.CollectionExists(collectionName2));
+
+ Assert.Throws<MongoCommandException>(() => database.RenameCollection(collectionName1, collectionName2));
database.RenameCollection(collectionName1, collectionName2, true);
Assert.IsFalse(database.CollectionExists(collectionName1));
Assert.IsTrue(database.CollectionExists(collectionName2));
View
@@ -29,6 +29,7 @@ see also: BreakingChanges.txt
* Simon Green simon@captaincodeman.com
* Richard Kreuter richard@10gen.com
* Kevin Lewis kevin.l.lewis@gmail.com
+* Dow Liu redforks@gmail.com
* Andrew Rondeau github@andrewrondeau.com
* Ed Rooth edward.rooth@wallstreetjapan.com
* Testo test1@doramail.com

0 comments on commit 92be4a6

Please sign in to comment.