From 0063f050e8ef3d79f64c81d89f36c8a5dceff489 Mon Sep 17 00:00:00 2001 From: Kevin Hahn Date: Fri, 20 Sep 2024 12:05:24 +0700 Subject: [PATCH] fix bug where you couldn't submit updates to an existing entity after creating a new data model instance. --- src/SIL.Harmony/Db/CrdtRepository.cs | 11 +++++++---- src/SIL.Harmony/Db/DbSetExtensions.cs | 7 +++++++ src/SIL.Harmony/SnapshotWorker.cs | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/SIL.Harmony/Db/CrdtRepository.cs b/src/SIL.Harmony/Db/CrdtRepository.cs index f0ba970..0636008 100644 --- a/src/SIL.Harmony/Db/CrdtRepository.cs +++ b/src/SIL.Harmony/Db/CrdtRepository.cs @@ -143,14 +143,17 @@ public async Task GetCommitsAfter(Commit? commit) .ToArrayAsync(); } - public async Task FindSnapshot(Guid id) + public async Task FindSnapshot(Guid id, bool tracking = false) { - return await Snapshots.Include(s => s.Commit).SingleOrDefaultAsync(s => s.Id == id); + return await Snapshots + .AsTracking(tracking) + .Include(s => s.Commit) + .SingleOrDefaultAsync(s => s.Id == id); } - public async Task GetCurrentSnapshotByObjectId(Guid objectId) + public async Task GetCurrentSnapshotByObjectId(Guid objectId, bool tracking = false) { - return await Snapshots.Include(s => s.Commit) + return await Snapshots.AsTracking(tracking).Include(s => s.Commit) .DefaultOrder() .LastOrDefaultAsync(s => s.EntityId == objectId && (ignoreChangesAfter == null || s.Commit.DateTime <= ignoreChangesAfter)); } diff --git a/src/SIL.Harmony/Db/DbSetExtensions.cs b/src/SIL.Harmony/Db/DbSetExtensions.cs index 73e2e4b..ece85de 100644 --- a/src/SIL.Harmony/Db/DbSetExtensions.cs +++ b/src/SIL.Harmony/Db/DbSetExtensions.cs @@ -1,3 +1,5 @@ +using Microsoft.EntityFrameworkCore; + namespace SIL.Harmony.Db; //todo, I would like to move these extensions into QueryHelperTests but that's in Core and ObjectSnapshot is not part of core @@ -26,4 +28,9 @@ public static IQueryable WhereAfter(this IQueryable AsTracking(this IQueryable queryable, bool tracking = true) where T : class + { + return queryable.AsTracking(tracking ? QueryTrackingBehavior.TrackAll : QueryTrackingBehavior.NoTracking); + } } \ No newline at end of file diff --git a/src/SIL.Harmony/SnapshotWorker.cs b/src/SIL.Harmony/SnapshotWorker.cs index 7b2a970..60ab1dd 100644 --- a/src/SIL.Harmony/SnapshotWorker.cs +++ b/src/SIL.Harmony/SnapshotWorker.cs @@ -177,10 +177,10 @@ private async ValueTask MarkDeleted(Guid deletedEntityId, Commit commit) if (_snapshotLookup.TryGetValue(entityId, out var snapshotId)) { if (snapshotId is null) return null; - return await _crdtRepository.FindSnapshot(snapshotId.Value); + return await _crdtRepository.FindSnapshot(snapshotId.Value, true); } - snapshot = await _crdtRepository.GetCurrentSnapshotByObjectId(entityId); + snapshot = await _crdtRepository.GetCurrentSnapshotByObjectId(entityId, true); _snapshotLookup[entityId] = snapshot?.Id; return snapshot;