using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class tv_PublicWalkResponseWithNamesEx : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { var viewName = "tv_PublicWalkResponseWithNames"; var sql = @"CREATE FUNCTION tv_PublicWalkResponseWithNames(@blockingAppUserId nvarchar(128)) RETURNS TABLE AS RETURN SELECT PWR.*, CONCAT(DW.FirstName, ' ', DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto, DW.Verified AS DogWalkerVerified, DW.Locked AS DogWalkerLocked, CAST((SELECT CASE WHEN AUB.Id IS NOT NULL THEN 1 ELSE 0 END) AS BIT) AS DogWalkerBlocked FROM PublicWalkResponses PWR LEFT JOIN AppUsers DW ON PWR.DogWalkerId = DW.Id LEFT OUTER JOIN AppUserBlocks AUB ON AUB.BlockedAppUserId = PWR.DogWalkerId AND AUB.BlockingAppUserId = @blockingAppUserId"; 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_PublicWalkResponseWithNames"; var sql = @"CREATE FUNCTION tv_PublicWalkResponseWithNames() RETURNS TABLE AS RETURN SELECT PWR.*, CONCAT(DW.FirstName, ' ', DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto, DW.Verified AS DogWalkerVerified FROM PublicWalkResponses PWR LEFT JOIN AppUsers DW ON PWR.DogWalkerId = DW.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"); } } }