using Microsoft.EntityFrameworkCore.Migrations; using NetTopologySuite.Geometries; using System; #nullable disable namespace gehGassi.Persistence.Migrations { public partial class PinLocation : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropColumn( name: "Lat", table: "Pins"); migrationBuilder.DropColumn( name: "Lng", table: "Pins"); migrationBuilder.AddColumn( name: "Location", table: "Pins", type: "geography", nullable: true); var viewName = "tv_PinsWithNames"; var sql = @"CREATE FUNCTION tv_PinsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Pins.*, C.Name AS CustomerName, Isnull(JSON_VALUE(dbo.Pins.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Pins.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Pins.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Pins.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Pins.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage, JSON_VALUE(dbo.Pins.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage FROM dbo.Pins LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Pins.CustomerId = C.Id"; 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) { migrationBuilder.DropColumn( name: "Location", table: "Pins"); migrationBuilder.AddColumn( name: "Lat", table: "Pins", type: "float", nullable: false, defaultValue: 0.0); migrationBuilder.AddColumn( name: "Lng", table: "Pins", type: "float", nullable: false, defaultValue: 0.0); var viewName = "tv_PinsWithNames"; var sql = @"CREATE FUNCTION tv_PinsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Pins.*, C.Name AS CustomerName, Isnull(JSON_VALUE(dbo.Pins.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Pins.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Pins.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Pins.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Pins.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage, JSON_VALUE(dbo.Pins.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage FROM dbo.Pins LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Pins.CustomerId = C.Id"; 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"); } } }