using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Microsoft.SelfService.Portal.Core.API.Migrations { /// public partial class AddTemplateVersions : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "DeploymentRuleId", table: "DeploymentBatches", type: "uniqueidentifier", nullable: true) .Annotation("Relational:ColumnOrder", 3); migrationBuilder.CreateTable( name: "TemplateVersions", columns: table => new { Id = table.Column(type: "uniqueidentifier", nullable: false, defaultValueSql: "NEWID()"), TemplateId = table.Column(type: "uniqueidentifier", nullable: false), Version = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), JsonData = table.Column(type: "nvarchar(max)", nullable: false), JsonHash = table.Column(type: "nvarchar(64)", maxLength: 64, nullable: false), SchemaVersion = table.Column(type: "nvarchar(32)", maxLength: 32, nullable: false), IsPublished = table.Column(type: "bit", nullable: false), PublishedAt = table.Column(type: "datetime2", nullable: true), PublishedBy = table.Column(type: "nvarchar(max)", nullable: true), Modified = table.Column(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"), ModifiedBy = table.Column(type: "nvarchar(max)", nullable: false), Created = table.Column(type: "datetime2", nullable: false, defaultValueSql: "GETDATE()"), CreatedBy = table.Column(type: "nvarchar(max)", nullable: false) }, constraints: table => { table.PrimaryKey("PK_TemplateVersions", x => x.Id); table.CheckConstraint("CK_TemplateVersions_JsonData_IsJson", "ISJSON([JsonData]) = 1"); table.ForeignKey( name: "FK_TemplateVersions_Templates_TemplateId", column: x => x.TemplateId, principalTable: "Templates", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.Sql(""" INSERT INTO [TemplateVersions] ([Id], [TemplateId], [Version], [JsonData], [JsonHash], [SchemaVersion], [IsPublished], [PublishedAt], [PublishedBy], [Modified], [ModifiedBy], [Created], [CreatedBy]) SELECT NEWID(), [Id], CASE WHEN [Version] IS NULL OR LTRIM(RTRIM([Version])) = '' THEN '1.0.0' ELSE [Version] END, CASE WHEN ISJSON([JSONData]) = 1 THEN [JSONData] ELSE '{}' END, CONVERT(varchar(64), HASHBYTES('SHA2_256', CONVERT(varbinary(max), CASE WHEN ISJSON([JSONData]) = 1 THEN [JSONData] ELSE '{}' END)), 2), '1.0', 1, SYSUTCDATETIME(), [CreatedBy], [Modified], [ModifiedBy], [Created], [CreatedBy] FROM [Templates] WHERE NOT EXISTS ( SELECT 1 FROM [TemplateVersions] WHERE [TemplateVersions].[TemplateId] = [Templates].[Id] ); """); migrationBuilder.CreateIndex( name: "IX_DeploymentBatches_DeploymentRuleId", table: "DeploymentBatches", column: "DeploymentRuleId"); migrationBuilder.CreateIndex( name: "IX_TemplateVersions_TemplateId", table: "TemplateVersions", column: "TemplateId", unique: true, filter: "[IsPublished] = 1"); migrationBuilder.CreateIndex( name: "IX_TemplateVersions_TemplateId_Version", table: "TemplateVersions", columns: new[] { "TemplateId", "Version" }, unique: true); migrationBuilder.AddForeignKey( name: "FK_DeploymentBatches_DeploymentRules_DeploymentRuleId", table: "DeploymentBatches", column: "DeploymentRuleId", principalTable: "DeploymentRules", principalColumn: "Id", onDelete: ReferentialAction.SetNull); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( name: "FK_DeploymentBatches_DeploymentRules_DeploymentRuleId", table: "DeploymentBatches"); migrationBuilder.DropTable( name: "TemplateVersions"); migrationBuilder.DropIndex( name: "IX_DeploymentBatches_DeploymentRuleId", table: "DeploymentBatches"); migrationBuilder.DropColumn( name: "DeploymentRuleId", table: "DeploymentBatches"); } } }