gehgassi_backend/gehGassi.Persistence/Migrations/20250225112725_tv_AppUserSubscriptionsWithNamesEx.cs

66 lines
3.4 KiB
C#

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