using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class AppUserSubscriptionsPlatformAdded : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "Platform", table: "AppUserSubscriptions", type: "int", nullable: false, defaultValue: 1); var viewName = "tv_AppUserSubscriptionsWithNames"; var sql = @"CREATE FUNCTION tv_AppUserSubscriptionsWithNames(@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT AUS.*, CONCAT(A.FirstName, A.LastName, ' ') AS AppUserName, A.Photo AS AppUserPhoto, A.Type AS AppUserType, S.Code AS SubscriptionCode, S.Length AS SubscriptionLength, S.Price AS SubscriptionPrice, S.AppMode AS SubscriptionAppMode, S.GracePeriodInDays AS SubscriptionGracePeriodInDays, Isnull(JSON_VALUE(S.NameLocalized, '$.' + @language), JSON_VALUE(S.NameLocalized, '$.' + @fallback)) AS SubscriptionName FROM AppUserSubscriptions AUS LEFT JOIN Subscriptions S ON S.Id = AUS.SubscriptionId LEFT JOIN AppUsers A ON A.Id = AUS.AppUserId"; 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: "Platform", table: "AppUserSubscriptions"); var viewName = "tv_AppUserSubscriptionsWithNames"; var sql = @"CREATE FUNCTION tv_AppUserSubscriptionsWithNames(@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN SELECT AUS.*, CONCAT(A.FirstName, A.LastName, ' ') AS AppUserName, A.Photo AS AppUserPhoto, A.Type AS AppUserType, S.Code AS SubscriptionCode, S.Length AS SubscriptionLength, S.Price AS SubscriptionPrice, S.AppMode AS SubscriptionAppMode, S.GracePeriodInDays AS SubscriptionGracePeriodInDays, Isnull(JSON_VALUE(S.NameLocalized, '$.' + @language), JSON_VALUE(S.NameLocalized, '$.' + @fallback)) AS SubscriptionName FROM AppUserSubscriptions AUS LEFT JOIN Subscriptions S ON S.Id = AUS.SubscriptionId LEFT JOIN AppUsers A ON A.Id = AUS.AppUserId"; 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"); } } }