235 lines
9.5 KiB
C#
235 lines
9.5 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace PowderCoating.Infrastructure.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class AddSubscriptionEnumAndStripeFields : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
// Ensure columns exist before altering — they may be missing if the orphaned
|
|
// AddEmailSenderToPreferences migration (missing its Designer file) was never applied.
|
|
migrationBuilder.Sql(@"
|
|
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = 'CompanyPreferences' AND COLUMN_NAME = 'EmailFromAddress')
|
|
ALTER TABLE [CompanyPreferences] ADD [EmailFromAddress] nvarchar(max) NULL;
|
|
");
|
|
|
|
migrationBuilder.Sql(@"
|
|
IF NOT EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_NAME = 'CompanyPreferences' AND COLUMN_NAME = 'EmailFromName')
|
|
ALTER TABLE [CompanyPreferences] ADD [EmailFromName] nvarchar(max) NULL;
|
|
");
|
|
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "EmailFromName",
|
|
table: "CompanyPreferences",
|
|
type: "nvarchar(max)",
|
|
nullable: true,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(100)",
|
|
oldMaxLength: 100,
|
|
oldNullable: true);
|
|
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "EmailFromAddress",
|
|
table: "CompanyPreferences",
|
|
type: "nvarchar(max)",
|
|
nullable: true,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(100)",
|
|
oldMaxLength: 100,
|
|
oldNullable: true);
|
|
|
|
// Data migration: convert SubscriptionPlan string → int
|
|
// Add temp int column, populate from string, drop old, rename
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "SubscriptionPlanNew",
|
|
table: "Companies",
|
|
type: "int",
|
|
nullable: false,
|
|
defaultValue: 0);
|
|
|
|
migrationBuilder.Sql(@"
|
|
UPDATE Companies SET SubscriptionPlanNew =
|
|
CASE SubscriptionPlan
|
|
WHEN 'Enterprise' THEN 2
|
|
WHEN 'Pro' THEN 1
|
|
WHEN 'Basic' THEN 0
|
|
ELSE 0
|
|
END
|
|
");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "SubscriptionPlan",
|
|
table: "Companies");
|
|
|
|
migrationBuilder.RenameColumn(
|
|
name: "SubscriptionPlanNew",
|
|
table: "Companies",
|
|
newName: "SubscriptionPlan");
|
|
|
|
migrationBuilder.AddColumn<string>(
|
|
name: "StripeCustomerId",
|
|
table: "Companies",
|
|
type: "nvarchar(max)",
|
|
nullable: true);
|
|
|
|
migrationBuilder.AddColumn<string>(
|
|
name: "StripeSubscriptionId",
|
|
table: "Companies",
|
|
type: "nvarchar(max)",
|
|
nullable: true);
|
|
|
|
migrationBuilder.AddColumn<int>(
|
|
name: "SubscriptionStatus",
|
|
table: "Companies",
|
|
type: "int",
|
|
nullable: false,
|
|
defaultValue: 0);
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "SubscriptionPlanConfigs",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<int>(type: "int", nullable: false)
|
|
.Annotation("SqlServer:Identity", "1, 1"),
|
|
Plan = table.Column<int>(type: "int", nullable: false),
|
|
DisplayName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
|
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
MaxUsers = table.Column<int>(type: "int", nullable: false),
|
|
MaxActiveJobs = table.Column<int>(type: "int", nullable: false),
|
|
MaxCustomers = table.Column<int>(type: "int", nullable: false),
|
|
MonthlyPrice = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
|
|
AnnualPrice = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
|
|
StripePriceIdMonthly = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
StripePriceIdAnnual = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
IsActive = table.Column<bool>(type: "bit", nullable: false),
|
|
SortOrder = table.Column<int>(type: "int", nullable: false),
|
|
CompanyId = table.Column<int>(type: "int", nullable: false),
|
|
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
|
|
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
|
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
|
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
|
|
DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
|
|
},
|
|
constraints: table =>
|
|
{
|
|
table.PrimaryKey("PK_SubscriptionPlanConfigs", x => x.Id);
|
|
});
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 1,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 24, 1, 51, 32, 257, DateTimeKind.Utc).AddTicks(7721));
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 2,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 24, 1, 51, 32, 257, DateTimeKind.Utc).AddTicks(7726));
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 3,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 24, 1, 51, 32, 257, DateTimeKind.Utc).AddTicks(7728));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropTable(
|
|
name: "SubscriptionPlanConfigs");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "StripeCustomerId",
|
|
table: "Companies");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "StripeSubscriptionId",
|
|
table: "Companies");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "SubscriptionStatus",
|
|
table: "Companies");
|
|
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "EmailFromName",
|
|
table: "CompanyPreferences",
|
|
type: "nvarchar(100)",
|
|
maxLength: 100,
|
|
nullable: true,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(max)",
|
|
oldNullable: true);
|
|
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "EmailFromAddress",
|
|
table: "CompanyPreferences",
|
|
type: "nvarchar(100)",
|
|
maxLength: 100,
|
|
nullable: true,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(max)",
|
|
oldNullable: true);
|
|
|
|
// Reverse data migration: convert SubscriptionPlan int → string
|
|
migrationBuilder.AddColumn<string>(
|
|
name: "SubscriptionPlanStr",
|
|
table: "Companies",
|
|
type: "nvarchar(max)",
|
|
nullable: true);
|
|
|
|
migrationBuilder.Sql(@"
|
|
UPDATE Companies SET SubscriptionPlanStr =
|
|
CASE SubscriptionPlan
|
|
WHEN 2 THEN 'Enterprise'
|
|
WHEN 1 THEN 'Pro'
|
|
ELSE 'Basic'
|
|
END
|
|
");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "SubscriptionPlan",
|
|
table: "Companies");
|
|
|
|
migrationBuilder.RenameColumn(
|
|
name: "SubscriptionPlanStr",
|
|
table: "Companies",
|
|
newName: "SubscriptionPlan");
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 1,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 22, 13, 30, 59, 414, DateTimeKind.Utc).AddTicks(7610));
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 2,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 22, 13, 30, 59, 414, DateTimeKind.Utc).AddTicks(7617));
|
|
|
|
migrationBuilder.UpdateData(
|
|
table: "PricingTiers",
|
|
keyColumn: "Id",
|
|
keyValue: 3,
|
|
column: "CreatedAt",
|
|
value: new DateTime(2026, 2, 22, 13, 30, 59, 414, DateTimeKind.Utc).AddTicks(7619));
|
|
}
|
|
}
|
|
}
|