feat(sites): record the artifact revision last deployed to a site

Site.ArtifactRevisionHash / ArtifactsDeployedAt (nullable, migration
AddSiteArtifactRevision) so the Sites page can tell whether a site's
system-wide artifacts are current. Not transported. Adds the
GetDeployedRevisionHashesAsync projection for the fleet-wide staleness batch.

Claude-Session: https://claude.ai/code/session_01Y5ef34iUUMjmwnMpL9xgmB
This commit is contained in:
Joseph Doherty
2026-09-14 13:33:59 -04:00
parent 0762d69132
commit dd202e5dde
9 changed files with 2333 additions and 4 deletions
@@ -18,6 +18,18 @@ public class Site
public string? GrpcNodeAAddress { get; set; }
/// <summary>gRPC endpoint for site node B used by the central SiteStreamGrpcClient.</summary>
public string? GrpcNodeBAddress { get; set; }
/// <summary>
/// Revision hash (<c>ArtifactRevisionHashService</c>) of the per-site artifact set last
/// deployed to this site successfully; null = artifacts never deployed. Environment-specific
/// deploy state — deliberately NOT carried by Transport bundles (like <c>Instance.State</c>).
/// </summary>
public string? ArtifactRevisionHash { get; set; }
/// <summary>
/// UTC instant at which the artifact set identified by <see cref="ArtifactRevisionHash"/> was
/// last deployed to this site successfully; null = artifacts never deployed. Environment-specific
/// deploy state — deliberately NOT carried by Transport bundles (like <c>Instance.State</c>).
/// </summary>
public DateTimeOffset? ArtifactsDeployedAt { get; set; }
/// <summary>
/// Initializes a new site with the required name and identifier.
@@ -204,6 +204,14 @@ public interface IDeploymentManagerRepository
/// <returns>The deployed config snapshot, or null if not found.</returns>
Task<DeployedConfigSnapshot?> GetDeployedSnapshotByInstanceIdAsync(int instanceId, CancellationToken cancellationToken = default);
/// <summary>
/// InstanceId → RevisionHash for every <c>DeployedConfigSnapshot</c>. A projection read — never
/// materialises <c>ConfigurationJson</c>, so it is safe to call for the whole fleet (the
/// pending-updates aggregate does, once per cache TTL).
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
/// <returns>A dictionary keyed by instance ID; instances with no snapshot are absent.</returns>
Task<IReadOnlyDictionary<int, string>> GetDeployedRevisionHashesAsync(CancellationToken cancellationToken = default);
/// <summary>
/// Adds a new deployed config snapshot.
/// </summary>
/// <param name="snapshot">The deployed config snapshot to add.</param>
@@ -27,6 +27,7 @@ public class SiteConfiguration : IEntityTypeConfiguration<Site>
builder.Property(s => s.NodeBAddress).HasMaxLength(500);
builder.Property(s => s.GrpcNodeAAddress).HasMaxLength(500);
builder.Property(s => s.GrpcNodeBAddress).HasMaxLength(500);
builder.Property(s => s.ArtifactRevisionHash).HasMaxLength(100);
builder.HasIndex(s => s.Name).IsUnique();
builder.HasIndex(s => s.SiteIdentifier).IsUnique();
@@ -0,0 +1,40 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
{
/// <inheritdoc />
public partial class AddSiteArtifactRevision : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ArtifactRevisionHash",
table: "Sites",
type: "nvarchar(100)",
maxLength: 100,
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "ArtifactsDeployedAt",
table: "Sites",
type: "datetimeoffset",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ArtifactRevisionHash",
table: "Sites");
migrationBuilder.DropColumn(
name: "ArtifactsDeployedAt",
table: "Sites");
}
}
}
@@ -1379,6 +1379,13 @@ namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Migrations
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("ArtifactRevisionHash")
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<DateTimeOffset?>("ArtifactsDeployedAt")
.HasColumnType("datetimeoffset");
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("nvarchar(2000)");
@@ -294,6 +294,17 @@ public class DeploymentManagerRepository : IDeploymentManagerRepository
.FirstOrDefaultAsync(s => s.InstanceId == instanceId, cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyDictionary<int, string>> GetDeployedRevisionHashesAsync(CancellationToken cancellationToken = default)
{
// Projection only: ConfigurationJson is never materialised, so this stays cheap enough to
// read for the whole fleet in one round trip.
return await _dbContext.Set<DeployedConfigSnapshot>()
.AsNoTracking()
.Select(s => new { s.InstanceId, s.RevisionHash })
.ToDictionaryAsync(s => s.InstanceId, s => s.RevisionHash, cancellationToken);
}
/// <inheritdoc />
public async Task AddDeployedSnapshotAsync(DeployedConfigSnapshot snapshot, CancellationToken cancellationToken = default)
{
@@ -0,0 +1,89 @@
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Instances;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Sites;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Templates;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Enums;
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests;
/// <summary>
/// Coverage for <c>IDeploymentManagerRepository.GetDeployedRevisionHashesAsync</c> — the
/// fleet-wide InstanceId → RevisionHash projection the pending-updates aggregate reads once
/// per cache TTL. Uses the shared SQLite in-memory fixture with real FK enforcement.
/// </summary>
public class DeployedRevisionHashesRepositoryTests : IDisposable
{
private readonly ScadaBridgeDbContext _context;
private readonly DeploymentManagerRepository _repository;
/// <summary>Creates the in-memory context and the repository under test.</summary>
public DeployedRevisionHashesRepositoryTests()
{
_context = SqliteTestHelper.CreateInMemoryContext();
_repository = new DeploymentManagerRepository(_context);
}
/// <summary>Closes and disposes the in-memory context.</summary>
public void Dispose()
{
_context.Database.CloseConnection();
_context.Dispose();
GC.SuppressFinalize(this);
}
private async Task<int> SeedInstanceAsync(string uniqueName)
{
var site = new Site($"Site-{uniqueName}", $"SI-{uniqueName}");
var template = new Template($"T-{uniqueName}");
_context.Sites.Add(site);
_context.Templates.Add(template);
await _context.SaveChangesAsync();
var instance = new Instance(uniqueName)
{
SiteId = site.Id,
TemplateId = template.Id,
State = InstanceState.Enabled,
};
_context.Instances.Add(instance);
await _context.SaveChangesAsync();
return instance.Id;
}
private async Task SeedSnapshotAsync(int instanceId, string deploymentId, string revisionHash)
{
var snapshot = new DeployedConfigSnapshot(deploymentId, revisionHash, "{\"big\":\"payload\"}")
{
InstanceId = instanceId,
};
_context.Set<DeployedConfigSnapshot>().Add(snapshot);
await _context.SaveChangesAsync();
}
[Fact]
public async Task GetDeployedRevisionHashes_returns_every_snapshot_keyed_by_instance()
{
var first = await SeedInstanceAsync("Pump-01");
var second = await SeedInstanceAsync("Pump-02");
await SeedSnapshotAsync(first, "dep-1", "hash-one");
await SeedSnapshotAsync(second, "dep-2", "hash-two");
var hashes = await _repository.GetDeployedRevisionHashesAsync();
Assert.Equal(2, hashes.Count);
Assert.Equal("hash-one", hashes[first]);
Assert.Equal("hash-two", hashes[second]);
}
[Fact]
public async Task GetDeployedRevisionHashes_returns_empty_when_nothing_is_deployed()
{
await SeedInstanceAsync("Pump-01");
var hashes = await _repository.GetDeployedRevisionHashesAsync();
Assert.Empty(hashes);
}
}
@@ -45,8 +45,10 @@ namespace ZB.MOM.WW.ScadaBridge.Transport.IntegrationTests;
/// Tasks 3–5. Excluded properties are limited to: surrogate <c>Id</c>s, FK id
/// columns that are legitimately remapped to the target's own surrogate keys,
/// inheritance-placeholder flags that are re-derived at import (never carried), and
/// the one documented design reset (imported <see cref="Instance.State"/> →
/// <see cref="InstanceState.NotDeployed"/>). Each exclusion is annotated inline.
/// the documented design resets — imported <see cref="Instance.State"/> →
/// <see cref="InstanceState.NotDeployed"/>, and <see cref="Site.ArtifactRevisionHash"/> /
/// <see cref="Site.ArtifactsDeployedAt"/> → null ("artifacts never deployed" in the importing
/// environment). Each exclusion is annotated inline.
/// </para>
/// <para>
/// Both the "source" and "imported" entities are re-read from the DB (AsNoTracking)
@@ -783,6 +785,10 @@ public sealed class RoundTripEquivalenceTests : IDisposable
NodeBAddress = "akka://site@10.0.0.2:2552",
GrpcNodeAAddress = "10.0.0.1:8083",
GrpcNodeBAddress = "10.0.0.2:8083",
// Non-default deploy state, so the imported site is provably reset rather than
// accidentally equal to an unset source.
ArtifactRevisionHash = "artifact-hash-1",
ArtifactsDeployedAt = new DateTimeOffset(2026, 9, 14, 10, 0, 0, TimeSpan.Zero),
};
ctx.Sites.Add(site);
await ctx.SaveChangesAsync();
@@ -883,8 +889,14 @@ public sealed class RoundTripEquivalenceTests : IDisposable
var impConn = await ctx.DataConnections.AsNoTracking().SingleAsync(c => c.Name == "OpcUaPrimary");
var impInstance = await LoadInstanceAsync(ctx, "Pump-01");
// Site.
AssertRoundTripEqual(srcSite, impSite, nameof(Site.Id));
// Site. ArtifactRevisionHash/ArtifactsDeployedAt are environment-specific deploy state
// (like Instance.State) — a site reads "artifacts never deployed" in the importing
// environment until artifacts are deployed there, so they are deliberately not
// transported.
AssertRoundTripEqual(srcSite, impSite,
nameof(Site.Id), nameof(Site.ArtifactRevisionHash), nameof(Site.ArtifactsDeployedAt));
Assert.Null(impSite.ArtifactRevisionHash);
Assert.Null(impSite.ArtifactsDeployedAt);
// DataConnection. SiteId remapped; Primary/Backup config ride the SecretsBlock.
AssertRoundTripEqual(srcConn, impConn, nameof(DataConnection.Id), nameof(DataConnection.SiteId));