Adds the plumbing for the fleet-aggregation feature without moving any
data yet. Same portal binary now supports two modes selected via
Application:RunMode (Client | Admin).
Backend
- New AdminDbContext (identity + branding shared via SharedSchemaConfiguration
helper + fleet schema). AppDbContext keeps existing identity + branding +
monitoring + rates; renamed implicitly the "Client" context. Only one is
registered with DI per RunMode.
- IWhiteLabelStore interface implemented by both contexts so BrandingService
works in either mode.
- Fleet entities: Customer, FleetSite, FleetDevice, FleetPowerMeasurement,
IngestEvent (all in the new fleet schema). Migration in Migrations/Admin/.
- CustomerService: 32-byte random token, SHA-256 hash stored, plaintext
shown once on create + rotate. Token lookup is a single O(log N) indexed
query.
- RunModeGuards: refuses Admin without conn string; refuses Client+push
without URL/token; refuses cross-DB pointing (Client at admin_fleet DB
with fleet.Customers, or Admin at customer DB with monitoring.PowerMeasurements).
- Endpoint maps now branch on RunMode:
Client → sites/measurements/rates/admin-sites/admin-rates
Admin → admin/customers
Shared → auth, users, branding, grafana, admin-config, app/info, health
- /api/app/info (anonymous) returns {runMode, applicationName, version} so
the SPA can drive nav without re-fetching auth state.
Frontend
- AppInfoProvider + useAppInfo hook fetch /api/app/info once on load.
- AdminCustomersPage with create / edit / rotate-token / delete.
- TokenShownOnceModal: shows token once, copy-to-clipboard, "I've stored
it" confirmation gate before closing.
- AppLayout nav swaps Sites <-> Customers based on RunMode and shows a
FLEET ADMIN tag in the header when in Admin mode.
Tests
- 11 new tests: CustomerTokenTests (5) + RunModeGuardsTests (6).
- 51/51 passing locally.
Verified
- dotnet build + dotnet test clean (zero errors, one EF1002 warning
suppressed in Phase 11 already).
- Client mode docker rebuild: no regressions, /api/app/info returns
Client, login works, /api/sites/ works.
- Admin mode spun up on port 8090 against a fresh admin_fleet DB:
/api/app/info returns Admin, customer ABC0001 registered, 64-char
token returned, list shows the row.
- Cross-DB guard: Client run against admin_fleet refuses with explicit
"is pointed at a database that contains fleet.Customers" error.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
484 lines
23 KiB
C#
484 lines
23 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|
|
|
#nullable disable
|
|
|
|
namespace Tau.Acuvim.Portal.Migrations.Admin
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class InitialFleet : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.EnsureSchema(
|
|
name: "identity");
|
|
|
|
migrationBuilder.EnsureSchema(
|
|
name: "fleet");
|
|
|
|
migrationBuilder.EnsureSchema(
|
|
name: "app");
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetRoles",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<string>(type: "text", nullable: false),
|
|
Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
NormalizedName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
ConcurrencyStamp = table.Column<string>(type: "text", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetUsers",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<string>(type: "text", nullable: false),
|
|
DisplayName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
IsActive = table.Column<bool>(type: "boolean", nullable: false),
|
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
|
UserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
NormalizedUserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
Email = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
NormalizedEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
|
|
EmailConfirmed = table.Column<bool>(type: "boolean", nullable: false),
|
|
PasswordHash = table.Column<string>(type: "text", nullable: true),
|
|
SecurityStamp = table.Column<string>(type: "text", nullable: true),
|
|
ConcurrencyStamp = table.Column<string>(type: "text", nullable: true),
|
|
PhoneNumber = table.Column<string>(type: "text", nullable: true),
|
|
PhoneNumberConfirmed = table.Column<bool>(type: "boolean", nullable: false),
|
|
TwoFactorEnabled = table.Column<bool>(type: "boolean", nullable: false),
|
|
LockoutEnd = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
|
|
LockoutEnabled = table.Column<bool>(type: "boolean", nullable: false),
|
|
AccessFailedCount = table.Column<int>(type: "integer", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "Customers",
|
|
schema: "fleet",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
Code = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
|
|
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
TokenHash = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
|
|
TokenIssuedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
|
TokenRotatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
|
IsActive = table.Column<bool>(type: "boolean", nullable: false),
|
|
FirstSeenAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
|
LastSeenAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
|
|
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_Customers", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "WhiteLabelSettings",
|
|
schema: "app",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "integer", nullable: false),
|
|
ApplicationName = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
LogoUrl = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
|
|
PrimaryColor = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
|
SecondaryColor = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
|
AccentColor = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
|
FooterText = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
|
|
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_WhiteLabelSettings", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetRoleClaims",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "integer", nullable: false)
|
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
|
RoleId = table.Column<string>(type: "text", nullable: false),
|
|
ClaimType = table.Column<string>(type: "text", nullable: true),
|
|
ClaimValue = table.Column<string>(type: "text", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_AspNetRoleClaims_AspNetRoles_RoleId",
|
|
column: x => x.RoleId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetRoles",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetUserClaims",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "integer", nullable: false)
|
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
|
UserId = table.Column<string>(type: "text", nullable: false),
|
|
ClaimType = table.Column<string>(type: "text", nullable: true),
|
|
ClaimValue = table.Column<string>(type: "text", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_AspNetUserClaims_AspNetUsers_UserId",
|
|
column: x => x.UserId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetUsers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetUserLogins",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
LoginProvider = table.Column<string>(type: "text", nullable: false),
|
|
ProviderKey = table.Column<string>(type: "text", nullable: false),
|
|
ProviderDisplayName = table.Column<string>(type: "text", nullable: true),
|
|
UserId = table.Column<string>(type: "text", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey });
|
|
table.ForeignKey(
|
|
name: "FK_AspNetUserLogins_AspNetUsers_UserId",
|
|
column: x => x.UserId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetUsers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetUserRoles",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
UserId = table.Column<string>(type: "text", nullable: false),
|
|
RoleId = table.Column<string>(type: "text", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
|
|
table.ForeignKey(
|
|
name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
|
|
column: x => x.RoleId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetRoles",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
table.ForeignKey(
|
|
name: "FK_AspNetUserRoles_AspNetUsers_UserId",
|
|
column: x => x.UserId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetUsers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "AspNetUserTokens",
|
|
schema: "identity",
|
|
columns: table => new
|
|
{
|
|
UserId = table.Column<string>(type: "text", nullable: false),
|
|
LoginProvider = table.Column<string>(type: "text", nullable: false),
|
|
Name = table.Column<string>(type: "text", nullable: false),
|
|
Value = table.Column<string>(type: "text", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
|
|
table.ForeignKey(
|
|
name: "FK_AspNetUserTokens_AspNetUsers_UserId",
|
|
column: x => x.UserId,
|
|
principalSchema: "identity",
|
|
principalTable: "AspNetUsers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "IngestEvents",
|
|
schema: "fleet",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
CustomerId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
ReceivedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
|
BatchType = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
|
|
RowsAccepted = table.Column<int>(type: "integer", nullable: false),
|
|
RowsRejected = table.Column<int>(type: "integer", nullable: false),
|
|
BatchBytes = table.Column<int>(type: "integer", nullable: false),
|
|
ClientHwm = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
|
|
TimeSpread = table.Column<TimeSpan>(type: "interval", nullable: true),
|
|
Error = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_IngestEvents", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_IngestEvents_Customers_CustomerId",
|
|
column: x => x.CustomerId,
|
|
principalSchema: "fleet",
|
|
principalTable: "Customers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "Sites",
|
|
schema: "fleet",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
CustomerId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
Address = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
|
|
LocalMunicipalityId = table.Column<int>(type: "integer", nullable: true),
|
|
IsActive = table.Column<bool>(type: "boolean", nullable: false),
|
|
ReceivedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_Sites", x => new { x.CustomerId, x.Id });
|
|
table.ForeignKey(
|
|
name: "FK_Sites_Customers_CustomerId",
|
|
column: x => x.CustomerId,
|
|
principalSchema: "fleet",
|
|
principalTable: "Customers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "Devices",
|
|
schema: "fleet",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
|
CustomerId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
SiteId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
Name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
ExternalId = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
|
|
Description = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
|
|
IsActive = table.Column<bool>(type: "boolean", nullable: false),
|
|
ReceivedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_Devices", x => new { x.CustomerId, x.Id });
|
|
table.ForeignKey(
|
|
name: "FK_Devices_Customers_CustomerId",
|
|
column: x => x.CustomerId,
|
|
principalSchema: "fleet",
|
|
principalTable: "Customers",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Cascade);
|
|
table.ForeignKey(
|
|
name: "FK_Devices_Sites_CustomerId_SiteId",
|
|
columns: x => new { x.CustomerId, x.SiteId },
|
|
principalSchema: "fleet",
|
|
principalTable: "Sites",
|
|
principalColumns: new[] { "CustomerId", "Id" },
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "PowerMeasurements",
|
|
schema: "fleet",
|
|
columns: table => new
|
|
{
|
|
Time = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
|
|
CustomerId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
DeviceId = table.Column<Guid>(type: "uuid", nullable: false),
|
|
ActivePowerKw = table.Column<double>(type: "double precision", nullable: false),
|
|
ReactivePowerKvar = table.Column<double>(type: "double precision", nullable: true),
|
|
ApparentPowerKva = table.Column<double>(type: "double precision", nullable: true),
|
|
PowerFactor = table.Column<double>(type: "double precision", nullable: true),
|
|
VoltageV = table.Column<double>(type: "double precision", nullable: true),
|
|
FrequencyHz = table.Column<double>(type: "double precision", nullable: true),
|
|
EnergyImportedKwh = table.Column<double>(type: "double precision", nullable: true),
|
|
EnergyExportedKwh = table.Column<double>(type: "double precision", nullable: true),
|
|
Source = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_PowerMeasurements", x => new { x.Time, x.CustomerId, x.DeviceId });
|
|
table.ForeignKey(
|
|
name: "FK_PowerMeasurements_Devices_CustomerId_DeviceId",
|
|
columns: x => new { x.CustomerId, x.DeviceId },
|
|
principalSchema: "fleet",
|
|
principalTable: "Devices",
|
|
principalColumns: new[] { "CustomerId", "Id" },
|
|
onDelete: ReferentialAction.Cascade);
|
|
});
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_AspNetRoleClaims_RoleId",
|
|
schema: "identity",
|
|
table: "AspNetRoleClaims",
|
|
column: "RoleId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "RoleNameIndex",
|
|
schema: "identity",
|
|
table: "AspNetRoles",
|
|
column: "NormalizedName",
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_AspNetUserClaims_UserId",
|
|
schema: "identity",
|
|
table: "AspNetUserClaims",
|
|
column: "UserId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_AspNetUserLogins_UserId",
|
|
schema: "identity",
|
|
table: "AspNetUserLogins",
|
|
column: "UserId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_AspNetUserRoles_RoleId",
|
|
schema: "identity",
|
|
table: "AspNetUserRoles",
|
|
column: "RoleId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "EmailIndex",
|
|
schema: "identity",
|
|
table: "AspNetUsers",
|
|
column: "NormalizedEmail");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "UserNameIndex",
|
|
schema: "identity",
|
|
table: "AspNetUsers",
|
|
column: "NormalizedUserName",
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Customers_Code",
|
|
schema: "fleet",
|
|
table: "Customers",
|
|
column: "Code",
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Customers_TokenHash",
|
|
schema: "fleet",
|
|
table: "Customers",
|
|
column: "TokenHash",
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Devices_CustomerId_SiteId",
|
|
schema: "fleet",
|
|
table: "Devices",
|
|
columns: new[] { "CustomerId", "SiteId" });
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_IngestEvents_CustomerId_ReceivedAt",
|
|
schema: "fleet",
|
|
table: "IngestEvents",
|
|
columns: new[] { "CustomerId", "ReceivedAt" },
|
|
descending: new[] { false, true });
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_PowerMeasurements_CustomerId_DeviceId_Time",
|
|
schema: "fleet",
|
|
table: "PowerMeasurements",
|
|
columns: new[] { "CustomerId", "DeviceId", "Time" },
|
|
descending: new[] { false, false, true });
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_PowerMeasurements_CustomerId_Time",
|
|
schema: "fleet",
|
|
table: "PowerMeasurements",
|
|
columns: new[] { "CustomerId", "Time" },
|
|
descending: new[] { false, true });
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetRoleClaims",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetUserClaims",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetUserLogins",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetUserRoles",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetUserTokens",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "IngestEvents",
|
|
schema: "fleet");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "PowerMeasurements",
|
|
schema: "fleet");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "WhiteLabelSettings",
|
|
schema: "app");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetRoles",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "AspNetUsers",
|
|
schema: "identity");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "Devices",
|
|
schema: "fleet");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "Sites",
|
|
schema: "fleet");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "Customers",
|
|
schema: "fleet");
|
|
}
|
|
}
|
|
}
|