using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace PowderCoating.Infrastructure.Migrations { /// public partial class AddSubscriptionEnumAndStripeFields : Migration { /// 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( name: "EmailFromName", table: "CompanyPreferences", type: "nvarchar(max)", nullable: true, oldClrType: typeof(string), oldType: "nvarchar(100)", oldMaxLength: 100, oldNullable: true); migrationBuilder.AlterColumn( 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( 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( name: "StripeCustomerId", table: "Companies", type: "nvarchar(max)", nullable: true); migrationBuilder.AddColumn( name: "StripeSubscriptionId", table: "Companies", type: "nvarchar(max)", nullable: true); migrationBuilder.AddColumn( name: "SubscriptionStatus", table: "Companies", type: "int", nullable: false, defaultValue: 0); migrationBuilder.CreateTable( name: "SubscriptionPlanConfigs", columns: table => new { Id = table.Column(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Plan = table.Column(type: "int", nullable: false), DisplayName = table.Column(type: "nvarchar(max)", nullable: false), Description = table.Column(type: "nvarchar(max)", nullable: true), MaxUsers = table.Column(type: "int", nullable: false), MaxActiveJobs = table.Column(type: "int", nullable: false), MaxCustomers = table.Column(type: "int", nullable: false), MonthlyPrice = table.Column(type: "decimal(18,2)", nullable: false), AnnualPrice = table.Column(type: "decimal(18,2)", nullable: false), StripePriceIdMonthly = table.Column(type: "nvarchar(max)", nullable: true), StripePriceIdAnnual = table.Column(type: "nvarchar(max)", nullable: true), IsActive = table.Column(type: "bit", nullable: false), SortOrder = table.Column(type: "int", nullable: false), CompanyId = table.Column(type: "int", nullable: false), CreatedAt = table.Column(type: "datetime2", nullable: false), UpdatedAt = table.Column(type: "datetime2", nullable: true), CreatedBy = table.Column(type: "nvarchar(max)", nullable: true), UpdatedBy = table.Column(type: "nvarchar(max)", nullable: true), IsDeleted = table.Column(type: "bit", nullable: false), DeletedAt = table.Column(type: "datetime2", nullable: true), DeletedBy = table.Column(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)); } /// 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( name: "EmailFromName", table: "CompanyPreferences", type: "nvarchar(100)", maxLength: 100, nullable: true, oldClrType: typeof(string), oldType: "nvarchar(max)", oldNullable: true); migrationBuilder.AlterColumn( 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( 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)); } } }