using Microsoft.EntityFrameworkCore.Migrations; using NetTopologySuite.Geometries; using System; #nullable disable namespace gehGassi.Persistence.Migrations { public partial class ListingListingAddressAdded : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "ListingAddress_AddressLine1", table: "Listings", type: "nvarchar(100)", maxLength: 100, nullable: true); migrationBuilder.AddColumn( name: "ListingAddress_AddressLine2", table: "Listings", type: "nvarchar(100)", maxLength: 100, nullable: true); migrationBuilder.AddColumn( name: "ListingAddress_City", table: "Listings", type: "nvarchar(100)", maxLength: 100, nullable: true); migrationBuilder.AddColumn( name: "ListingAddress_CountryCode", table: "Listings", type: "nvarchar(2)", maxLength: 2, nullable: true); migrationBuilder.AddColumn( name: "ListingAddress_State", table: "Listings", type: "nvarchar(100)", maxLength: 100, nullable: true); migrationBuilder.AddColumn( name: "ListingAddress_Zip", table: "Listings", type: "nvarchar(20)", maxLength: 20, nullable: true); migrationBuilder.AddColumn( name: "ListingLocation", table: "Listings", type: "geography", nullable: true); migrationBuilder.Sql("UPDATE Listings SET ListingAddress_State = Address_State, ListingAddress_CountryCode = Address_CountryCode"); var viewName = "tv_ListingsWithNames"; var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Listings.*, C.Name AS CustomerName, BR.Name AS BranchName, BR.OnlineStatus AS BranchOnlineStatus, Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage, JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage FROM dbo.Listings LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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: "ListingAddress_AddressLine1", table: "Listings"); migrationBuilder.DropColumn( name: "ListingAddress_AddressLine2", table: "Listings"); migrationBuilder.DropColumn( name: "ListingAddress_City", table: "Listings"); migrationBuilder.DropColumn( name: "ListingAddress_CountryCode", table: "Listings"); migrationBuilder.DropColumn( name: "ListingAddress_State", table: "Listings"); migrationBuilder.DropColumn( name: "ListingAddress_Zip", table: "Listings"); migrationBuilder.DropColumn( name: "ListingLocation", table: "Listings"); var viewName = "tv_ListingsWithNames"; var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Listings.*, C.Name AS CustomerName, BR.Name AS BranchName, BR.OnlineStatus AS BranchOnlineStatus, Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name, Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description, JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage, JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage FROM dbo.Listings LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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"); } } }