fix(migrations): make Phase6 completion slide-over migration idempotent

Use IF EXISTS / IF COL_LENGTH guards and explicit EF migration metadata
so partial environments can apply Phase 6 safely.
This commit is contained in:
Arthur Bassi 2026-07-17 10:36:32 -03:00
parent 2c26ead193
commit 3e2952f8a0

View file

@ -1,4 +1,5 @@
using System; using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable #nullable disable
@ -6,45 +7,52 @@ using Microsoft.EntityFrameworkCore.Migrations;
namespace Data.SeaHavenIndustries.Migrations namespace Data.SeaHavenIndustries.Migrations
{ {
/// <inheritdoc /> /// <inheritdoc />
[DbContext(typeof(ApplicationDbContext))]
[Migration("20260625120000_Phase6_CompletionSlideOver")]
public partial class Phase6_CompletionSlideOver : Migration public partial class Phase6_CompletionSlideOver : Migration
{ {
/// <inheritdoc /> /// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.AddColumn<int>( migrationBuilder.Sql(@"
name: "Category", IF COL_LENGTH('workOrderAttachments', 'Category') IS NULL
table: "workOrderAttachments", BEGIN
type: "int", ALTER TABLE workOrderAttachments ADD Category int NULL;
nullable: true); END
");
migrationBuilder.CreateTable( migrationBuilder.Sql(@"
name: "CompletionDocTemplates", IF OBJECT_ID(N'dbo.CompletionDocTemplates', N'U') IS NULL
columns: table => new BEGIN
{ CREATE TABLE CompletionDocTemplates (
Id = table.Column<int>(type: "int", nullable: false) Id int NOT NULL IDENTITY(1,1),
.Annotation("SqlServer:Identity", "1, 1"), Name nvarchar(max) NOT NULL,
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), ServiceKey nvarchar(450) NOT NULL,
ServiceKey = table.Column<string>(type: "nvarchar(450)", nullable: false), WorkOrderType int NULL,
WorkOrderType = table.Column<int>(type: "int", nullable: true), TemplateUrl nvarchar(max) NOT NULL,
TemplateUrl = table.Column<string>(type: "nvarchar(max)", nullable: false), IsActive bit NOT NULL,
IsActive = table.Column<bool>(type: "bit", nullable: false), IsDeleted bit NULL,
IsDeleted = table.Column<bool>(type: "bit", nullable: true), createdby nvarchar(max) NULL,
createdby = table.Column<string>(type: "nvarchar(max)", nullable: true), DeleterUserId nvarchar(max) NULL,
DeleterUserId = table.Column<string>(type: "nvarchar(max)", nullable: true), DeletionTime datetime2 NULL,
DeletionTime = table.Column<DateTime>(type: "datetime2", nullable: true), CreatedDate datetime2 NULL,
CreatedDate = table.Column<DateTime>(type: "datetime2", nullable: true), LastModificationTime datetime2 NULL,
LastModificationTime = table.Column<DateTime>(type: "datetime2", nullable: true), LastModifierUserId int NULL,
LastModifierUserId = table.Column<int>(type: "int", nullable: true) CONSTRAINT PK_CompletionDocTemplates PRIMARY KEY (Id)
}, );
constraints: table => END
{ ");
table.PrimaryKey("PK_CompletionDocTemplates", x => x.Id);
});
migrationBuilder.CreateIndex( migrationBuilder.Sql(@"
name: "IX_CompletionDocTemplates_ServiceKey_IsActive", IF NOT EXISTS (
table: "CompletionDocTemplates", SELECT 1 FROM sys.indexes
columns: new[] { "ServiceKey", "IsActive" }); WHERE name = N'IX_CompletionDocTemplates_ServiceKey_IsActive'
AND object_id = OBJECT_ID(N'dbo.CompletionDocTemplates'))
BEGIN
CREATE INDEX IX_CompletionDocTemplates_ServiceKey_IsActive
ON CompletionDocTemplates (ServiceKey, IsActive);
END
");
migrationBuilder.Sql(@" migrationBuilder.Sql(@"
UPDATE workOrders UPDATE workOrders
@ -66,12 +74,15 @@ namespace Data.SeaHavenIndustries.Migrations
AND LTRIM(RTRIM(SignOffAttachment)) <> ''; AND LTRIM(RTRIM(SignOffAttachment)) <> '';
"); ");
migrationBuilder.DropTable( migrationBuilder.Sql(@"
name: "CompletionDocTemplates"); IF OBJECT_ID(N'dbo.CompletionDocTemplates', N'U') IS NOT NULL
DROP TABLE CompletionDocTemplates;
");
migrationBuilder.DropColumn( migrationBuilder.Sql(@"
name: "Category", IF COL_LENGTH('workOrderAttachments', 'Category') IS NOT NULL
table: "workOrderAttachments"); ALTER TABLE workOrderAttachments DROP COLUMN Category;
");
} }
} }
} }