shoc-backend/Data.SeaHavenIndustries/Migrations/20260916204606_SH187_WorkOrderServiceLink.cs
2026-09-16 18:02:57 -03:00

73 lines
2.5 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Data.SeaHavenIndustries.Migrations
{
/// <inheritdoc />
public partial class SH187_WorkOrderServiceLink : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "ServiceId",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ServiceNameSnapshot",
table: "workOrders",
type: "nvarchar(200)",
maxLength: 200,
nullable: true);
migrationBuilder.Sql(
"""
UPDATE w
SET
w.[ServiceNameSnapshot] = LEFT(COALESCE(NULLIF(LTRIM(RTRIM(w.[Trade])), N''), NULLIF(LTRIM(RTRIM(w.[Problem])), N'')), 200),
w.[ServiceId] = s.[Id]
FROM [workOrders] w
LEFT JOIN [Services] s
ON (s.[IsDeleted] <> 1 OR s.[IsDeleted] IS NULL)
AND s.[NormalizedName] = LOWER(LTRIM(RTRIM(COALESCE(NULLIF(LTRIM(RTRIM(w.[Trade])), N''), NULLIF(LTRIM(RTRIM(w.[Problem])), N'')))))
WHERE w.[ServiceNameSnapshot] IS NULL;
""");
migrationBuilder.CreateIndex(
name: "IX_workOrders_ServiceId",
table: "workOrders",
column: "ServiceId");
migrationBuilder.AddForeignKey(
name: "FK_workOrders_Services_ServiceId",
table: "workOrders",
column: "ServiceId",
principalTable: "Services",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_workOrders_Services_ServiceId",
table: "workOrders");
migrationBuilder.DropIndex(
name: "IX_workOrders_ServiceId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ServiceId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ServiceNameSnapshot",
table: "workOrders");
}
}
}