gehgassi_backend/gehGassi.Persistence/Migrations/20230304134420_AppUserUpdateViews.cs

82 lines
4.0 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
/// <inheritdoc />
public partial class AppUserUpdateViews : 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");
viewName = "vw_DogWalkers";
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");
}
/// <inheritdoc />
protected override void Down(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");
viewName = "vw_DogWalkers";
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");
}
}
}