gehgassi_backend/gehGassi.Persistence/Migrations/20221028052344_WalkStartedAndCompletedDateAdded.cs

61 lines
2.6 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class WalkStartedAndCompletedDateAdded : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "Completed",
table: "Walks",
type: "datetimeoffset",
nullable: true);
migrationBuilder.AddColumn<DateTimeOffset>(
name: "Started",
table: "Walks",
type: "datetimeoffset",
nullable: 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");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Completed",
table: "Walks");
migrationBuilder.DropColumn(
name: "Started",
table: "Walks");
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");
}
}
}