Browse Source

Test another update.

pull/1301/head
Sebastian Stehle 3 months ago
parent
commit
981ca57150
  1. 19
      backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs
  2. 3
      backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs

19
backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs

@ -168,11 +168,6 @@ public static class MongoExtensions
var (key, snapshot, _, oldVersion) = job;
try
{
Expression<Func<T, bool>> filter2 =
oldVersion > EtagVersion.Any ?
x => x.DocumentId.Equals(key) && x.Version == oldVersion :
x => x.DocumentId.Equals(key);
var filter = filters.Eq(x => x.DocumentId, key);
if (oldVersion > EtagVersion.Any)
@ -182,6 +177,20 @@ public static class MongoExtensions
var result = await collection.ReplaceOneAsync(filter, job.Value, UpsertReplace, ct);
if (result.IsAcknowledged && result.ModifiedCount == 0 && oldVersion > EtagVersion.Any)
{
var existingVersion =
await collection.Find(filters.Eq("_id", key))
.Project<BsonDocument>(Builders<T>.Projection.Include("_id").Include(versionField))
.FirstOrDefaultAsync(ct);
var currentVersion = existingVersion != null
? existingVersion[Field.Of<T>(x => nameof(x.Version))].AsInt64
: EtagVersion.Any;
throw new InconsistentStateException(currentVersion, oldVersion);
}
return result.IsAcknowledged && result.ModifiedCount == 1;
}
catch (MongoWriteException ex) when (ex.WriteError?.Category == ServerErrorCategory.DuplicateKey)

3
backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs

@ -104,7 +104,8 @@ public abstract class SnapshotStoreTests<TEntity>
await sut.WriteAsync(new SnapshotWriteJob<TEntity>(sourceKey, sourceValue, 42));
var ex = await Assert.ThrowsAsync<InconsistentStateException>(() => sut.WriteAsync(new SnapshotWriteJob<TEntity>(sourceKey, sourceValue, 2, 1)));
var ex = await Assert.ThrowsAsync<InconsistentStateException>(() =>
sut.WriteAsync(new SnapshotWriteJob<TEntity>(sourceKey, sourceValue, 2, 1)));
Assert.Equal(42, ex.VersionCurrent);
}

Loading…
Cancel
Save