mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 07:13:12 +00:00
162 lines
8.1 KiB
C#
162 lines
8.1 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Data.SeaHavenIndustries.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class SH303_ServicesRegistry : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.CreateTable(
|
|
name: "Services",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "int", nullable: false)
|
|
.Annotation("SqlServer:Identity", "1, 1"),
|
|
Name = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
|
|
NormalizedName = table.Column<string>(type: "nvarchar(200)", maxLength: 200, nullable: false),
|
|
Trade = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
|
|
IconKey = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
|
|
RequiresCompletionDocument = table.Column<bool>(type: "bit", nullable: false),
|
|
CompletionDocTemplateId = table.Column<int>(type: "int", nullable: true),
|
|
IsActive = table.Column<bool>(type: "bit", nullable: false),
|
|
IsDeleted = table.Column<bool>(type: "bit", nullable: true),
|
|
createdby = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
DeleterUserId = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
CreatedDate = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
LastModifierUserId = table.Column<int>(type: "int", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_Services", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_Services_CompletionDocTemplates_CompletionDocTemplateId",
|
|
column: x => x.CompletionDocTemplateId,
|
|
principalTable: "CompletionDocTemplates",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
});
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "ServiceWorkOrderTypes",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "int", nullable: false)
|
|
.Annotation("SqlServer:Identity", "1, 1"),
|
|
ServiceId = table.Column<int>(type: "int", nullable: false),
|
|
WorkOrderType = table.Column<int>(type: "int", nullable: false),
|
|
IsDeleted = table.Column<bool>(type: "bit", nullable: true),
|
|
createdby = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
DeleterUserId = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
CreatedDate = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
LastModifierUserId = table.Column<int>(type: "int", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_ServiceWorkOrderTypes", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_ServiceWorkOrderTypes_Services_ServiceId",
|
|
column: x => x.ServiceId,
|
|
principalTable: "Services",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
});
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Services_CompletionDocTemplateId",
|
|
table: "Services",
|
|
column: "CompletionDocTemplateId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Services_IsActive",
|
|
table: "Services",
|
|
column: "IsActive");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Services_NormalizedName",
|
|
table: "Services",
|
|
column: "NormalizedName",
|
|
unique: true);
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_Services_Trade",
|
|
table: "Services",
|
|
column: "Trade");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_ServiceWorkOrderTypes_ServiceId_WorkOrderType",
|
|
table: "ServiceWorkOrderTypes",
|
|
columns: new[] { "ServiceId", "WorkOrderType" },
|
|
unique: true);
|
|
|
|
// Additive seed: one active service per active Problem dropdown
|
|
// value already stored in the database (never the prototype's demo
|
|
// leaves). Idempotent via NormalizedName NOT EXISTS; dropdown rows are
|
|
// only read, never altered. Repeated source values collapse onto a
|
|
// deterministic MIN(Value) representative per normalized key.
|
|
migrationBuilder.Sql(
|
|
"""
|
|
INSERT INTO [Services] ([Name], [NormalizedName], [Trade], [IconKey], [RequiresCompletionDocument], [CompletionDocTemplateId], [IsActive], [IsDeleted], [createdby])
|
|
SELECT seeded.[Name], seeded.[NormalizedName], seeded.[Trade],
|
|
CASE seeded.[Trade]
|
|
WHEN N'Plumbing & Water Systems' THEN N'plumbing-water-systems'
|
|
WHEN N'Electrical' THEN N'electrical'
|
|
WHEN N'HVAC' THEN N'hvac'
|
|
WHEN N'Fire & Life Safety' THEN N'fire-life-safety'
|
|
ELSE N'general-building-handyman'
|
|
END,
|
|
0, NULL, 1, 0, N'sh303-migration'
|
|
FROM (
|
|
SELECT agg.[Name], agg.[NormalizedName],
|
|
CASE
|
|
WHEN agg.[Name] IN (N'Leak', N'Clogged') THEN N'Plumbing & Water Systems'
|
|
WHEN agg.[Name] = N'No Power' THEN N'Electrical'
|
|
WHEN agg.[Name] IN (N'Not Cooling', N'Not Heating') THEN N'HVAC'
|
|
WHEN agg.[Name] = N'Alarm Fault' THEN N'Fire & Life Safety'
|
|
ELSE N'General Building & Handyman'
|
|
END AS [Trade]
|
|
FROM (
|
|
SELECT MIN(src.[Value]) AS [Name],
|
|
LOWER(LTRIM(RTRIM(src.[Value]))) AS [NormalizedName]
|
|
FROM [DropdownOptions] src
|
|
WHERE src.[Category] = N'Problem'
|
|
AND src.[IsActive] = 1
|
|
GROUP BY LOWER(LTRIM(RTRIM(src.[Value])))
|
|
) agg
|
|
) seeded
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM [Services] s WHERE s.[NormalizedName] = seeded.[NormalizedName]
|
|
);
|
|
|
|
INSERT INTO [ServiceWorkOrderTypes] ([ServiceId], [WorkOrderType], [IsDeleted])
|
|
SELECT s.[Id], types.[WorkOrderType], 0
|
|
FROM [Services] s
|
|
CROSS JOIN (VALUES (2), (6), (3)) AS types([WorkOrderType])
|
|
WHERE s.[createdby] = N'sh303-migration'
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM [ServiceWorkOrderTypes] x
|
|
WHERE x.[ServiceId] = s.[Id]
|
|
AND x.[WorkOrderType] = types.[WorkOrderType]
|
|
);
|
|
""");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropTable(
|
|
name: "ServiceWorkOrderTypes");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "Services");
|
|
}
|
|
}
|
|
}
|