fix(491): review polish — heal cannot abort a scan, ordinal settle unit-tested

Final low-severity items from the re-review of ee10f932.

L2: DbUpdateConcurrencyException derives from DbUpdateException but carries no
provider exception, so IsUniqueConstraintViolation does not classify it. A row
deleted by a concurrent library edit between the heal's read and its save would
propagate and fail the scan, contradicting the invariant stated directly above
it. Admit it in the filter.

L1: lift the in-memory ordinal settle into LibraryRepository.ResolveExact and
unit-test it with both spellings in the candidate list. No SQLite-backed test
can exercise it (SQLite's = on TEXT is already binary), so this converts the
half that rested on hand-run MySQL evidence into automated coverage. The
end-to-end companion test's comment no longer claims to be provider-independent.

L4: assert PRAGMA foreign_keys is 1 before migrating, so the enforcement guard
cannot silently degrade into the weak pre-fix form it was added to replace.

L3: detach the failed heal, matching the insert path.
N3: the heal's inner predicate now matches its IsNullOrEmpty outer guard, so a
PathHash = '' row cannot enter the branch and silently never heal.
N4: record that GetFolder returning null for a case-differing spelling makes
MySQL insert a second row where it used to reuse one — correct, and now matching
SQLite, but a real behaviour change on a case-insensitive filesystem.

Refs #488 #308
fix #491
This commit is contained in:
2026-07-25 21:13:30 +02:00
parent 14e9b03433
commit ecb763ea58
3 changed files with 93 additions and 9 deletions
@@ -235,10 +235,13 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory<TvConte
// (LibraryPathId, hash) — a legacy duplicate the migration's dedupe could not see (e.g. one
// with a NULL Path, which `NULL = NULL` excludes from its grouping).
string pathHash = PathUtils.GetPathHash(folder);
LibraryFolder tracked = null;
try
{
LibraryFolder tracked = await dbContext.LibraryFolders
.FirstOrDefaultAsync(f => f.Id == knownFolder.Id && f.PathHash == null);
// the predicate must agree with the IsNullOrEmpty guard above, or a PathHash = '' row would
// enter this branch, match nothing, and silently never heal
tracked = await dbContext.LibraryFolders
.FirstOrDefaultAsync(f => f.Id == knownFolder.Id && (f.PathHash == null || f.PathHash == ""));
if (tracked is not null)
{
tracked.PathHash = pathHash;
@@ -246,9 +249,18 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory<TvConte
knownFolder.PathHash = pathHash;
}
}
catch (DbUpdateException ex) when (TvContext.IsUniqueConstraintViolation(ex))
catch (DbUpdateException ex) when (
TvContext.IsUniqueConstraintViolation(ex) || ex is DbUpdateConcurrencyException)
{
// another row already owns this hash — leave this one unhealed rather than fail the scan
// Either another row already owns this hash, or the row was deleted out from under us by a
// concurrent library edit (DbUpdateConcurrencyException derives from DbUpdateException but
// carries no provider exception, so the classifier does NOT recognize it). Both mean "the
// heal is moot" — leave the row unhealed rather than fail the scan, per the invariant above.
if (tracked is not null)
{
// drop the failed change so it cannot be replayed by a later save on this context
dbContext.Entry(tracked).State = EntityState.Detached;
}
}
}
@@ -304,6 +316,26 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory<TvConte
/// can flip it), which would make the heal non-idempotent.
/// </para>
/// </summary>
/// <summary>
/// The in-memory half of <see cref="GetFolder" />, lifted out so the ordinal decision is pinned by a
/// test with no database at all: the collation behaviour that makes it necessary is MySQL-only, so a
/// SQLite-backed test cannot exercise it (SQLite's <c>=</c> on TEXT is already binary and never
/// returns the case-differing candidate). Given the candidates a case-INsensitive server may return,
/// pick the one whose path matches ordinally, lowest <c>Id</c> first.
/// </summary>
public static LibraryFolder ResolveExact(IReadOnlyList<LibraryFolder> candidates, string folder)
{
for (var i = 0; i < candidates.Count; i++)
{
if (string.Equals(candidates[i].Path, folder, StringComparison.Ordinal))
{
return candidates[i];
}
}
return null;
}
private static async Task<LibraryFolder> GetFolder(TvContext dbContext, int libraryPathId, string folder)
{
List<LibraryFolder> candidates = await dbContext.LibraryFolders
@@ -312,7 +344,7 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory<TvConte
.OrderBy(f => f.Id)
.ToListAsync();
return candidates.Find(f => string.Equals(f.Path, folder, StringComparison.Ordinal));
return ResolveExact(candidates, folder);
}
private static LibraryFolder CreateNewFolder(LibraryPath libraryPath, Option<int> maybeParentFolder, string folder)
@@ -111,6 +111,7 @@ public class LibraryFolderDedupeMigrationTests
await using (TvContext context = MigrateContext())
{
await AssertForeignKeysEnforced(context);
await context.Database.MigrateAsync();
}
@@ -192,6 +193,7 @@ public class LibraryFolderDedupeMigrationTests
await using (TvContext context = MigrateContext())
{
await AssertForeignKeysEnforced(context);
await context.Database.MigrateAsync();
}
@@ -209,6 +211,17 @@ public class LibraryFolderDedupeMigrationTests
}
}
// The `Foreign Keys=True` keyword above is the whole point of splitting the contexts, so prove it took
// effect rather than trusting the connection string: a typo or a pragma reset would silently revert
// this test to its weaker "enforcement off" form and still pass every assertion.
private static async Task AssertForeignKeysEnforced(TvContext context)
{
(await context.Connection.ExecuteScalarAsync<long>("PRAGMA foreign_keys")).ShouldBe(
1,
"the migration must run with foreign keys ENFORCED — otherwise DELETE FROM LibraryFolder is "
+ "not actually tested against the Restrict constraints it must not violate");
}
private TvContext SeedContext() => Create(_seedOptions);
private TvContext MigrateContext() => Create(_migrateOptions);
@@ -191,10 +191,49 @@ public class LibraryRepositoryTests
healed.PathHash.ShouldBe(PathUtils.GetPathHash("/data/music/artist1"));
}
// ersatztv#491 / H1: the lookup narrows with a SQL equality that is case-INsensitive on MySQL, while
// PathHash is a case-SENSITIVE hash. Identity must be settled ordinally, or a scan of "/x/foo" can
// resolve the "/x/Foo" row and stamp the wrong hash onto it. Reproduced provider-independently by
// making the two rows exist and asserting the exact row is chosen and healed.
// ersatztv#491 / H1: on MySQL the lookup's SQL equality is case-INsensitive (longtext under the
// default collation), so it hands back BOTH "/x/Foo" and "/x/foo" for a "/x/foo" scan — verified on a
// real MySQL 8.4 server, where an unordered LIMIT 1 returns "/x/Foo". PathHash is a case-SENSITIVE
// hash, so identity has to be settled ordinally or the heal stamps the wrong path's hash onto a row.
//
// This pins that decision with no database at all, because no SQLite-backed test can: SQLite's `=` on
// TEXT is binary, so the case-differing candidate never reaches the in-memory step. Feeding the
// candidate list directly is the only way to exercise it in CI.
[Test]
public void ResolveExact_Should_Pick_The_Ordinal_Match_Not_A_Case_Variant()
{
var candidates = new List<LibraryFolder>
{
new() { Id = 10, Path = "/x/Foo" },
new() { Id = 11, Path = "/x/foo" }
};
LibraryRepository.ResolveExact(candidates, "/x/foo").Id.ShouldBe(11);
LibraryRepository.ResolveExact(candidates, "/x/Foo").Id.ShouldBe(10);
// a spelling that matches nothing ordinally is absent, not "close enough"
LibraryRepository.ResolveExact(candidates, "/x/FOO").ShouldBeNull();
LibraryRepository.ResolveExact([], "/x/foo").ShouldBeNull();
}
// Candidates arrive ordered by Id, and the first ordinal match wins — so true duplicates resolve to
// MIN(Id), the same row the #491 migration keeps.
[Test]
public void ResolveExact_Should_Prefer_The_First_Candidate_On_A_True_Duplicate()
{
var candidates = new List<LibraryFolder>
{
new() { Id = 3, Path = "/x/Foo" },
new() { Id = 7, Path = "/x/foo" },
new() { Id = 9, Path = "/x/foo" }
};
LibraryRepository.ResolveExact(candidates, "/x/foo").Id.ShouldBe(7);
}
// End-to-end companion to the ResolveExact tests above. On SQLite this passes on pre-fix code too (the
// SQL equality already excludes the case variant); its value is guarding the in-memory step against
// later being relaxed to OrdinalIgnoreCase, and covering the heal/no-duplicate behaviour around it.
[Test]
public async Task GetOrAddFolder_Should_Not_Resolve_A_Folder_Differing_Only_In_Case()
{