diff --git a/src/Titano.Data/Archiving/ArchiveIndexer.cs b/src/Titano.Data/Archiving/ArchiveIndexer.cs index a8508381..fd15f4c8 100644 --- a/src/Titano.Data/Archiving/ArchiveIndexer.cs +++ b/src/Titano.Data/Archiving/ArchiveIndexer.cs @@ -100,7 +100,8 @@ public sealed class ArchiveIndexer PositionMove? positions = null; await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) { - var existing = await tx.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false); + // Locked: a settings edit committed after this read (the server's PATCH takes no lease) would be written back over. + var existing = await tx.Sequences.GetForUpdateAsync(manifest.Id, cancellationToken).ConfigureAwait(false); if (existing is not null) { positions = await ProjectPositions.MoveAsync(tx, existing, manifest, cancellationToken).ConfigureAwait(false); diff --git a/src/Titano.Data/Archiving/ArchiveIngest.cs b/src/Titano.Data/Archiving/ArchiveIngest.cs index 81fd0afc..58fae32e 100644 --- a/src/Titano.Data/Archiving/ArchiveIngest.cs +++ b/src/Titano.Data/Archiving/ArchiveIngest.cs @@ -195,7 +195,8 @@ public sealed class ArchiveIngest : IArchiveIngest { if (row.SettingsJson is { } settings) AtomicFile.WriteAllText(sequence.ProjectPath, settings); await using var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false); - var current = await tx.Sequences.GetAsync(sequenceId, cancellationToken).ConfigureAwait(false) + // Locked, so an edit of the new sequence committed meanwhile is not written back over. + var current = await tx.Sequences.GetForUpdateAsync(sequenceId, cancellationToken).ConfigureAwait(false) ?? throw new InvalidOperationException($"Sequence {sequenceId} was deleted before its first frame."); await tx.Sequences.UpsertAsync(SequenceMapping.ToSequence(manifest, archive.Id, sequence, current), cancellationToken).ConfigureAwait(false); await tx.CommitAsync(cancellationToken).ConfigureAwait(false); @@ -473,7 +474,8 @@ public sealed class ArchiveIngest : IArchiveIngest PositionMove? placed = null; await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false)) { - var existing = await tx.Sequences.GetAsync(manifest.Id, cancellationToken).ConfigureAwait(false); + // Locked: a settings edit committed after this read (the server's PATCH takes no lease) would be written back over. + var existing = await tx.Sequences.GetForUpdateAsync(manifest.Id, cancellationToken).ConfigureAwait(false); if (existing is not null && ProjectPositions.Place(existing, manifest) is { } positions) { placed = positions; diff --git a/src/Titano.Data/Repositories/SequenceRepository.cs b/src/Titano.Data/Repositories/SequenceRepository.cs index 75b0be08..3ee1a531 100644 --- a/src/Titano.Data/Repositories/SequenceRepository.cs +++ b/src/Titano.Data/Repositories/SequenceRepository.cs @@ -167,6 +167,14 @@ public sealed class SequenceRepository public Task GetAsync(Guid id, CancellationToken cancellationToken = default) => _db.SingleAsync($"{_select} WHERE s.id = $1", [id], Map, cancellationToken); + /// + /// inside a transaction, the row locked until it ends (PostgreSQL; SQLite's write lock already + /// serialises writers): a transaction that writes the whole row back from this read cannot undo an edit that another + /// one commits meanwhile, as takes the same lock. + /// + public Task GetForUpdateAsync(Guid id, CancellationToken cancellationToken = default) => + _db.SingleAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken); + /// Deletes the sequence with its frames, renders and tag links. public async Task DeleteAsync(Guid id, CancellationToken cancellationToken = default) => await _db.ExecuteAsync("DELETE FROM sequences WHERE id = $1", [id], cancellationToken).ConfigureAwait(false) > 0; diff --git a/tests/Titano.Data.Tests/Repositories/SequenceEditTests.cs b/tests/Titano.Data.Tests/Repositories/SequenceEditTests.cs index 5ab34ca1..bab97147 100644 --- a/tests/Titano.Data.Tests/Repositories/SequenceEditTests.cs +++ b/tests/Titano.Data.Tests/Repositories/SequenceEditTests.cs @@ -69,6 +69,38 @@ public abstract class SequenceEditTests(TestContext context) : RepositoryTest(co Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome); Assert.Null((await db.Sequences.GetAsync(sequence.Id, Ct))!.HeroFrameId); } + + [Test] + public async Task AnEditWaitsForARowWrittenBackFromALockedRead() + { + var db = await OpenTitanoAsync(); + var archive = TestData.Archive(); + await db.Archives.UpsertAsync(archive, Ct); + var sequence = TestData.Sequence(archive.Id); + await db.Sequences.UpsertAsync(sequence, Ct); + + // An index commit reads the row and writes it back with its frames; a settings edit (the server's PATCH) arrives meanwhile. + Task edit; + await using (var tx = await db.BeginAsync(Ct)) + { + var read = Assert.NotNull(await tx.Sequences.GetForUpdateAsync(sequence.Id, Ct)); + edit = db.Sequences.EditAsync(sequence.Id, new SequenceEdit { SettingsJson = """{"excludedFrames":[1]}""" }, read.SettingsVersion, read.FramesVersion, Ct); + await Task.WhenAny(edit, Task.Delay(300, Ct)); + Assert.False(edit.IsCompleted, "the edit waits for the row"); + await tx.Sequences.UpsertAsync(read with { FramesVersion = read.FramesVersion + 1 }, Ct); + await tx.CommitAsync(Ct); + } + + // It then finds the frames changed: written against the old ones, it is refused rather than undone. + var result = await edit; + Assert.Equal(SequenceEditOutcome.Conflict, result.Outcome); + var stored = (await db.Sequences.GetAsync(sequence.Id, Ct))!; + Assert.Equal((sequence.SettingsVersion, sequence.FramesVersion + 1), (stored.SettingsVersion, stored.FramesVersion)); + + // Without frames in question, it applies on top of the committed row. + var later = await db.Sequences.EditAsync(sequence.Id, new SequenceEdit { Rating = 3 }, stored.SettingsVersion, Ct); + Assert.Equal((sequence.SettingsVersion + 1, sequence.FramesVersion + 1, 3), (later.Sequence!.SettingsVersion, later.Sequence.FramesVersion, later.Sequence.Rating)); + } } public sealed class SqliteSequenceEditTests(TestContext context) : SequenceEditTests(context)