gehgassi_backend/gehGassi.Persistence/Migrations/20221010094524_DogsWithNamesCorrection.cs

41 lines
2.0 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
using System;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class DogsWithNamesCorrection : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
var viewName = "tv_DogsWithNames";
var sql = @"CREATE FUNCTION tv_DogsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Dogs.*,
DR.Name AS DogRaceName,
CONCAT(APP.FirstName, ' ', APP.LastName) AS AppUserName
FROM dbo.Dogs
LEFT JOIN dbo.tv_DogRacesWithNames(@language, @fallback) DR ON dbo.Dogs.DogRaceId = DR.Id
LEFT JOIN dbo.AppUsers APP ON dbo.Dogs.AppUserId = APP.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)
{
var viewName = "tv_DogsWithNames";
var sql = @"CREATE FUNCTION tv_DogsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Dogs.*,
DR.Name AS DogRaceName,
CONCAT(APP.FirstName, ' ', APP.LastName) AS AppUserName
FROM dbo.Dogs
LEFT JOIN dbo.tv_DogRacesWithNames(@language, @fallback) DR ON dbo.Dogs.DogRaceId = DR.Id
LEFT JOIN dbo.AppUsers APP ON dbo.Dogs.AppUserId = APP.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");
}
}
}