diff --git a/backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs b/backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs index 7d929ddd8..e06b86713 100644 --- a/backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs +++ b/backend/src/Squidex.Data.MongoDb/Infrastructure/MongoExtensions.cs @@ -168,11 +168,6 @@ public static class MongoExtensions var (key, snapshot, _, oldVersion) = job; try { - Expression> 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(Builders.Projection.Include("_id").Include(versionField)) + .FirstOrDefaultAsync(ct); + + var currentVersion = existingVersion != null + ? existingVersion[Field.Of(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) diff --git a/backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs b/backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs index 8c02c9b45..034c7596d 100644 --- a/backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs +++ b/backend/tests/Squidex.Data.Tests/Shared/SnapshotStoreTests_T.cs @@ -104,7 +104,8 @@ public abstract class SnapshotStoreTests await sut.WriteAsync(new SnapshotWriteJob(sourceKey, sourceValue, 42)); - var ex = await Assert.ThrowsAsync(() => sut.WriteAsync(new SnapshotWriteJob(sourceKey, sourceValue, 2, 1))); + var ex = await Assert.ThrowsAsync(() => + sut.WriteAsync(new SnapshotWriteJob(sourceKey, sourceValue, 2, 1))); Assert.Equal(42, ex.VersionCurrent); }