diff --git a/LittleShop/Migrations/20250928014850_AddVariantCollectionsAndSalesLedger.cs b/LittleShop/Migrations/20250928014850_AddVariantCollectionsAndSalesLedger.cs index 45dd10e..720d4da 100644 --- a/LittleShop/Migrations/20250928014850_AddVariantCollectionsAndSalesLedger.cs +++ b/LittleShop/Migrations/20250928014850_AddVariantCollectionsAndSalesLedger.cs @@ -10,31 +10,61 @@ namespace LittleShop.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { - // Add variant columns to Products table - migrationBuilder.AddColumn( - name: "VariantCollectionId", - table: "Products", - type: "TEXT", - nullable: true); + // Create VariantCollections table (using raw SQL for IF NOT EXISTS support) + migrationBuilder.Sql(@" + CREATE TABLE IF NOT EXISTS VariantCollections ( + Id TEXT PRIMARY KEY NOT NULL, + Name TEXT NOT NULL, + PropertiesJson TEXT NOT NULL DEFAULT '[]', + IsActive INTEGER NOT NULL DEFAULT 1, + CreatedAt TEXT NOT NULL, + UpdatedAt TEXT NOT NULL + ); + CREATE INDEX IF NOT EXISTS IX_VariantCollections_Name ON VariantCollections(Name); + CREATE INDEX IF NOT EXISTS IX_VariantCollections_IsActive ON VariantCollections(IsActive); + "); - migrationBuilder.AddColumn( - name: "VariantsJson", - table: "Products", - type: "TEXT", - nullable: true); + // Create SalesLedgers table + migrationBuilder.Sql(@" + CREATE TABLE IF NOT EXISTS SalesLedgers ( + Id TEXT PRIMARY KEY NOT NULL, + OrderId TEXT NOT NULL, + ProductId TEXT NOT NULL, + ProductName TEXT NOT NULL, + Quantity INTEGER NOT NULL, + SalePriceFiat TEXT NOT NULL, + FiatCurrency TEXT NOT NULL DEFAULT 'GBP', + SalePriceBTC TEXT, + Cryptocurrency TEXT, + SoldAt TEXT NOT NULL, + FOREIGN KEY (OrderId) REFERENCES Orders(Id), + FOREIGN KEY (ProductId) REFERENCES Products(Id) + ); + CREATE INDEX IF NOT EXISTS IX_SalesLedgers_OrderId ON SalesLedgers(OrderId); + CREATE INDEX IF NOT EXISTS IX_SalesLedgers_ProductId ON SalesLedgers(ProductId); + CREATE INDEX IF NOT EXISTS IX_SalesLedgers_SoldAt ON SalesLedgers(SoldAt); + CREATE INDEX IF NOT EXISTS IX_SalesLedgers_ProductId_SoldAt ON SalesLedgers(ProductId, SoldAt); + "); + + // Add variant columns to Products table (ignore if already exists) + migrationBuilder.Sql(@" + ALTER TABLE Products ADD COLUMN VariantCollectionId TEXT; + ", suppressTransaction: true); + + migrationBuilder.Sql(@" + ALTER TABLE Products ADD COLUMN VariantsJson TEXT; + ", suppressTransaction: true); } /// protected override void Down(MigrationBuilder migrationBuilder) { - // Remove variant columns from Products table - migrationBuilder.DropColumn( - name: "VariantCollectionId", - table: "Products"); + // Drop tables + migrationBuilder.Sql("DROP TABLE IF EXISTS SalesLedgers;"); + migrationBuilder.Sql("DROP TABLE IF EXISTS VariantCollections;"); - migrationBuilder.DropColumn( - name: "VariantsJson", - table: "Products"); + // Note: SQLite doesn't support DROP COLUMN easily, so we leave the columns + // In a real scenario, you'd need to recreate the Products table } } }