fix(transport): case-insensitive name lookups inside a record on import (ScadaBridge#100)

Item 2 of #100, following the #93/#95 pattern (ToNameLookup: OrdinalIgnoreCase,
first wins, never throws):

- Overwrite child sync matches template attributes, alarms, scripts and native
  alarm sources, and external-system methods, case-insensitively, like their
  (parent, Name) unique indexes under SQL Server's collation. A member that
  differs only in case is updated in place instead of deleted and re-added
  (which lost the row id and could collide with the unique index).
- ResolveAlarmScriptLinksAsync resolves the alarm and its on-trigger script
  case-insensitively; AlarmsEqual compares the script name the same way.
- ArtifactDiff (preview) matches child collections case-insensitively so it
  agrees with the sync; notification recipients keep the ordinal match (no
  unique index, replaced wholesale).
- The rewire passes found the template a DTO was imported onto with an
  ordinal index. Since #93 an Overwrite can match a differently-cased target and
  keeps its spelling, so the passes silently skipped it and the alarm on-trigger
  links the sync had cleared stayed null. TryGetImported adds a case-insensitive
  (base-first) fallback; composed/base template references keep the exact
  lookup (item 1 of #100 is still undecided).

Test: ImportNameLookupTests.Members_inside_a_record_match_case_insensitively,
over a proxy that gives the by-name template reads SQL Server's collation.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 15:09:18 -05:00
parent f5eed5e8fd
commit 6ff5efd3f4
4 changed files with 228 additions and 34 deletions
@@ -253,7 +253,8 @@ public sealed class ArtifactDiff
// number added/changed on an existing recipient is a real diff.
(e, i) => e.EmailAddress == i.EmailAddress && e.PhoneNumber == i.PhoneNumber,
"Recipients",
changes);
changes,
ignoreCase: false);
return BuildItem("NotificationList", incoming.Name, changes);
}
@@ -622,12 +623,19 @@ public sealed class ArtifactDiff
Func<TIncoming, string> incomingKey,
Func<TExisting, TIncoming, bool> equal,
string childCategory,
List<FieldChange> changes)
List<FieldChange> changes,
bool ignoreCase = true)
{
var existingByName = existing.GroupBy(existingKey, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
var incomingByName = incoming.GroupBy(incomingKey, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
// Child names are unique per parent case-insensitively (the DB unique indexes
// under SQL Server's collation), and the Overwrite sync matches them that way, so
// the diff does too: a case-only difference is the same row (ScadaBridge#100).
// Notification recipients have no such index and are replaced wholesale, so they
// keep the ordinal match (ignoreCase: false).
var comparer = ignoreCase ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
var existingByName = existing.GroupBy(existingKey, comparer)
.ToDictionary(g => g.Key, g => g.First(), comparer);
var incomingByName = incoming.GroupBy(incomingKey, comparer)
.ToDictionary(g => g.Key, g => g.First(), comparer);
var added = incomingByName.Keys.Where(k => !existingByName.ContainsKey(k)).OrderBy(k => k, StringComparer.Ordinal).ToList();
var removed = existingByName.Keys.Where(k => !incomingByName.ContainsKey(k)).OrderBy(k => k, StringComparer.Ordinal).ToList();
@@ -659,10 +667,9 @@ public sealed class ArtifactDiff
IEnumerable<TemplateScriptDto> incoming,
List<FieldChange> changes)
{
var existingByName = existing.GroupBy(s => s.Name, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
var incomingByName = incoming.GroupBy(s => s.Name, StringComparer.Ordinal)
.ToDictionary(g => g.Key, g => g.First(), StringComparer.Ordinal);
// Case-insensitive, as DiffChildren (ScadaBridge#100).
var existingByName = BundleImporter.ToNameLookup(existing, s => s.Name);
var incomingByName = BundleImporter.ToNameLookup(incoming, s => s.Name);
foreach (var name in incomingByName.Keys.Where(k => !existingByName.ContainsKey(k)).OrderBy(k => k, StringComparer.Ordinal))
{