fix(transport): template Overwrite clears compositions and cascades removed slots' derived templates (TRN-4, TRN-5, WP2 task 2.6)

TRN-4: the composition rewire skipped any bundle template with zero
compositions before the clear ran, so an Overwrite never removed the target's
slots although the preview reported the removals. It now skips only an
Add/Rename (nothing to clear).

TRN-5: a slot the bundle removes is deleted through
TemplateService.DeleteCompositionAsync, which cascades the slot-owned derived
template (no DB relationship on OwnerCompositionId, so the row-only delete
orphaned it and blocked deleting its base). A slot the bundle re-composes keeps
the row-only delete so a pre-#118 bundle that re-composes the slot onto its
derived template by name still resolves. Tests run the real importer against
an EF context and fail on the previous importer.
This commit is contained in:
Joseph Doherty
2026-12-03 09:55:53 -05:00
parent fe60c4a1dc
commit cbdd9fc40b
3 changed files with 243 additions and 2 deletions
@@ -3346,7 +3346,11 @@ public sealed partial class BundleImporter : IBundleImporter
{
var resolution = ResolveOrDefault(resolutionMap, "Template", dto.Name);
if (resolution.Action == ResolutionAction.Skip) continue;
if (dto.Compositions.Count == 0) continue;
// TRN-4: a bundle template with NO compositions must still run the
// clear below when it overwrites an existing target row — the source
// removed every slot, and the diff reports those removals. Only an
// Add/Rename (a brand-new row with nothing to clear) can be skipped.
if (dto.Compositions.Count == 0 && resolution.Action != ResolutionAction.Overwrite) continue;
var importedName = resolution.Action == ResolutionAction.Rename
? (resolution.RenameTo ?? dto.Name)
@@ -3378,9 +3382,42 @@ public sealed partial class BundleImporter : IBundleImporter
var existingComps = parent.Compositions
.Where(c => derivedSlots is null || !derivedSlots.Contains(c.InstanceName))
.ToList();
// TRN-5: a slot row owns its slot-derived template (OwnerCompositionId,
// no database relationship), so deleting the row alone orphaned the
// derived template with a live ParentTemplateId that then blocked
// deleting its base. A slot the bundle REMOVES (its instance name is
// not among the bundle's compositions for this owner, and no bundle
// composition names its composed template) is deleted through
// TemplateService.DeleteCompositionAsync, which cascades the slot-owned
// derived templates (it saves inside the import's transaction, as the
// derived-template pass already does). A slot the bundle re-composes
// keeps the row-only delete: a pre-#118 bundle re-composes the slot
// onto that very derived template by name, so it must survive.
var bundleSlotNames = dto.Compositions
.Select(c => c.InstanceName)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
var bundleComposedNames = dto.Compositions
.Where(c => !string.IsNullOrEmpty(c.ComposedTemplateName))
.Select(c => c.ComposedTemplateName!)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
TemplateService? compositionService = null;
foreach (var existing in existingComps)
{
await _templateRepo.DeleteTemplateCompositionAsync(existing.Id, ct).ConfigureAwait(false);
var removedSlot = !bundleSlotNames.Contains(existing.InstanceName)
&& await IsRemovableSlotDerivedAsync(existing, bundleComposedNames, ct).ConfigureAwait(false);
if (!removedSlot)
{
await _templateRepo.DeleteTemplateCompositionAsync(existing.Id, ct).ConfigureAwait(false);
continue;
}
compositionService ??= new TemplateService(_templateRepo, _auditService);
var deleted = await compositionService.DeleteCompositionAsync(existing.Id, user, ct).ConfigureAwait(false);
if (!deleted.IsSuccess)
{
throw new InvalidOperationException(
$"Could not remove composition '{parent.Name}.{existing.InstanceName}': {deleted.Error}");
}
}
if (derivedSlots is null)
{
@@ -3602,6 +3639,26 @@ public sealed partial class BundleImporter : IBundleImporter
/// looked up by each stub's id: a database collation that matches names
/// case-insensitively can only add rows, never swap the exact one out.
/// </summary>
/// <summary>
/// TRN-5: true when <paramref name="composition"/> owns a slot-derived template
/// (<c>IsDerived</c> with <c>OwnerCompositionId</c> pointing back at the row) that no
/// bundle composition of the same owner names — i.e. deleting the slot must cascade
/// the derived template. A slot that composes a base directly (pre-derived data) has
/// nothing to cascade and keeps the plain row delete.
/// </summary>
private async Task<bool> IsRemovableSlotDerivedAsync(
TemplateComposition composition, IReadOnlySet<string> bundleComposedNames, CancellationToken ct)
{
var composed = await _dbContext.Templates.AsNoTracking()
.Where(t => t.Id == composition.ComposedTemplateId)
.Select(t => new { t.Name, t.IsDerived, t.OwnerCompositionId })
.FirstOrDefaultAsync(ct).ConfigureAwait(false);
return composed is not null
&& composed.IsDerived
&& composed.OwnerCompositionId == composition.Id
&& !bundleComposedNames.Contains(composed.Name);
}
private async Task<Dictionary<int, Template>> LoadTemplatesWithChildrenAsync(
IEnumerable<Template> stubs, CancellationToken ct)
{