shoc-backend/Data.SeaHavenIndustries/Migrations/20260624163145_Phase0_DomainFoundation.cs

744 lines
24 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Data.SeaHavenIndustries.Migrations
{
/// <inheritdoc />
public partial class Phase0_DomainFoundation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Dispatches_workOrders_WorkOrderId",
table: "Dispatches");
migrationBuilder.DropForeignKey(
name: "FK_Locations_Accounts_AccountId",
table: "Locations");
migrationBuilder.DropIndex(
name: "IX_Locations_AccountId",
table: "Locations");
migrationBuilder.DropColumn(
name: "CreatedDate",
table: "Regions");
migrationBuilder.DropColumn(
name: "DeleterUserId",
table: "Regions");
migrationBuilder.DropColumn(
name: "DeletionTime",
table: "Regions");
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "Regions");
migrationBuilder.DropColumn(
name: "LastModificationTime",
table: "Regions");
migrationBuilder.DropColumn(
name: "LastModifierUserId",
table: "Regions");
migrationBuilder.DropColumn(
name: "createdby",
table: "Regions");
migrationBuilder.DropColumn(
name: "AccountId",
table: "Locations");
migrationBuilder.DropColumn(
name: "CreatedDate",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "DeleterUserId",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "DeletionTime",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "LastModificationTime",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "LastModifierUserId",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "createdby",
table: "JobTitles");
migrationBuilder.DropColumn(
name: "CreatedDate",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "DeleterUserId",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "DeletionTime",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "LastModificationTime",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "LastModifierUserId",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "createdby",
table: "EmployeeEmails");
migrationBuilder.DropColumn(
name: "CreatedDate",
table: "Departments");
migrationBuilder.DropColumn(
name: "DeleterUserId",
table: "Departments");
migrationBuilder.DropColumn(
name: "DeletionTime",
table: "Departments");
migrationBuilder.DropColumn(
name: "IsDeleted",
table: "Departments");
migrationBuilder.DropColumn(
name: "LastModificationTime",
table: "Departments");
migrationBuilder.DropColumn(
name: "LastModifierUserId",
table: "Departments");
migrationBuilder.DropColumn(
name: "createdby",
table: "Departments");
migrationBuilder.RenameColumn(
name: "ZipCode",
table: "Locations",
newName: "Zip");
migrationBuilder.RenameColumn(
name: "Phone",
table: "Locations",
newName: "PhoneNumber");
migrationBuilder.RenameColumn(
name: "ContactEmail",
table: "Locations",
newName: "Email");
migrationBuilder.RenameColumn(
name: "Contact",
table: "Locations",
newName: "Address2");
migrationBuilder.RenameColumn(
name: "Address",
table: "Locations",
newName: "Address1");
migrationBuilder.AddColumn<DateOnly>(
name: "AssignDate",
table: "workOrders",
type: "date",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "AvettaTask",
table: "workOrders",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "CarriedOver",
table: "workOrders",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<int>(
name: "DocStatus",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "LegacyStatus",
table: "workOrders",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "LifecycleStatus",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "OperationalFlags",
table: "workOrders",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<DateOnly>(
name: "OriginalDate",
table: "workOrders",
type: "date",
nullable: true);
migrationBuilder.AddColumn<DateOnly>(
name: "OriginalWeek",
table: "workOrders",
type: "date",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "PrimaryDispatchId",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "RescheduleCount",
table: "workOrders",
type: "int",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<byte[]>(
name: "RowVersion",
table: "workOrders",
type: "rowversion",
rowVersion: true,
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "ScheduleWeekOnly",
table: "workOrders",
type: "bit",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "ScheduledEnd",
table: "workOrders",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<DateOnly>(
name: "TargetWeek",
table: "workOrders",
type: "date",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "WorkOrderType",
table: "workOrders",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Service",
table: "workOrders",
type: "nvarchar(128)",
maxLength: 128,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Notes",
table: "WorkOrderContacts",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ActorType",
table: "WorkOrderAuditLogs",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "CorrelationId",
table: "WorkOrderAuditLogs",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "DispatchId",
table: "WorkOrderAuditLogs",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "EventType",
table: "WorkOrderAuditLogs",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<byte[]>(
name: "RowVersion",
table: "Dispatches",
type: "rowversion",
rowVersion: true,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Color",
table: "AspNetUsers",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Initials",
table: "AspNetUsers",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.CreateTable(
name: "WorkOrderFieldLocks",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
WorkOrderId = table.Column<int>(type: "int", nullable: false),
FieldName = table.Column<string>(type: "nvarchar(450)", nullable: false),
LockedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
LockedByUserId = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_WorkOrderFieldLocks", x => x.Id);
table.ForeignKey(
name: "FK_WorkOrderFieldLocks_workOrders_WorkOrderId",
column: x => x.WorkOrderId,
principalTable: "workOrders",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_workOrders_PrimaryDispatchId",
table: "workOrders",
column: "PrimaryDispatchId");
migrationBuilder.CreateIndex(
name: "IX_WorkOrderFieldLocks_WorkOrderId_FieldName",
table: "WorkOrderFieldLocks",
columns: new[] { "WorkOrderId", "FieldName" },
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_Dispatches_workOrders_WorkOrderId",
table: "Dispatches",
column: "WorkOrderId",
principalTable: "workOrders",
principalColumn: "Id");
migrationBuilder.AddForeignKey(
name: "FK_workOrders_Dispatches_PrimaryDispatchId",
table: "workOrders",
column: "PrimaryDispatchId",
principalTable: "Dispatches",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.Sql("""
UPDATE workOrders SET LegacyStatus = Status WHERE LegacyStatus IS NULL AND Status IS NOT NULL;
""");
migrationBuilder.Sql("""
UPDATE workOrders SET LifecycleStatus = CASE
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('open', 'incomplete') THEN 1
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('pending', 'onhold', 'on hold') THEN 2
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('scheduled') THEN 3
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('en route', 'enroute') THEN 4
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('on site', 'onsite') THEN 5
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('inprogress', 'in progress') THEN 6
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('completed', 'complete', 'done', 'closed') THEN 7
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('rescheduled') THEN 8
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('cancelled', 'canceled') THEN 9
WHEN LOWER(LTRIM(RTRIM(Status))) IN ('pending quote', 'pendingquote', 'pendingapproval', 'pending approval') THEN 10
ELSE 1
END
WHERE LifecycleStatus IS NULL AND Status IS NOT NULL;
""");
migrationBuilder.Sql("""
UPDATE wo SET wo.PrimaryDispatchId = latest.DispatchId
FROM workOrders wo
INNER JOIN (
SELECT WorkOrderId, MAX(Id) AS DispatchId
FROM Dispatches
WHERE WorkOrderId IS NOT NULL
GROUP BY WorkOrderId
) latest ON wo.Id = latest.WorkOrderId
WHERE wo.PrimaryDispatchId IS NULL;
""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Dispatches_workOrders_WorkOrderId",
table: "Dispatches");
migrationBuilder.DropForeignKey(
name: "FK_workOrders_Dispatches_PrimaryDispatchId",
table: "workOrders");
migrationBuilder.DropTable(
name: "WorkOrderFieldLocks");
migrationBuilder.DropIndex(
name: "IX_workOrders_PrimaryDispatchId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "AssignDate",
table: "workOrders");
migrationBuilder.DropColumn(
name: "AvettaTask",
table: "workOrders");
migrationBuilder.DropColumn(
name: "CarriedOver",
table: "workOrders");
migrationBuilder.DropColumn(
name: "DocStatus",
table: "workOrders");
migrationBuilder.DropColumn(
name: "LegacyStatus",
table: "workOrders");
migrationBuilder.DropColumn(
name: "LifecycleStatus",
table: "workOrders");
migrationBuilder.DropColumn(
name: "OperationalFlags",
table: "workOrders");
migrationBuilder.DropColumn(
name: "OriginalDate",
table: "workOrders");
migrationBuilder.DropColumn(
name: "OriginalWeek",
table: "workOrders");
migrationBuilder.DropColumn(
name: "PrimaryDispatchId",
table: "workOrders");
migrationBuilder.DropColumn(
name: "RescheduleCount",
table: "workOrders");
migrationBuilder.DropColumn(
name: "RowVersion",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ScheduleWeekOnly",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ScheduledEnd",
table: "workOrders");
migrationBuilder.DropColumn(
name: "TargetWeek",
table: "workOrders");
migrationBuilder.DropColumn(
name: "WorkOrderType",
table: "workOrders");
migrationBuilder.DropColumn(
name: "Service",
table: "workOrders");
migrationBuilder.DropColumn(
name: "Notes",
table: "WorkOrderContacts");
migrationBuilder.DropColumn(
name: "ActorType",
table: "WorkOrderAuditLogs");
migrationBuilder.DropColumn(
name: "CorrelationId",
table: "WorkOrderAuditLogs");
migrationBuilder.DropColumn(
name: "DispatchId",
table: "WorkOrderAuditLogs");
migrationBuilder.DropColumn(
name: "EventType",
table: "WorkOrderAuditLogs");
migrationBuilder.DropColumn(
name: "RowVersion",
table: "Dispatches");
migrationBuilder.DropColumn(
name: "Color",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "Initials",
table: "AspNetUsers");
migrationBuilder.RenameColumn(
name: "Zip",
table: "Locations",
newName: "ZipCode");
migrationBuilder.RenameColumn(
name: "PhoneNumber",
table: "Locations",
newName: "Phone");
migrationBuilder.RenameColumn(
name: "Email",
table: "Locations",
newName: "ContactEmail");
migrationBuilder.RenameColumn(
name: "Address2",
table: "Locations",
newName: "Contact");
migrationBuilder.RenameColumn(
name: "Address1",
table: "Locations",
newName: "Address");
migrationBuilder.AddColumn<DateTime>(
name: "CreatedDate",
table: "Regions",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "DeleterUserId",
table: "Regions",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "DeletionTime",
table: "Regions",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "Regions",
type: "bit",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastModificationTime",
table: "Regions",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "LastModifierUserId",
table: "Regions",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "createdby",
table: "Regions",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "AccountId",
table: "Locations",
type: "int",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "CreatedDate",
table: "JobTitles",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "DeleterUserId",
table: "JobTitles",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "DeletionTime",
table: "JobTitles",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "JobTitles",
type: "bit",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastModificationTime",
table: "JobTitles",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "LastModifierUserId",
table: "JobTitles",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "createdby",
table: "JobTitles",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "CreatedDate",
table: "EmployeeEmails",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "DeleterUserId",
table: "EmployeeEmails",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "DeletionTime",
table: "EmployeeEmails",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "EmployeeEmails",
type: "bit",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastModificationTime",
table: "EmployeeEmails",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "LastModifierUserId",
table: "EmployeeEmails",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "createdby",
table: "EmployeeEmails",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "CreatedDate",
table: "Departments",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "DeleterUserId",
table: "Departments",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "DeletionTime",
table: "Departments",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<bool>(
name: "IsDeleted",
table: "Departments",
type: "bit",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastModificationTime",
table: "Departments",
type: "datetime2",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "LastModifierUserId",
table: "Departments",
type: "int",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "createdby",
table: "Departments",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Locations_AccountId",
table: "Locations",
column: "AccountId");
migrationBuilder.AddForeignKey(
name: "FK_Dispatches_workOrders_WorkOrderId",
table: "Dispatches",
column: "WorkOrderId",
principalTable: "workOrders",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_Locations_Accounts_AccountId",
table: "Locations",
column: "AccountId",
principalTable: "Accounts",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
}
}