gehgassi_backend/gehGassi.Persistence/Migrations/20221020083125_PublicWalkRequestStatusAdded.cs

66 lines
3.0 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class PublicWalkRequestStatusAdded : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "PlacedDate",
table: "PublicWalkRequests",
type: "datetimeoffset",
nullable: true);
migrationBuilder.AddColumn<int>(
name: "Status",
table: "PublicWalkRequests",
type: "int",
nullable: false,
defaultValue: 0);
var viewName = "tv_PublicWalkRequestWithNames";
var sql = @"CREATE FUNCTION tv_PublicWalkRequestWithNames() RETURNS TABLE AS RETURN
SELECT PWR.*,
CONCAT(DO.FirstName, ' ', DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto,
D.Name As DogName, D.Photo AS DogPhoto,
CONCAT(DW.FirstName, ' ', DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto
FROM PublicWalkRequests PWR
LEFT JOIN AppUsers DO ON PWR.DogOwnerId = DO.Id
LEFT JOIN Dogs D ON PWR.DogId = D.Id
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");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PlacedDate",
table: "PublicWalkRequests");
migrationBuilder.DropColumn(
name: "Status",
table: "PublicWalkRequests");
var viewName = "tv_PublicWalkRequestWithNames";
var sql = @"CREATE FUNCTION tv_PublicWalkRequestWithNames() RETURNS TABLE AS RETURN
SELECT PWR.*,
CONCAT(DO.FirstName, ' ', DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto,
D.Name As DogName, D.Photo AS DogPhoto,
CONCAT(DW.FirstName, ' ', DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto
FROM PublicWalkRequests PWR
LEFT JOIN AppUsers DO ON PWR.DogOwnerId = DO.Id
LEFT JOIN Dogs D ON PWR.DogId = D.Id
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");
}
}
}