Files
Chronos/tests/Chronos.Data.Tests/Migrations/MigrationTests.cs
T
Alby96 51782a3ada feat(archive): photo delete, tombstones, trash kinds and emptying, library API
Owner review 2, R6: the web panel manages the library without editing it, so
the engine and the server must delete single photos safely, keep them out of
sync afterwards, and let an administrator restore or finally erase the trash.

- Migration 0008: frame_removals (tombstones by sequence and SHA-256, reason,
  replacement); the sidecar gains "removed" (omitted when empty) and frames a
  reserved "source"; the indexer writes the tombstones on every sync, so a
  rebuild keeps them, and drops a hero frame that is gone.
- FrameTrash: dry run (busy, frames changed, offline, would empty), delete under
  the sequence lease with a write-ahead journal that the next writer undoes
  after a crash, files to trash/photos/<seq>-<time>/originals with a record,
  sidecar without the frames plus tombstones, index sync clearing analyses,
  thumbnails and proxies, proxies renamed to follow their frames, follow-up
  jobs. Restore re-hashes every file, keeps mismatches in the trash, puts the
  same frame records back and drops their tombstones, or files the photos into
  a new sequence through the ingest when theirs is gone.
- Project positions on vanished photos are translated by photo and wait as
  pending frames, so they come back with a restore.
- Trash entries have a kind (sequence, photos, replaced); emptying is a dry run
  with a fingerprint and a new EmptyTrash job (18) that erases exactly the
  planned entries, refuses a stale plan and erases replaced originals only
  while their replacements still hash right.
- Sync honours tombstones: nothing one side deleted is offered back, local
  files are never deleted, replaced photos count as their replacement; the
  server answers an upload of a tombstoned SHA-256 as already there.
- Server: sequence thumbnail, photo delete, bulk delete/retag/jobs, trash
  list/restore/empty endpoints with their error messages; a verify of
  sequences now targets their archive (POST /jobs failed before).
- Desktop trash dialog lists photo entries and empties the trash after a
  typed confirmation through the same job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 35bb56690a1d69eae57153d036e1409201cd73f7)
2026-10-27 10:49:34 +01:00

96 lines
4.5 KiB
C#

using Chronos.Data.Migrations;
using Chronos.Data.Tests.Infrastructure;
using Chronos.Testing;
namespace Chronos.Data.Tests.Migrations;
public abstract class MigrationTests(TestContext context) : RepositoryTest(context)
{
private static readonly string[] Tables =
[
"archives", "sequences", "frames", "renders", "jobs", "media_cards", "import_history", "users", "tokens",
"render_nodes", "settings", "tags", "sequence_tags", "audit_log", "uploads", "schedules", "frame_removals",
];
[Test]
public void EmbeddedScriptsAreFoundForBothDialects()
{
foreach (var dialect in new[] { Dialect.Sqlite, Dialect.Postgres })
{
var migrations = MigrationRunner.Load(dialect);
Assert.NotEmpty(migrations);
Assert.Equal(1, migrations[0].Version);
Assert.Equal("initial", migrations[0].Name);
Assert.Contains("CREATE TABLE frames", migrations[0].Sql);
}
}
[Test]
public async Task CreatesEveryTableAndIsIdempotent()
{
var chronos = await OpenChronosAsync();
var latest = MigrationRunner.Load(Dialect)[^1].Version;
Assert.Equal(latest, chronos.SchemaVersion);
Assert.Equal(latest, await MigrationRunner.MigrateAsync(chronos.Database, Ct));
await using var session = await chronos.Database.OpenAsync(Ct);
foreach (var table in Tables) Assert.Equal(0L, await session.ScalarAsync<long>($"SELECT count(*) FROM {table}", [], Ct));
var recorded = await session.QueryListAsync("SELECT version, name, applied_at FROM schema_migrations ORDER BY version", [], r => r, Ct);
Assert.Equal(MigrationRunner.Load(Dialect).Count, recorded.Count);
Assert.Equal("initial", recorded[0].GetString(1));
Assert.True(recorded[0].GetDateTimeOffset(2) > DateTimeOffset.UtcNow.AddMinutes(-5));
}
[Test]
public async Task AppliesOnlyPendingMigrations()
{
var chronos = await OpenChronosAsync();
var next = MigrationRunner.Load(Dialect).Append(new Migration(9000, "extra", "CREATE TABLE extra (id integer PRIMARY KEY)")).ToList();
Assert.Equal(9000, await MigrationRunner.MigrateAsync(chronos.Database, next, Ct));
Assert.Equal(9000, await MigrationRunner.MigrateAsync(chronos.Database, next, Ct));
await using var session = await chronos.Database.OpenAsync(Ct);
Assert.Equal(1L, await session.ScalarAsync<long>("SELECT count(*) FROM schema_migrations WHERE version = $1", [9000], Ct));
Assert.Equal(0L, await session.ScalarAsync<long>("SELECT count(*) FROM extra", [], Ct));
}
[Test]
public async Task RefusesADatabaseFromANewerBuild()
{
var chronos = await OpenChronosAsync();
await using (var session = await chronos.Database.OpenAsync(Ct))
await session.ExecuteAsync("INSERT INTO schema_migrations (version, name, applied_at) VALUES ($1, $2, $3)", [9999, "future", DateTimeOffset.UtcNow], Ct);
var error = await Assert.ThrowsAsync<SchemaTooNewException>(() => MigrationRunner.MigrateAsync(chronos.Database, Ct));
Assert.Equal(9999, error.DatabaseVersion);
Assert.Equal(MigrationRunner.Load(Dialect)[^1].Version, error.SupportedVersion);
}
[Test]
public async Task AFailingMigrationLeavesNothingBehind()
{
var database = await OpenDatabaseAsync();
List<Migration> broken =
[
new(1, "good", "CREATE TABLE good_one (id integer PRIMARY KEY)"),
new(2, "bad", "CREATE TABLE good_two (id integer PRIMARY KEY); THIS IS NOT SQL"),
];
await Assert.ThrowsAsync<DatabaseException>(() => MigrationRunner.MigrateAsync(database, broken, Ct));
await using var session = await database.OpenAsync(Ct);
var leftovers = await session.ScalarAsync<long>(ForDialect(
"SELECT count(*) FROM sqlite_master WHERE name IN ('good_one', 'good_two', 'schema_migrations')",
"SELECT count(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name IN ('good_one', 'good_two', 'schema_migrations')"),
[], Ct);
Assert.Equal(0L, leftovers);
}
}
public sealed class SqliteMigrationTests(TestContext context) : MigrationTests(context)
{
protected override Backend Backend => Backend.SqliteMemory;
}
[RequiresEnvironment(PgTestServer.Variable)]
public sealed class PostgresMigrationTests(TestContext context) : MigrationTests(context)
{
protected override Backend Backend => Backend.Postgres;
}