using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LittleShop.Migrations
{
///
public partial class AddVariantCollectionsAndSalesLedger : Migration
{
///
protected override void Up(MigrationBuilder migrationBuilder)
{
// 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);
");
// 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)
{
// Drop tables
migrationBuilder.Sql("DROP TABLE IF EXISTS SalesLedgers;");
migrationBuilder.Sql("DROP TABLE IF EXISTS VariantCollections;");
// 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
}
}
}