From ecb763ea58ce577e4bca189b5516ccd1bcb5e0db Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 25 Jul 2026 15:30:24 +0200 Subject: [PATCH] =?UTF-8?q?fix(491):=20review=20polish=20=E2=80=94=20heal?= =?UTF-8?q?=20cannot=20abort=20a=20scan,=20ordinal=20settle=20unit-tested?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../Data/Repositories/LibraryRepository.cs | 42 +++++++++++++++-- .../LibraryFolderDedupeMigrationTests.cs | 13 +++++ .../Integration/LibraryRepositoryTests.cs | 47 +++++++++++++++++-- 3 files changed, 93 insertions(+), 9 deletions(-) diff --git a/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs b/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs index db09a7d12..b052bb7e8 100644 --- a/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs +++ b/ErsatzTV.Infrastructure/Data/Repositories/LibraryRepository.cs @@ -235,10 +235,13 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory 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 /// + /// + /// The in-memory half of , 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 = 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 Id first. + /// + public static LibraryFolder ResolveExact(IReadOnlyList 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 GetFolder(TvContext dbContext, int libraryPathId, string folder) { List candidates = await dbContext.LibraryFolders @@ -312,7 +344,7 @@ public class LibraryRepository(IFileSystem fileSystem, IDbContextFactory 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 maybeParentFolder, string folder) diff --git a/ErsatzTV.Tests/Integration/LibraryFolderDedupeMigrationTests.cs b/ErsatzTV.Tests/Integration/LibraryFolderDedupeMigrationTests.cs index dc3f598d4..cde81f1ba 100644 --- a/ErsatzTV.Tests/Integration/LibraryFolderDedupeMigrationTests.cs +++ b/ErsatzTV.Tests/Integration/LibraryFolderDedupeMigrationTests.cs @@ -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("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); diff --git a/ErsatzTV.Tests/Integration/LibraryRepositoryTests.cs b/ErsatzTV.Tests/Integration/LibraryRepositoryTests.cs index 36ee5142e..b24377468 100644 --- a/ErsatzTV.Tests/Integration/LibraryRepositoryTests.cs +++ b/ErsatzTV.Tests/Integration/LibraryRepositoryTests.cs @@ -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 + { + 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 + { + 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() {