gehgassi_backend/gehGassi.Persistence/Migrations/20220830072136_ListingWithNamesEx.cs

49 lines
3.2 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
using System;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class ListingWithNamesEx : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
var viewName = "tv_ListingsWithNames";
var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Listings.*,
C.Name AS CustomerName,
BR.Name AS BranchName,
Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name,
Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description,
JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
FROM dbo.Listings
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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_ListingsWithNames";
var sql = @"CREATE FUNCTION tv_ListingsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Listings.*,
C.Name AS CustomerName,
BR.Name AS BranchName,
Isnull(JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.NameLocalized, '$.' + @fallback)) AS Name,
Isnull(JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Listings.DescriptionLocalized, '$.' + @fallback)) AS Description,
JSON_VALUE(dbo.Listings.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
JSON_VALUE(dbo.Listings.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
FROM dbo.Listings
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Listings.CustomerId = C.Id
LEFT JOIN dbo.tv_BranchesWithNames(@language, @fallback) BR ON dbo.Listings.BranchId = BR.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");
}
}
}