using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Data.SeaHavenIndustries.Migrations { /// public partial class SH133_WorkOrderReconciliation : Migration { /// 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( name: "ExternalAssignedTo", table: "workOrders", type: "nvarchar(450)", maxLength: 450, nullable: true); migrationBuilder.AddColumn( name: "ExternalRecordType", table: "workOrders", type: "nvarchar(64)", maxLength: 64, nullable: true); migrationBuilder.AddColumn( name: "ExternalSource", table: "workOrders", type: "nvarchar(128)", maxLength: 128, nullable: true); migrationBuilder.AddColumn( name: "ExternalUpdatedAt", table: "workOrders", type: "datetimeoffset", nullable: true); migrationBuilder.AddColumn( name: "ExternalVersionHash", table: "workOrders", type: "nvarchar(64)", maxLength: 64, nullable: true); migrationBuilder.AddColumn( name: "ExternalLocationId", table: "Locations", type: "nvarchar(450)", maxLength: 450, nullable: true); migrationBuilder.AddColumn( name: "ExternalSource", table: "Locations", type: "nvarchar(128)", maxLength: 128, nullable: true); migrationBuilder.AddColumn( name: "ExternalSource", table: "Comments", type: "nvarchar(128)", maxLength: 128, nullable: true); migrationBuilder.AddColumn( name: "ExternalSourceEmailS3Key", table: "Comments", type: "nvarchar(2048)", maxLength: 2048, nullable: true); migrationBuilder.AddColumn( name: "ExternalUpdatedAt", table: "Comments", type: "datetimeoffset", nullable: true); migrationBuilder.AddColumn( name: "ExternalVersionHash", table: "Comments", type: "nvarchar(64)", maxLength: 64, nullable: true); migrationBuilder.CreateTable( name: "WorkOrderExternalReceipts", columns: table => new { Id = table.Column(type: "bigint", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Source = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: false), Kind = table.Column(type: "nvarchar(32)", maxLength: 32, nullable: false), ExternalId = table.Column(type: "nvarchar(450)", maxLength: 450, nullable: false), UpdatedAt = table.Column(type: "datetimeoffset", nullable: false), VersionHash = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), ProcessedAt = table.Column(type: "datetimeoffset", nullable: false) }, constraints: table => { table.PrimaryKey("PK_WorkOrderExternalReceipts", x => x.Id); }); migrationBuilder.CreateTable( name: "WorkOrderReconciliationJobs", columns: table => new { Id = table.Column(type: "int", nullable: false), RunId = table.Column(type: "uniqueidentifier", nullable: false), FenceToken = table.Column(type: "uniqueidentifier", nullable: true), State = table.Column(type: "nvarchar(32)", maxLength: 32, nullable: false), Reason = table.Column(type: "nvarchar(128)", maxLength: 128, nullable: true), RequestedAt = table.Column(type: "datetimeoffset", nullable: true), StartedAt = table.Column(type: "datetimeoffset", nullable: true), LeaseExpiresAt = table.Column(type: "datetimeoffset", nullable: true), CompletedAt = table.Column(type: "datetimeoffset", nullable: true), WorkOrdersProcessed = table.Column(type: "int", nullable: false), CommentsProcessed = table.Column(type: "int", nullable: false), ErrorCode = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: true), RowVersion = table.Column(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"); } /// 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"); } } }