using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class AppVersionAdded : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "AppVersions", columns: table => new { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Platform = table.Column(type: "int", nullable: false), Major = table.Column(type: "int", nullable: false), Minor = table.Column(type: "int", nullable: false), Build = table.Column(type: "int", nullable: true), Revision = table.Column(type: "int", nullable: true), Version = table.Column(type: "nvarchar(100)", maxLength: 100, nullable: false), IsCurrent = table.Column(type: "bit", nullable: false), IsMandatory = table.Column(type: "bit", nullable: false), ReleaseTitleLocalized = table.Column(type: "nvarchar(max)", nullable: true), ReleaseNotesLocalized = table.Column(type: "nvarchar(max)", nullable: true), ReleaseDate = table.Column(type: "datetimeoffset", nullable: false), LastUpdate = table.Column(type: "datetimeoffset", nullable: false), Created = table.Column(type: "datetimeoffset", nullable: false) }, constraints: table => { table.PrimaryKey("PK_AppVersions", x => x.Id); }); var viewName = "tv_AppVersionsWithNames"; var sql = @"CREATE FUNCTION tv_AppVersionsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.AppVersions.*, Isnull(JSON_VALUE(dbo.AppVersions.ReleaseTitleLocalized, '$.' + @language), JSON_VALUE(dbo.AppVersions.ReleaseTitleLocalized, '$.' + @fallback)) AS ReleaseTitle, Isnull(JSON_VALUE(dbo.AppVersions.ReleaseNotesLocalized, '$.' + @language), JSON_VALUE(dbo.AppVersions.ReleaseNotesLocalized, '$.' + @fallback)) AS ReleaseNotes FROM dbo.AppVersions"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.Sql($"{sql} {Environment.NewLine} GO"); } /// protected override void Down(MigrationBuilder migrationBuilder) { var viewName = "tv_AppVersionsWithNames"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.DropTable( name: "AppVersions"); } } }