Merge pull request #17042 from Shadowghost/linked-children-local-alternate-priority
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
Format / format-check (push) Waiting to run
Tests / run-tests (macos-latest) (push) Waiting to run
Tests / run-tests (ubuntu-latest) (push) Waiting to run
Tests / run-tests (windows-latest) (push) Waiting to run
OpenAPI Publish / OpenAPI - Publish Artifact (push) Waiting to run
OpenAPI Publish / OpenAPI - Publish Unstable Spec (push) Blocked by required conditions
OpenAPI Publish / OpenAPI - Publish Stable Spec (push) Blocked by required conditions
Project Automation / Project board (push) Waiting to run
Merge Conflict Labeler / main (push) Waiting to run

Prefer local alternate versions when deduplicating linked children
This commit is contained in:
Bond-009
2026-06-08 19:41:39 +02:00
committed by GitHub
2 changed files with 32 additions and 1 deletions

View File

@@ -557,9 +557,11 @@ public class ItemPersistenceService : IItemPersistenceService
}
}
// Deduplicate; local (file-based) relationships take priority over linked (user-merged)
// ones, matching the LinkedChildren migration.
newLinkedChildren = newLinkedChildren
.GroupBy(c => c.ChildId)
.Select(g => g.Last())
.Select(g => g.OrderBy(c => c.Type == LinkedChildType.LocalAlternateVersion ? 0 : 1).First())
.ToList();
var childIdsToCheck = newLinkedChildren.Select(c => c.ChildId).ToList();

View File

@@ -223,6 +223,35 @@ internal class MigrateLinkedChildren : IDatabaseMigrationRoutine
toInsert = toInsert.Where(lc => existingChildIds.Contains(lc.ChildId)).ToList();
// Drop linked (user-merged) entries that point at items the parent owns (local
// file-based alternates or extras). These stem from legacy data that merged an
// owned item onto its own primary and would wrongly mark server-merged groups
// as user-merged (splittable).
var linkedChildIds = toInsert
.Where(lc => lc.ChildType == LinkedChildType.LinkedAlternateVersion)
.Select(lc => lc.ChildId)
.Distinct()
.ToList();
if (linkedChildIds.Count > 0)
{
var ownerIdByChildId = context.BaseItems
.WhereOneOrMany(linkedChildIds, b => b.Id)
.Where(b => b.OwnerId.HasValue)
.Select(b => new { b.Id, b.OwnerId })
.ToDictionary(b => b.Id, b => b.OwnerId!.Value);
var removedCount = toInsert.RemoveAll(lc =>
lc.ChildType == LinkedChildType.LinkedAlternateVersion
&& ownerIdByChildId.TryGetValue(lc.ChildId, out var ownerId)
&& ownerId.Equals(lc.ParentId));
if (removedCount > 0)
{
_logger.LogInformation("Skipped {Count} LinkedAlternateVersion records pointing at items owned by their parent.", removedCount);
}
}
context.LinkedChildren.AddRange(toInsert);
context.SaveChanges();