feat(db): idempotent startup normalizer rewriting List values to native JSON

This commit is contained in:
Joseph Doherty
2026-06-16 17:50:19 -04:00
parent e3d804a1a6
commit f4b101b532
3 changed files with 360 additions and 0 deletions
@@ -0,0 +1,123 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.ScadaBridge.Commons.Types;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase;
/// <summary>
/// Idempotent central startup normalizer that rewrites already-persisted List attribute
/// values from the old array-of-strings JSON form (<c>["10","20"]</c>) to the new
/// native-typed form (<c>[10,20]</c>).
///
/// <para>
/// This is a safety net: at the time of writing there is no deployed List attribute data,
/// so in practice it finds nothing to rewrite. It runs on every central startup after
/// migrations apply and MUST never abort startup for data reasons — every row's work is
/// wrapped in a per-row try/catch that logs and skips on malformed data. A second run
/// finds nothing to change (native → native re-encode is byte-identical).
/// </para>
/// </summary>
public static class ListValueNormalizer
{
/// <summary>
/// Rewrites old-form List attribute values to the native-typed JSON form across
/// <see cref="ScadaBridgeDbContext.TemplateAttributes"/> and
/// <see cref="ScadaBridgeDbContext.InstanceAttributeOverrides"/>. Idempotent and
/// best-effort: malformed rows are logged and skipped, never rethrown.
/// </summary>
/// <param name="db">The configuration database context.</param>
/// <param name="logger">Optional logger for diagnostics.</param>
/// <param name="ct">Cancellation token.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
public static async Task NormalizeAsync(
ScadaBridgeDbContext db,
ILogger? logger = null,
CancellationToken ct = default)
{
var rewritten = 0;
// TemplateAttributes: List rows carry the element type on the row itself.
var templateRows = await db.TemplateAttributes
.Where(a => a.DataType == DataType.List)
.ToListAsync(ct);
foreach (var a in templateRows)
{
try
{
var native = AttributeValueCodec.Encode(
AttributeValueCodec.Decode(a.Value, DataType.List, a.ElementDataType));
if (native != a.Value)
{
a.Value = native;
rewritten++;
}
}
catch (FormatException ex)
{
logger?.LogWarning(ex,
"List value normalizer: skipping unparseable list value for TemplateAttribute {Id}.",
a.Id);
}
}
// InstanceAttributeOverrides: only rows that carry an element type are List rows.
// Rows with a null ElementDataType are scalar/legacy rows (no deployed List data
// exists, so none in practice) and are skipped.
var overrideRows = await db.InstanceAttributeOverrides
.Where(o => o.ElementDataType != null)
.ToListAsync(ct);
foreach (var o in overrideRows)
{
if (o.ElementDataType is null)
{
logger?.LogDebug(
"List value normalizer: skipping InstanceAttributeOverride {Id} with no element type.",
o.Id);
continue;
}
try
{
var native = AttributeValueCodec.Encode(
AttributeValueCodec.Decode(o.OverrideValue, DataType.List, o.ElementDataType));
if (native != o.OverrideValue)
{
o.OverrideValue = native;
rewritten++;
}
}
catch (FormatException ex)
{
logger?.LogWarning(ex,
"List value normalizer: skipping unparseable list value for InstanceAttributeOverride {Id}.",
o.Id);
}
}
try
{
await db.SaveChangesAsync(ct);
}
catch (Exception ex)
{
// A catastrophic DB failure on SaveChanges may propagate, but log it first so
// startup diagnostics are not silent. Per-row data problems are already handled
// above and never reach here.
logger?.LogError(ex, "List value normalizer: SaveChanges failed.");
throw;
}
if (rewritten > 0)
{
logger?.LogInformation(
"List value normalizer: rewrote {n} attribute value(s) to native JSON.", rewritten);
}
else
{
logger?.LogDebug("List value normalizer: no attribute values required rewriting.");
}
}
}
@@ -47,6 +47,12 @@ public static class MigrationHelper
"Apply migrations using 'dotnet ef database update' or the generated SQL scripts before starting in production mode.");
}
}
// Safety-net normalizer: rewrite any already-persisted List attribute values from the
// old array-of-strings JSON form to the new native-typed form. Idempotent and never
// aborts startup for data reasons (per-row skip + log). Safe even if both central
// nodes run it concurrently on startup.
await ListValueNormalizer.NormalizeAsync(dbContext, logger, cancellationToken);
}
private static async Task WaitForDatabaseReadyAsync(