fix(data): trash moves finish whatever is cancelled, archive-wide jobs hold their sequences
Once a folder has moved into the trash (or back), its index step runs to the end with no cancellation and a failed step moves the folder back, so trash and index never disagree. Running or paused archive-wide jobs (scan, rebuild, import, maintenance, a verify of everything) block a delete of every sequence of their archive; a verify of named sequences blocks those. A row whose folder is gone is refused (FolderMissing) instead of losing its settings. Delete revalidates the plan cheaply (index row, jobs, sidecar id from its head) instead of measuring every folder again; the dry run reports progress. A restored folder without a sidecar gets the record's title, tags, rating and notes and drops a hero frame it no longer has. EditSequenceInfoAsync changes one field from the current sidecar values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
4 files changed
+475
-135
No files matched your search
@@ -177,4 +177,22 @@ public sealed class ArchiveIndexTests(TestContext context)
|
||||
Assert.SequenceEqual(["Night"], (await fx.Db.Sequences.GetAsync(comet.Id))!.Tags);
|
||||
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnEditOfOneFieldKeepsWhatChangedSinceTheRowWasRead()
|
||||
{
|
||||
await using var fx = await CreateAsync(context);
|
||||
Populate(fx.Paths);
|
||||
await fx.ScanAsync();
|
||||
var comet = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single(l => l.Folder == "Trips/Comet");
|
||||
var stale = (await fx.Db.Sequences.GetAsync(comet.Id))!;
|
||||
var edited = await fx.Service.SetSequenceInfoAsync(comet.Id, "Comet 2026", ["night"], 2, "Tripod", fx.Ct);
|
||||
|
||||
var rated = await fx.Service.EditSequenceInfoAsync(stale.Id, info => info with { Rating = 5 }, fx.Ct);
|
||||
|
||||
Assert.Equal(("Comet 2026", "night", 5, "Tripod"), (rated.Title, rated.Tags.Single(), rated.Rating, rated.Notes));
|
||||
Assert.Equal(edited.SettingsVersion + 1, rated.SettingsVersion);
|
||||
var manifest = ManifestStore.ReadSequence(fx.Paths.Sequence(comet.Folder).ManifestPath);
|
||||
Assert.Equal(("Comet 2026", "night", 5, "Tripod"), (manifest.Title, manifest.Tags.Single(), manifest.Rating, manifest.Notes));
|
||||
}
|
||||
}
|
||||
@@ -57,6 +57,40 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
return render;
|
||||
}
|
||||
|
||||
private static Job Running(JobKind kind, IReadOnlyList<Guid> targets, string? parameters = null) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = kind,
|
||||
TargetIds = targets,
|
||||
ParametersJson = parameters,
|
||||
State = JobState.Running,
|
||||
NodeId = "test#0",
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
Started = DateTimeOffset.UtcNow,
|
||||
};
|
||||
|
||||
/// <summary>Makes the next write of <paramref name="operation"/> on the sequences table fail, as a lost database connection would.</summary>
|
||||
private async Task FailSequencesAsync(ArchiveFixture fx, string operation)
|
||||
{
|
||||
await using var session = await fx.Db.Database.OpenAsync(fx.Ct);
|
||||
await session.ExecuteScriptAsync(ForDialect(
|
||||
$"CREATE TRIGGER fail_{operation} BEFORE {operation} ON sequences BEGIN SELECT RAISE(ABORT, 'injected'); END;",
|
||||
$"CREATE FUNCTION fail_{operation}() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected'; END $$; " +
|
||||
$"CREATE TRIGGER fail_{operation} BEFORE {operation} ON sequences FOR EACH ROW EXECUTE FUNCTION fail_{operation}();"), fx.Ct);
|
||||
}
|
||||
|
||||
private async Task HealSequencesAsync(ArchiveFixture fx, string operation)
|
||||
{
|
||||
await using var session = await fx.Db.Database.OpenAsync(fx.Ct);
|
||||
await session.ExecuteScriptAsync(ForDialect($"DROP TRIGGER fail_{operation};", $"DROP TRIGGER fail_{operation} ON sequences;"), fx.Ct);
|
||||
}
|
||||
|
||||
/// <summary>Reports on the calling thread, so a test can act between two sequences.</summary>
|
||||
private sealed class Inline(Action<int> report) : IProgress<int>
|
||||
{
|
||||
public void Report(int value) => report(value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheDryRunCountsWhatMovesAndChangesNothing()
|
||||
{
|
||||
@@ -65,7 +99,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id);
|
||||
var folder = fx.Paths.Sequence(sequences[0].Folder).Folder;
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct);
|
||||
Assert.Empty(plan.Blocked);
|
||||
Assert.Count(2, plan.Steps);
|
||||
var (files, bytes) = OnDisk(folder);
|
||||
@@ -88,7 +122,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var folder = fx.Paths.Sequence(victim.Folder).Folder;
|
||||
var (files, bytes) = OnDisk(folder);
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct);
|
||||
var entries = await fx.Service.DeleteAsync(plan, "alberto", null, fx.Ct);
|
||||
|
||||
var entry = Assert.Contains(entries, e => e.SequenceId == victim.Id);
|
||||
@@ -123,7 +157,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id);
|
||||
var settings = (await fx.Db.Sequences.GetAsync(victim.Id))!.SettingsJson;
|
||||
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single();
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single();
|
||||
var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct);
|
||||
|
||||
Assert.Equal(victim.Folder, restored.Folder);
|
||||
@@ -136,7 +170,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
Assert.Equal(0, (await fx.ScanAsync()).Indexed);
|
||||
|
||||
// Deleted again: the trash takes it again under the same name.
|
||||
var again = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single();
|
||||
var again = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single();
|
||||
Assert.Equal(entry.Name, again.Name);
|
||||
}
|
||||
|
||||
@@ -147,7 +181,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var victim = sequences[2];
|
||||
await RenderAsync(fx, victim);
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single();
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single();
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "c"), Start.AddDays(5), 2, firstSeed: 40);
|
||||
await fx.ScanAsync();
|
||||
|
||||
@@ -165,14 +199,20 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var victim = sequences[1];
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], fx.Ct), null, null, fx.Ct)).Single();
|
||||
var victim = await fx.Service.SetSequenceInfoAsync(sequences[1].Id, "Ridge", ["fog"], 4, "Tripod", fx.Ct);
|
||||
var hero = (await fx.Db.Frames.ListBySequenceAsync(victim.Id)).Last().Id;
|
||||
await fx.Db.Sequences.UpsertAsync(victim with { HeroFrameId = hero }, fx.Ct);
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single();
|
||||
var trashed = fx.Paths.Sequence(fx.Paths.ToRelative(Path.Combine(SequenceTrash.TrashDirectory(fx.Paths), entry.Name)));
|
||||
File.Delete(trashed.ManifestPath);
|
||||
|
||||
var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct);
|
||||
|
||||
Assert.Equal(victim.Id, restored.Id);
|
||||
Assert.Null(restored.HeroFrameId, "the frames were adopted with new ids");
|
||||
var sidecar = ManifestStore.ReadSequence(fx.Paths.Sequence(victim.Folder).ManifestPath);
|
||||
Assert.Equal(("Ridge", "fog", 4, "Tripod"), (sidecar.Title, sidecar.Tags.Single(), sidecar.Rating, sidecar.Notes));
|
||||
Assert.Equal(("Ridge", 4), (restored.Title, restored.Rating));
|
||||
Assert.Equal(victim.Folder, restored.Folder);
|
||||
Assert.Equal(3, restored.FrameCount);
|
||||
Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(victim.Id));
|
||||
@@ -187,21 +227,11 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var sequences = await PopulateAsync(fx);
|
||||
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "a", "night"), Start.AddDays(3), 2, firstSeed: 30);
|
||||
await fx.ScanAsync();
|
||||
var running = new Job
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
Kind = JobKind.Proxies,
|
||||
TargetIds = [sequences[1].Id],
|
||||
State = JobState.Running,
|
||||
NodeId = "test#0",
|
||||
Created = DateTimeOffset.UtcNow,
|
||||
Started = DateTimeOffset.UtcNow,
|
||||
};
|
||||
await fx.Db.Jobs.EnqueueAsync(running);
|
||||
await fx.Db.Jobs.EnqueueAsync(Running(JobKind.Proxies, [sequences[1].Id]));
|
||||
// The folder of the third sequence now holds the sidecar of another sequence (copied over by hand).
|
||||
File.Copy(fx.Paths.Sequence(sequences[0].Folder).ManifestPath, fx.Paths.Sequence(sequences[2].Folder).ManifestPath, overwrite: true);
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], null, fx.Ct);
|
||||
Assert.Empty(plan.Steps);
|
||||
Assert.SequenceEqual([DeleteBlock.ContainsSequences, DeleteBlock.Busy, DeleteBlock.ForeignSidecar],
|
||||
plan.Blocked.OrderBy(b => b.Sequence.Folder, StringComparer.Ordinal).Select(b => b.Reason));
|
||||
@@ -217,7 +247,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
var location = (await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id)).Single();
|
||||
Assert.Equal(string.Empty, location.Folder);
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([location.Id], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([location.Id], null, fx.Ct);
|
||||
Assert.Equal(DeleteBlock.ArchiveRoot, plan.Blocked.Single().Reason);
|
||||
}
|
||||
|
||||
@@ -226,7 +256,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct);
|
||||
await fx.Service.ApplyRenameAsync(await fx.Service.PlanRenameAsync(fx.Archive.Id, [sequences[1].Id], "moved/{date}", null, fx.Ct), null, fx.Ct);
|
||||
|
||||
var stale = await Assert.ThrowsAsync<StaleDeletePlanException>(() => fx.Service.DeleteAsync(plan, null, null, fx.Ct));
|
||||
@@ -236,16 +266,136 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARowWithoutAFolderLeavesOnlyTheIndex()
|
||||
public async Task ARowWhoseFolderIsMissingStaysForTheOrphansCheck()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
Directory.Delete(fx.Paths.Sequence(sequences[0].Folder).Folder, recursive: true);
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id], fx.Ct);
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id], null, fx.Ct);
|
||||
Assert.Empty(plan.Steps);
|
||||
Assert.Equal(DeleteBlock.FolderMissing, plan.Blocked.Single().Reason);
|
||||
Assert.NotNull(await fx.Db.Sequences.GetAsync(sequences[0].Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ASequenceWithoutAFolderYetLosesOnlyItsRows()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var empty = new Sequence
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
ArchiveId = fx.Archive.Id,
|
||||
Title = "Waiting for photos",
|
||||
Folder = string.Empty,
|
||||
Origin = SequenceOrigin.Server,
|
||||
SyncState = SyncState.InSync,
|
||||
UpdatedAt = DateTimeOffset.UtcNow,
|
||||
};
|
||||
await fx.Db.Sequences.UpsertAsync(empty, fx.Ct);
|
||||
|
||||
var plan = await fx.Service.PlanDeleteAsync([empty.Id], null, fx.Ct);
|
||||
Assert.Null(plan.Steps.Single().TrashFolder);
|
||||
Assert.Empty(await fx.Service.DeleteAsync(plan, null, null, fx.Ct));
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(empty.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task JobsOnTheWholeArchiveHoldEverySequenceOfIt()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var ids = sequences.Select(s => s.Id).ToList();
|
||||
|
||||
// A verify of one named sequence holds only that one.
|
||||
var verifyOne = Running(JobKind.Verify, [fx.Archive.Id], ArchiveJobs.Verify(fx.Archive, [sequences[2].Id]).ParametersJson);
|
||||
await fx.Db.Jobs.EnqueueAsync(verifyOne);
|
||||
var plan = await fx.Service.PlanDeleteAsync(ids, null, fx.Ct);
|
||||
Assert.Equal(sequences[2].Id, plan.Blocked.Single(b => b.Reason == DeleteBlock.Busy).Sequence.Id);
|
||||
Assert.Count(2, plan.Steps);
|
||||
|
||||
// An import (whose folders only its plan knows) holds the whole archive, and so does a paused one.
|
||||
var import = Running(JobKind.Import, [fx.Archive.Id]) with { State = JobState.Paused };
|
||||
await fx.Db.Jobs.EnqueueAsync(import);
|
||||
plan = await fx.Service.PlanDeleteAsync(ids, null, fx.Ct);
|
||||
Assert.Empty(plan.Steps);
|
||||
Assert.True(plan.Blocked.All(b => b.Reason == DeleteBlock.Busy));
|
||||
|
||||
// A plan made before the job started is refused when it runs.
|
||||
await fx.Db.Jobs.SetStateAsync(import.Id, JobState.Done, null, fx.Ct);
|
||||
var before = await fx.Service.PlanDeleteAsync([sequences[0].Id], null, fx.Ct);
|
||||
await fx.Db.Jobs.EnqueueAsync(Running(JobKind.Scan, [fx.Archive.Id]));
|
||||
await Assert.ThrowsAsync<StaleDeletePlanException>(() => fx.Service.DeleteAsync(before, null, null, fx.Ct));
|
||||
Assert.True(Directory.Exists(fx.Paths.Sequence(sequences[0].Folder).Folder));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task WhenTheIndexCannotLetGoTheFolderComesBack()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var victim = sequences[0];
|
||||
var folder = fx.Paths.Sequence(victim.Folder).Folder;
|
||||
var before = await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id);
|
||||
var plan = await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct);
|
||||
|
||||
await FailSequencesAsync(fx, "DELETE");
|
||||
await Assert.ThrowsAsync<DatabaseException>(() => fx.Service.DeleteAsync(plan, null, null, fx.Ct));
|
||||
|
||||
Assert.True(Directory.Exists(folder), "the folder is back where it was");
|
||||
Assert.Equal(before, await Photos.DumpIndexAsync(fx.Db, fx.Archive.Id));
|
||||
Assert.Empty(Directory.EnumerateFileSystemEntries(SequenceTrash.TrashDirectory(fx.Paths)));
|
||||
await HealSequencesAsync(fx, "DELETE");
|
||||
Assert.Count(1, await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ARestoreThatCannotBeIndexedLeavesTheEntryInTheTrash()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var victim = sequences[1];
|
||||
var entry = (await fx.Service.DeleteAsync(await fx.Service.PlanDeleteAsync([victim.Id], null, fx.Ct), null, null, fx.Ct)).Single();
|
||||
|
||||
await FailSequencesAsync(fx, "INSERT");
|
||||
await Assert.ThrowsAsync<DatabaseException>(() => fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct));
|
||||
|
||||
Assert.False(Directory.Exists(fx.Paths.Sequence(victim.Folder).Folder));
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(victim.Id));
|
||||
Assert.Equal(entry.Name, (await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct)).Single().Name);
|
||||
await HealSequencesAsync(fx, "INSERT");
|
||||
Assert.Equal(victim.Folder, (await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct)).Folder);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CancellingStopsBetweenTwoWholeSequences()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var plan = await fx.Service.PlanDeleteAsync([sequences[0].Id, sequences[1].Id], null, fx.Ct);
|
||||
using var cancel = CancellationTokenSource.CreateLinkedTokenSource(fx.Ct);
|
||||
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() => fx.Service.DeleteAsync(plan, null, new Inline(_ => cancel.Cancel()), cancel.Token));
|
||||
|
||||
Assert.Null(await fx.Db.Sequences.GetAsync(sequences[0].Id));
|
||||
Assert.Count(1, await fx.Service.ListTrashAsync(fx.Archive.Id, fx.Ct));
|
||||
Assert.NotNull(await fx.Db.Sequences.GetAsync(sequences[1].Id));
|
||||
Assert.True(Directory.Exists(fx.Paths.Sequence(sequences[1].Folder).Folder));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ACancelledDryRunStopsAndReportsWhatItChecked()
|
||||
{
|
||||
var fx = await FixtureAsync();
|
||||
var sequences = await PopulateAsync(fx);
|
||||
var checkedCounts = new List<int>();
|
||||
var plan = await fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], new Inline(checkedCounts.Add), fx.Ct);
|
||||
Assert.SequenceEqual([1, 2, 3], checkedCounts);
|
||||
Assert.Count(3, plan.Steps);
|
||||
|
||||
using var cancel = CancellationTokenSource.CreateLinkedTokenSource(fx.Ct);
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(() =>
|
||||
fx.Service.PlanDeleteAsync([.. sequences.Select(s => s.Id)], new Inline(_ => cancel.Cancel()), cancel.Token));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user