using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { public partial class PagesWithNamesAdded : Migration { protected override void Up(MigrationBuilder migrationBuilder) { var viewName = "tv_PagesWithNames"; var sql = @"CREATE FUNCTION tv_PagesWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT dbo.Pages.*, Isnull(JSON_VALUE(dbo.Pages.ContentLocalized, '$.' + @language), JSON_VALUE(dbo.Pages.ContentLocalized, '$.' + @fallback)) AS Content FROM dbo.Pages"; 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_PagesWithNames"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); } } }