fix(data): restore a sequence whose folder lost its sidecar

Restore left a folder without a sidecar out of the index until the next scan,
so Undo and the trash's Restore seemed to do nothing; such a folder is now
adopted under the sequence's own id, as a scan would adopt it, and comes back
at once with its settings and renders.

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-03 16:49:10 +00:00
1 parent b17d008f65
commit f4e1d02c1d
3 files changed
+39 -14

No files matched your search

+2 -2
View File
@@ -178,8 +178,8 @@ public sealed class ArchiveService : IJobHandlerProvider
public async Task<IReadOnlyList<TrashEntry>> ListTrashAsync(Guid archiveId, CancellationToken cancellationToken = default) =>
await Trash.ListAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
/// <summary>Moves a sequence back from the trash and indexes it again; null when its folder has no sidecar (a scan adopts it).</summary>
public async Task<Sequence?> RestoreAsync(Guid archiveId, string entryName, CancellationToken cancellationToken = default) =>
/// <summary>Moves a sequence back from the trash and indexes it again.</summary>
public async Task<Sequence> RestoreAsync(Guid archiveId, string entryName, CancellationToken cancellationToken = default) =>
await Trash.RestoreAsync(await LoadArchiveAsync(archiveId, cancellationToken).ConfigureAwait(false), entryName, cancellationToken).ConfigureAwait(false);
/// <summary>Plans a card or folder import into an archive (nothing is copied).</summary>
+15 -10
View File
@@ -185,12 +185,12 @@ public sealed class SequenceTrash
/// <summary>
/// Moves a sequence back from the trash into its folder (or, when that is taken, a free one next to it) and indexes
/// it again from its sidecar with its ids, settings and renders. Returns the restored row; null when the folder has
/// no sidecar (the next scan adopts it).
/// it again from its sidecar with its ids, settings and renders. A folder without a sidecar is adopted under the
/// sequence's id, as a scan would adopt it. Returns the restored row.
/// </summary>
/// <exception cref="InvalidOperationException">The index already holds that sequence.</exception>
/// <exception cref="DirectoryNotFoundException">The entry is not in the trash.</exception>
public async Task<Sequence?> RestoreAsync(Archive archive, string name, CancellationToken cancellationToken = default)
public async Task<Sequence> RestoreAsync(Archive archive, string name, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(archive);
ArgumentException.ThrowIfNullOrWhiteSpace(name);
@@ -214,12 +214,7 @@ public sealed class SequenceTrash
Directory.CreateDirectory(Path.GetDirectoryName(sequence.Folder)!);
Directory.Move(source, sequence.Folder);
Log.Info("archive", $"{record.Sequence.DisplayTitle} restored from the trash to {folder}");
if (!File.Exists(sequence.ManifestPath))
{
File.Delete(recordPath);
return null;
}
var manifest = SequenceWriter.ReadLocked(sequence, id);
var manifest = File.Exists(sequence.ManifestPath) ? SequenceWriter.ReadLocked(sequence, id) : Adopt(sequence, id, cancellationToken);
var kept = record.Sequence with { ArchiveId = archive.Id, Folder = folder, RenderedCount = 0 };
await using (var tx = await _db.BeginAsync(cancellationToken).ConfigureAwait(false))
{
@@ -233,7 +228,17 @@ public sealed class SequenceTrash
await _indexer.RecordStampsAsync(archive, sequence, id, cancellationToken).ConfigureAwait(false);
File.Delete(recordPath);
}
return await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false);
return await _db.Sequences.GetAsync(id, cancellationToken).ConfigureAwait(false)
?? throw new InvalidOperationException($"{record.Sequence.DisplayTitle} was restored but is not in the index.");
}
/// <summary>A sidecar for a restored folder that had none (its frames get new ids; the sequence keeps its own).</summary>
private SequenceManifest Adopt(SequencePaths sequence, Guid id, CancellationToken cancellationToken)
{
var manifest = SequenceAdopter.Build(id, sequence, _writer.Options.Owner, cancellationToken);
Directory.CreateDirectory(sequence.MetaDirectory);
ManifestStore.WriteSequence(sequence.ManifestPath, manifest);
return manifest;
}
private async Task<TrashEntry?> DeleteOneAsync(DeleteStep step, string? deletedBy, CancellationToken cancellationToken)
@@ -124,7 +124,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
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 restored = Assert.NotNull(await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct));
var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct);
Assert.Equal(victim.Folder, restored.Folder);
Assert.Equal(settings, restored.SettingsJson);
@@ -151,7 +151,7 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
Photos.WriteSeries(Path.Combine(fx.Paths.Root, "inbox", "c"), Start.AddDays(5), 2, firstSeed: 40);
await fx.ScanAsync();
var restored = Assert.NotNull(await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct));
var restored = await fx.Service.RestoreAsync(fx.Archive.Id, entry.Name, fx.Ct);
Assert.Equal("inbox/c (2)", restored.Folder);
var output = (await fx.Db.Renders.ListBySequenceAsync(victim.Id)).Single().OutputPath;
Assert.Equal("inbox/c (2)/exports/preview.mp4", output);
@@ -160,6 +160,26 @@ public abstract class SequenceTrashTests(TestContext context) : DatabaseTest(con
Assert.Count(4, await fx.Db.Sequences.ListLocationsAsync(fx.Archive.Id));
}
[Test]
public async Task AFolderThatLostItsSidecarComesBackUnderItsId()
{
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 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.Equal(victim.Folder, restored.Folder);
Assert.Equal(3, restored.FrameCount);
Assert.Count(3, await fx.Db.Frames.ListBySequenceAsync(victim.Id));
Assert.Equal(victim.Id, ManifestStore.ReadSequence(fx.Paths.Sequence(victim.Folder).ManifestPath).Id);
Assert.Equal(0, (await fx.ScanAsync()).Indexed, "the index and the new sidecar agree");
}
[Test]
public async Task SequencesThatCannotMoveAreBlockedAndUntouched()
{