using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Data.SeaHavenIndustries.Migrations { /// /// SH-393: board-created work orders with a vendor stored their primary dispatch with /// no WorkOrderId and no DispatchWorkOrders link, so every owned-or-linked read (uplift /// list, allowance, approval queue WO number, dispatch patches) missed it. Data-only /// heal: assign the dispatch to the single work order that names it as primary. Rows /// already owned or linked, and dispatches named primary by more than one work order, /// are left untouched, so re-running is a no-op. /// public partial class SH393_BackfillBoardCreatedDispatchWorkOrder : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.Sql( """ UPDATE d SET d.[WorkOrderId] = w.[Id] FROM [Dispatches] AS d INNER JOIN [workOrders] AS w ON w.[PrimaryDispatchId] = d.[Id] WHERE d.[WorkOrderId] IS NULL AND NOT EXISTS ( SELECT 1 FROM [DispatchWorkOrders] AS l WHERE l.[DispatchId] = d.[Id]) AND ( SELECT COUNT(*) FROM [workOrders] AS owner WHERE owner.[PrimaryDispatchId] = d.[Id]) = 1; """); } /// protected override void Down(MigrationBuilder migrationBuilder) { // The heal is not reversible: healed rows are indistinguishable from dispatches // that were created with their work order set. } } }