using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace gehGassi.Persistence.Migrations { /// public partial class tv_AppUserSubscriptionsWithNamesEx : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: "ExpirationDateWithGrace", table: "AppUserSubscriptions", type: "datetimeoffset", nullable: true); migrationBuilder.AddColumn( name: "InAppBillingPurchaseJson", table: "AppUserSubscriptions", type: "nvarchar(max)", nullable: true); 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: "ExpirationDateWithGrace", table: "AppUserSubscriptions"); migrationBuilder.DropColumn( name: "InAppBillingPurchaseJson", 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, 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"); } } }