Skip to content

Commit

Permalink
fix bug where you couldn't submit updates to an existing entity after…
Browse files Browse the repository at this point in the history
… creating a new data model instance.
  • Loading branch information
hahn-kev committed Sep 20, 2024
1 parent 59a1700 commit 0063f05
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/SIL.Harmony/Db/CrdtRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ public async Task<Commit[]> GetCommitsAfter(Commit? commit)
.ToArrayAsync();
}

public async Task<ObjectSnapshot?> FindSnapshot(Guid id)
public async Task<ObjectSnapshot?> 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<ObjectSnapshot?> GetCurrentSnapshotByObjectId(Guid objectId)
public async Task<ObjectSnapshot?> 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));
}
Expand Down
7 changes: 7 additions & 0 deletions src/SIL.Harmony/Db/DbSetExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -26,4 +28,9 @@ public static IQueryable<ObjectSnapshot> WhereAfter(this IQueryable<ObjectSnapsh
|| (after.HybridDateTime.DateTime == s.Commit.HybridDateTime.DateTime && after.HybridDateTime.Counter < s.Commit.HybridDateTime.Counter)
|| (after.HybridDateTime.DateTime == s.Commit.HybridDateTime.DateTime && after.HybridDateTime.Counter == s.Commit.HybridDateTime.Counter && after.Id < s.Commit.Id));
}

public static IQueryable<T> AsTracking<T>(this IQueryable<T> queryable, bool tracking = true) where T : class
{
return queryable.AsTracking(tracking ? QueryTrackingBehavior.TrackAll : QueryTrackingBehavior.NoTracking);
}
}
4 changes: 2 additions & 2 deletions src/SIL.Harmony/SnapshotWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0063f05

Please sign in to comment.