shoc-backend/Data.SeaHavenIndustries/Migrations/20260725005530_SH133_WorkOrderReconciliation.cs
2026-07-24 22:13:25 -03:00

221 lines
8.5 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Data.SeaHavenIndustries.Migrations
{
/// <inheritdoc />
public partial class SH133_WorkOrderReconciliation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(
"""
DECLARE @nextValue bigint =
COALESCE((
SELECT MAX(TRY_CONVERT(bigint, [InternalWONumber])) + 1
FROM [workOrders]
WHERE [InternalWONumber] IS NOT NULL
), 1);
IF NOT EXISTS (
SELECT 1 FROM sys.sequences
WHERE [name] = N'WorkOrderInternalNumberSequence'
AND [schema_id] = SCHEMA_ID(N'dbo')
)
BEGIN
DECLARE @sql nvarchar(max) =
N'CREATE SEQUENCE [dbo].[WorkOrderInternalNumberSequence] AS bigint START WITH '
+ CONVERT(nvarchar(20), @nextValue)
+ N' INCREMENT BY 1;';
EXEC sp_executesql @sql;
END
""");
migrationBuilder.AddColumn<string>(
name: "ExternalAssignedTo",
table: "workOrders",
type: "nvarchar(450)",
maxLength: 450,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalRecordType",
table: "workOrders",
type: "nvarchar(64)",
maxLength: 64,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalSource",
table: "workOrders",
type: "nvarchar(128)",
maxLength: 128,
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "ExternalUpdatedAt",
table: "workOrders",
type: "datetimeoffset",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalVersionHash",
table: "workOrders",
type: "nvarchar(64)",
maxLength: 64,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalLocationId",
table: "Locations",
type: "nvarchar(450)",
maxLength: 450,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalSource",
table: "Locations",
type: "nvarchar(128)",
maxLength: 128,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalSource",
table: "Comments",
type: "nvarchar(128)",
maxLength: 128,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalSourceEmailS3Key",
table: "Comments",
type: "nvarchar(2048)",
maxLength: 2048,
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "ExternalUpdatedAt",
table: "Comments",
type: "datetimeoffset",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "ExternalVersionHash",
table: "Comments",
type: "nvarchar(64)",
maxLength: 64,
nullable: true);
migrationBuilder.CreateTable(
name: "WorkOrderExternalReceipts",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Source = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
Kind = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
ExternalId = table.Column<string>(type: "nvarchar(450)", maxLength: 450, nullable: false),
UpdatedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false),
VersionHash = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: false),
ProcessedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_WorkOrderExternalReceipts", x => x.Id);
});
migrationBuilder.CreateTable(
name: "WorkOrderReconciliationJobs",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false),
RunId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
FenceToken = table.Column<Guid>(type: "uniqueidentifier", nullable: true),
State = table.Column<string>(type: "nvarchar(32)", maxLength: 32, nullable: false),
Reason = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: true),
RequestedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
StartedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
LeaseExpiresAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
CompletedAt = table.Column<DateTimeOffset>(type: "datetimeoffset", nullable: true),
WorkOrdersProcessed = table.Column<int>(type: "int", nullable: false),
CommentsProcessed = table.Column<int>(type: "int", nullable: false),
ErrorCode = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
RowVersion = table.Column<byte[]>(type: "rowversion", rowVersion: true, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_WorkOrderReconciliationJobs", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_WorkOrderExternalReceipts_Source_Kind_ExternalId",
table: "WorkOrderExternalReceipts",
columns: new[] { "Source", "Kind", "ExternalId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_WorkOrderReconciliationJobs_State",
table: "WorkOrderReconciliationJobs",
column: "State");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "WorkOrderExternalReceipts");
migrationBuilder.DropTable(
name: "WorkOrderReconciliationJobs");
migrationBuilder.DropColumn(
name: "ExternalAssignedTo",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ExternalRecordType",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ExternalSource",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ExternalUpdatedAt",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ExternalVersionHash",
table: "workOrders");
migrationBuilder.DropColumn(
name: "ExternalLocationId",
table: "Locations");
migrationBuilder.DropColumn(
name: "ExternalSource",
table: "Locations");
migrationBuilder.DropColumn(
name: "ExternalSource",
table: "Comments");
migrationBuilder.DropColumn(
name: "ExternalSourceEmailS3Key",
table: "Comments");
migrationBuilder.DropColumn(
name: "ExternalUpdatedAt",
table: "Comments");
migrationBuilder.DropColumn(
name: "ExternalVersionHash",
table: "Comments");
migrationBuilder.DropSequence(
name: "WorkOrderInternalNumberSequence");
}
}
}