mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 04:53:11 +00:00
Stop client writes from changing Locations.AccountId, make the SH-221 migration discoverable, and thread the board-create CancellationToken through lookup and persistence.
72 lines
2.1 KiB
C#
72 lines
2.1 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Data.SeaHavenIndustries.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
[DbContext(typeof(ApplicationDbContext))]
|
|
[Migration("20260826120000_SH221_LocationAccountScope")]
|
|
public partial class SH221_LocationAccountScope : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "AccountId",
|
|
table: "Locations",
|
|
type: "int",
|
|
nullable: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Locations_AccountId",
|
|
table: "Locations",
|
|
column: "AccountId");
|
|
|
|
migrationBuilder.AddForeignKey(
|
|
name: "FK_Locations_Accounts_AccountId",
|
|
table: "Locations",
|
|
column: "AccountId",
|
|
principalTable: "Accounts",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
|
|
migrationBuilder.Sql(@"
|
|
;WITH UniqueLocationAccounts AS (
|
|
SELECT
|
|
wo.[LocationId] AS [LocationId],
|
|
MIN(wo.[AccountId]) AS [AccountId]
|
|
FROM [workOrders] wo
|
|
WHERE wo.[LocationId] IS NOT NULL
|
|
AND wo.[AccountId] IS NOT NULL
|
|
GROUP BY wo.[LocationId]
|
|
HAVING COUNT(DISTINCT wo.[AccountId]) = 1
|
|
)
|
|
UPDATE loc
|
|
SET loc.[AccountId] = ula.[AccountId]
|
|
FROM [Locations] loc
|
|
INNER JOIN UniqueLocationAccounts ula
|
|
ON ula.[LocationId] = loc.[Id]
|
|
WHERE loc.[AccountId] IS NULL;
|
|
");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropForeignKey(
|
|
name: "FK_Locations_Accounts_AccountId",
|
|
table: "Locations");
|
|
|
|
migrationBuilder.DropIndex(
|
|
name: "IX_Locations_AccountId",
|
|
table: "Locations");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "AccountId",
|
|
table: "Locations");
|
|
}
|
|
}
|
|
}
|