gehgassi_backend/gehGassi.Persistence/Migrations/20230221123922_WalkConfirmationComplaintAdded2.cs

64 lines
2.8 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
using System;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
/// <inheritdoc />
public partial class WalkConfirmationComplaintAdded2 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "ComplainReason",
table: "Walks",
type: "nvarchar(500)",
maxLength: 500,
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(max)",
oldNullable: true);
var viewName = "tv_WalksWithNames";
var sql = @"CREATE FUNCTION tv_WalksWithNames() RETURNS TABLE AS RETURN
SELECT W.*,
CONCAT(DW.FirstName, ' ' , DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto,
CONCAT(DO.FirstName, ' ' , DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto
FROM Walks W
LEFT JOIN AppUsers DW ON W.DogWalkerId = DW.Id
LEFT JOIN AppUsers DO ON W.DogOwnerId = DO.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");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "ComplainReason",
table: "Walks",
type: "nvarchar(max)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(500)",
oldMaxLength: 500,
oldNullable: true);
var viewName = "tv_WalksWithNames";
var sql = @"CREATE FUNCTION tv_WalksWithNames() RETURNS TABLE AS RETURN
SELECT W.*,
CONCAT(DW.FirstName, ' ' , DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto,
CONCAT(DO.FirstName, ' ' , DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto
FROM Walks W
LEFT JOIN AppUsers DW ON W.DogWalkerId = DW.Id
LEFT JOIN AppUsers DO ON W.DogOwnerId = DO.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");
}
}
}