using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class AppUserAnonymizedAdded : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "Anonymized", table: "AppUsers", type: "bit", nullable: false, defaultValue: false); migrationBuilder.AddColumn( name: "AnonymizedDate", table: "AppUsers", type: "datetimeoffset", nullable: true); migrationBuilder.AddColumn( name: "LockedCount", table: "AppUsers", type: "int", nullable: false, defaultValue: 0); var viewName = "vw_DogWalkers"; var sql = @"Select AU.*, DP.Radius, DP.About, DP.AllowedRequests, DP.PuppyAllowed, DP.DifficultAllowed, DP.AggressionLevel, DP.AcceptedSize, DP.PriceWalk, DP.PriceDay, DP.PriceSitting, DP.NotAvailable, DP.NotAvailableInfo from AppUsers AU JOIN DogWalkerProfiles DP ON DP.Id = AU.Id WHERE AU.Type != 10"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.views WHERE Name = '{viewName}')) BEGIN DROP VIEW [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.Sql($"CREATE VIEW [dbo].[{viewName}] AS {sql} {Environment.NewLine} GO"); viewName = "tv_AppUserBlocksWithNames"; sql = @"CREATE FUNCTION tv_AppUserBlocksWithNames (@blockingAppUserId nvarchar(128)) RETURNS TABLE AS RETURN Select AUB.*, AU.Type, AU.FirstName, AU.LastName, AU.Photo, AU.Address_City AS LocationHint, AU.Location, AU.Locked, AU.Deleted FROM AppUserBlocks AUB LEFT JOIN AppUsers AU ON AU.Id = AUB.BlockedAppUserId WHERE 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) { migrationBuilder.DropColumn( name: "Anonymized", table: "AppUsers"); migrationBuilder.DropColumn( name: "AnonymizedDate", table: "AppUsers"); migrationBuilder.DropColumn( name: "LockedCount", table: "AppUsers"); var viewName = "vw_DogWalkers"; var sql = @"Select AU.*, DP.Radius, DP.About, DP.AllowedRequests, DP.PuppyAllowed, DP.DifficultAllowed, DP.AggressionLevel, DP.AcceptedSize, DP.PriceWalk, DP.PriceDay, DP.PriceSitting, DP.NotAvailable, DP.NotAvailableInfo from AppUsers AU JOIN DogWalkerProfiles DP ON DP.Id = AU.Id WHERE AU.Type != 10"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.views WHERE Name = '{viewName}')) BEGIN DROP VIEW [dbo].[{viewName}] END {Environment.NewLine} GO"); migrationBuilder.Sql($"CREATE VIEW [dbo].[{viewName}] AS {sql} {Environment.NewLine} GO"); viewName = "tv_AppUserBlocksWithNames"; sql = @"CREATE FUNCTION tv_AppUserBlocksWithNames (@blockingAppUserId nvarchar(128)) RETURNS TABLE AS RETURN Select AUB.*, AU.Type, AU.FirstName, AU.LastName, AU.Photo, AU.Address_City AS LocationHint, AU.Location, AU.Locked, AU.Deleted FROM AppUserBlocks AUB LEFT JOIN AppUsers AU ON AU.Id = AUB.BlockedAppUserId WHERE 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"); } } }