Skip to content

Commit

Permalink
Added an ignore collection possibility to the clean all collections f…
Browse files Browse the repository at this point in the history
…unction. (#72)
  • Loading branch information
nscheibe authored Dec 10, 2022
1 parent 0d34b8e commit 191fc31
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 7 deletions.
80 changes: 80 additions & 0 deletions src/Prime.Extensions.Tests/MongoDatabaseExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,46 @@ public async Task CleanAllCollections_CleanAllDocumentsOfAllCollections_AllColle
Assert.Equal(0, fooCollection.CountDocuments());
}

[Fact]
public async Task CleanAllCollections_IgnoreOneCollectionToClean_SuccessfullyIgnored()
{
// Arrange
IMongoCollection<Bar> barCollection = _mongoDatabase.GetCollection<Bar>();
IMongoCollection<Foo> fooCollection = _mongoDatabase.GetCollection<Foo>();

var arrangedBars = new List<Bar> {
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"),
};

var arrangedFoo = new List<Foo> {
new Foo("Foo1", "Value1"),
new Foo("Foo2", "Value2"),
new Foo("Foo3", "Value3"),
new Foo("Foo4", "Value4"),
new Foo("Foo5", "Value5")
};

await barCollection.InsertManyAsync(arrangedBars);
await fooCollection.InsertManyAsync(arrangedFoo);

Assert.Equal(7, barCollection.CountDocuments());
Assert.Equal(5, fooCollection.CountDocuments());

// Act
_mongoDatabase.CleanAllCollections(
barCollection.CollectionNamespace.CollectionName);

// Assert
Assert.Equal(7, barCollection.CountDocuments());
Assert.Equal(0, fooCollection.CountDocuments());
}

[Fact]
public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_AllCollectionsEmpty()
{
Expand Down Expand Up @@ -285,6 +325,46 @@ public async Task CleanAllCollectionsAsync_CleanAllDocumentsOfAllCollections_All
Assert.Equal(0, fooCollection.CountDocuments());
}

[Fact]
public async Task CleanAllCollectionsAsync_IgnoreOneCollectionToClean_SuccessfullyIgnored()
{
// Arrange
IMongoCollection<Bar> barCollection = _mongoDatabase.GetCollection<Bar>();
IMongoCollection<Foo> fooCollection = _mongoDatabase.GetCollection<Foo>();

var arrangedBars = new List<Bar> {
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903041"), "Bar1", "Value1"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903042"), "Bar2", "Value2"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903043"), "Bar3", "Value3"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903044"), "Bar3", "Value4"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903045"), "Bar5", "Value5"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903046"), "Bar6", "Value6"),
new Bar(Guid.Parse("A1C9E3E8-B448-42DA-A684-716932903047"), "Bar7", "Value7"),
};

var arrangedFoo = new List<Foo> {
new Foo("Foo1", "Value1"),
new Foo("Foo2", "Value2"),
new Foo("Foo3", "Value3"),
new Foo("Foo4", "Value4"),
new Foo("Foo5", "Value5")
};

await barCollection.InsertManyAsync(arrangedBars);
await fooCollection.InsertManyAsync(arrangedFoo);

Assert.Equal(7, barCollection.CountDocuments());
Assert.Equal(5, fooCollection.CountDocuments());

// Act
await _mongoDatabase.CleanAllCollectionsAsync(
ignoreCollectionNames: fooCollection.CollectionNamespace.CollectionName);

// Assert
Assert.Equal(0, barCollection.CountDocuments());
Assert.Equal(5, fooCollection.CountDocuments());
}

#endregion CleanAllCollections Tests
}
}
43 changes: 36 additions & 7 deletions src/Prime.Extensions/MongoDatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -91,33 +93,46 @@ public static IMongoCollection<TDocument> GetCollection<TDocument>(
/// </summary>
/// <param name="mongoDatabase">The database to clean the collections.</param>
public static void CleanAllCollections(
this IMongoDatabase mongoDatabase)
this IMongoDatabase mongoDatabase,
params string[] ignoreCollectionNames)
{
foreach (var name in mongoDatabase.ListCollectionNames().ToList())
foreach (string collectionName in mongoDatabase.ListCollectionNames().ToList())
{
if (IgnoreCollection(collectionName, ignoreCollectionNames))
{
continue;
}

IMongoCollection<BsonDocument> collection =
mongoDatabase.GetCollection<BsonDocument>(name);
mongoDatabase.GetCollection<BsonDocument>(collectionName);

collection.CleanCollection();
}
}


/// <summary>
/// Deletes all entries of every collection of the mongo database.
/// The collections will NOT be dropped and the indexes stay unmodified.
/// </summary>
/// <param name="mongoDatabase">The database to clean the collections.</param>
public static async Task CleanAllCollectionsAsync(
this IMongoDatabase mongoDatabase,
CancellationToken cancellationToken = default)
this IMongoDatabase mongoDatabase,
CancellationToken cancellationToken = default,
params string[] ignoreCollectionNames)
{
IAsyncCursor<string> cursor = await mongoDatabase
.ListCollectionNamesAsync(cancellationToken: cancellationToken);

foreach (var name in await cursor.ToListAsync(cancellationToken))
foreach (var collectionName in await cursor.ToListAsync(cancellationToken))
{
if (IgnoreCollection(collectionName, ignoreCollectionNames))
{
continue;
}

IMongoCollection<BsonDocument> collection =
mongoDatabase.GetCollection<BsonDocument>(name);
mongoDatabase.GetCollection<BsonDocument>(collectionName);

await collection.CleanCollectionAsync(cancellationToken);
}
Expand Down Expand Up @@ -145,4 +160,18 @@ public static IEnumerable<KeyValuePair<string, IEnumerable<BsonDocument>>> DumpA

return dumpedCollections.OrderBy(entry => entry.Key);
}

private static bool IgnoreCollection(
string collectionName,
string[] ignoreCollectionNames)
{
bool isIgnoredCollection = ignoreCollectionNames
.Any(ignoredCollectionName => ignoredCollectionName
.Equals(collectionName, StringComparison.OrdinalIgnoreCase));

bool isSystemCollection = collectionName
.StartsWith("system", StringComparison.OrdinalIgnoreCase);

return isSystemCollection || isIgnoredCollection;
}
}

0 comments on commit 191fc31

Please sign in to comment.