fix(index): lock the sequence row while an index commit writes it back

ArchiveIndexer.SyncAsync and the ingest's in-step append (and the first
frame of an API sequence) read the sequence row and upsert the whole row
from that read in one transaction. On PostgreSQL (READ COMMITTED) a
PATCH accepted between the read and the upsert was written back over:
its settings were lost and settings_version went back, so the PC that
pushed them later pulled the old settings over its edit.

The row is now read with SequenceRepository.GetForUpdateAsync (FOR
UPDATE, as EditAsync takes it). A PATCH committed first is then moved by
the insertion like any position; one arriving later sees the new frames
version and gets 409 frames_conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 17:09:11 +00:00
1 parent b0c364539d
commit 20c08a9250
4 files changed
+46 -3

No files matched your search

@@ -167,6 +167,14 @@ public sealed class SequenceRepository
public Task<Sequence?> GetAsync(Guid id, CancellationToken cancellationToken = default) =>
_db.SingleAsync($"{_select} WHERE s.id = $1", [id], Map, cancellationToken);
/// <summary>
/// <see cref="GetAsync"/> 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 <see cref="EditAsync(Guid, SequenceEdit, int?, int?, CancellationToken)"/> takes the same lock.
/// </summary>
public Task<Sequence?> GetForUpdateAsync(Guid id, CancellationToken cancellationToken = default) =>
_db.SingleAsync($"{_select} WHERE s.id = $1{_db.Sql.ForUpdate}", [id], Map, cancellationToken);
/// <summary>Deletes the sequence with its frames, renders and tag links.</summary>
public async Task<bool> DeleteAsync(Guid id, CancellationToken cancellationToken = default) =>
await _db.ExecuteAsync("DELETE FROM sequences WHERE id = $1", [id], cancellationToken).ConfigureAwait(false) > 0;