Skip to content

Commit

Permalink
hotfix for bulk updates not working with .ModifyWith(entity)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-nitehawk committed Feb 10, 2022
1 parent e2dbb08 commit b03f64e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MongoDB.Entities/Builders/Update.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ public Update<T> AddToQueue()
var mergedFilter = Logic.MergeWithGlobalFilter(ignoreGlobalFilters, globalFilters, filter);
if (mergedFilter == Builders<T>.Filter.Empty) throw new ArgumentException("Please use Match() method first!");
if (defs.Count == 0) throw new ArgumentException("Please use Modify() method first!");
if (Cache<T>.HasModifiedOn) Modify(b => b.CurrentDate(Cache<T>.ModifiedOnPropName));
if (ShouldSetModDate()) Modify(b => b.CurrentDate(Cache<T>.ModifiedOnPropName));
onUpdateAction?.Invoke(this);
models.Add(new UpdateManyModel<T>(mergedFilter, Builders<T>.Update.Combine(defs))
{
Expand Down
6 changes: 2 additions & 4 deletions MongoDB.Entities/MongoDB.Entities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

<PropertyGroup>

<Version>20.26.1</Version>
<Version>20.26.2</Version>

<PackageReleaseNotes>
- hotfix for issue #146 (all children not removed when supplying multiple child IDs)
- change project lang version to 10.0
- upgrade test/benchmark project SDK to .net 6.0
- hotfix for bulk updates not working with .ModifyWith(entity)
- upgrade dependencies to latest
</PackageReleaseNotes>

Expand Down
43 changes: 43 additions & 0 deletions Tests/TestUpdating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,49 @@ await DB.Update<Flower>()
Assert.AreEqual("daisy", res.Name);
}

[TestMethod]
public async Task bulk_update_with_modifywith()
{
var books = new[] {
new Book{ Title ="one"},
new Book{ Title ="two"},
};

await books.SaveAsync();

foreach (var book in books)
{
await DB
.Update<Book>()
.MatchID(book.ID)
.Modify(b => b.ModifiedOn, DateTime.UtcNow.AddDays(-100))
.ExecuteAsync();
}

var bulkUpdate = DB.Update<Book>();

foreach (var book in books)
{
book.Title = "updated!";
bulkUpdate
.MatchID(book.ID)
.ModifyWith(book)
.AddToQueue();
}

await bulkUpdate.ExecuteAsync();

var bIDs = books.Select(b => b.ID).ToArray();

var res = await DB.Find<Book>()
.Match(b => bIDs.Contains(b.ID))
.ExecuteAsync();

Assert.AreEqual("updated!", res[0].Title);
Assert.AreEqual("updated!", res[1].Title);
Assert.IsTrue(res.All(b => b.ModifiedOn.Date == DateTime.UtcNow.Date));
}

[TestMethod]
public async Task update_with_modifyexcept_works()
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.0">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit b03f64e

Please sign in to comment.