Files
ERP_Distribution/Distribution/Migrations/20261001080630_AddItemTrackingConfig.cs

506 lines
22 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace Distribution.Migrations
{
/// <inheritdoc />
public partial class AddItemTrackingConfig : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<Guid>(
name: "FK_ItemBatchId",
table: "StockMovements",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");
migrationBuilder.AddColumn<Guid>(
name: "FK_ItemSerialNumberId",
table: "StockMovements",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");
migrationBuilder.AddColumn<Guid>(
name: "FK_ItemBatchId",
table: "StockBalances",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");
migrationBuilder.AddColumn<Guid>(
name: "FK_ItemSerialNumberId",
table: "StockBalances",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci");
migrationBuilder.AddColumn<bool>(
name: "AllowCustomWarrantyPeriod",
table: "Items",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "HasWarranty",
table: "Items",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "TracksBatches",
table: "Items",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "TracksExpiryDate",
table: "Items",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<bool>(
name: "TracksSerialNumbers",
table: "Items",
type: "tinyint(1)",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<int>(
name: "WarrantyPeriodDays",
table: "Items",
type: "int",
nullable: true);
migrationBuilder.CreateTable(
name: "ItemBatches",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_ItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
BatchNumber = table.Column<string>(type: "varchar(255)", nullable: true)
.Annotation("MySql:CharSet", "utf8mb4"),
ManufactureDate = table.Column<DateTime>(type: "datetime(6)", nullable: true),
ExpiryDate = table.Column<DateTime>(type: "datetime(6)", nullable: true),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ItemBatches", x => x.Id);
table.ForeignKey(
name: "FK_ItemBatches_Items_FK_ItemId",
column: x => x.FK_ItemId,
principalTable: "Items",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "ItemSerialNumbers",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_ItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
SerialNumber = table.Column<string>(type: "varchar(255)", nullable: false)
.Annotation("MySql:CharSet", "utf8mb4"),
Status = table.Column<int>(type: "int", nullable: false),
CurrentLocationId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
CurrentLocationType = table.Column<int>(type: "int", nullable: true),
ReceivedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
WarrantyPeriodDaysOverride = table.Column<int>(type: "int", nullable: true),
WarrantyExpiresAt = table.Column<DateTime>(type: "datetime(6)", nullable: true),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ItemSerialNumbers", x => x.Id);
table.ForeignKey(
name: "FK_ItemSerialNumbers_Items_FK_ItemId",
column: x => x.FK_ItemId,
principalTable: "Items",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "GoodsReceiptItemBatches",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_GoodsReceiptItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_ItemBatchId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
Quantity = table.Column<int>(type: "int", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GoodsReceiptItemBatches", x => x.Id);
table.ForeignKey(
name: "FK_GoodsReceiptItemBatches_GoodsReceiptItems_FK_GoodsReceiptIte~",
column: x => x.FK_GoodsReceiptItemId,
principalTable: "GoodsReceiptItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_GoodsReceiptItemBatches_ItemBatches_FK_ItemBatchId",
column: x => x.FK_ItemBatchId,
principalTable: "ItemBatches",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateTable(
name: "GoodsReceiptItemSerials",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_GoodsReceiptItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
FK_ItemSerialNumberId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
IsActive = table.Column<bool>(type: "tinyint(1)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_GoodsReceiptItemSerials", x => x.Id);
table.ForeignKey(
name: "FK_GoodsReceiptItemSerials_GoodsReceiptItems_FK_GoodsReceiptIte~",
column: x => x.FK_GoodsReceiptItemId,
principalTable: "GoodsReceiptItems",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "FK_GoodsReceiptItemSerials_ItemSerialNumbers_FK_ItemSerialNumbe~",
column: x => x.FK_ItemSerialNumberId,
principalTable: "ItemSerialNumbers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
})
.Annotation("MySql:CharSet", "utf8mb4");
migrationBuilder.UpdateData(
table: "Items",
keyColumn: "Id",
keyValue: new Guid("23000000-0000-0000-0000-000000000001"),
columns: new[] { "AllowCustomWarrantyPeriod", "HasWarranty", "TracksBatches", "TracksExpiryDate", "TracksSerialNumbers", "WarrantyPeriodDays" },
values: new object[] { false, false, false, false, false, null });
migrationBuilder.UpdateData(
table: "Items",
keyColumn: "Id",
keyValue: new Guid("23000000-0000-0000-0000-000000000002"),
columns: new[] { "AllowCustomWarrantyPeriod", "HasWarranty", "TracksBatches", "TracksExpiryDate", "TracksSerialNumbers", "WarrantyPeriodDays" },
values: new object[] { false, false, false, false, false, null });
migrationBuilder.UpdateData(
table: "Items",
keyColumn: "Id",
keyValue: new Guid("23000000-0000-0000-0000-000000000003"),
columns: new[] { "AllowCustomWarrantyPeriod", "HasWarranty", "TracksBatches", "TracksExpiryDate", "TracksSerialNumbers", "WarrantyPeriodDays" },
values: new object[] { false, false, false, false, false, null });
migrationBuilder.UpdateData(
table: "Items",
keyColumn: "Id",
keyValue: new Guid("23000000-0000-0000-0000-000000000004"),
columns: new[] { "AllowCustomWarrantyPeriod", "HasWarranty", "TracksBatches", "TracksExpiryDate", "TracksSerialNumbers", "WarrantyPeriodDays" },
values: new object[] { false, false, false, false, false, null });
migrationBuilder.UpdateData(
table: "StockBalances",
keyColumn: "Id",
keyValue: new Guid("61000000-0000-0000-0000-000000000001"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockBalances",
keyColumn: "Id",
keyValue: new Guid("61000000-0000-0000-0000-000000000002"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockBalances",
keyColumn: "Id",
keyValue: new Guid("61000000-0000-0000-0000-000000000003"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000001"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000002"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.UpdateData(
table: "StockMovements",
keyColumn: "Id",
keyValue: new Guid("62000000-0000-0000-0000-000000000003"),
columns: new[] { "FK_ItemBatchId", "FK_ItemSerialNumberId" },
values: new object[] { null, null });
migrationBuilder.InsertData(
table: "SysConfigs",
columns: new[] { "Key", "UpdatedAt", "UpdatedBy", "Value" },
values: new object[,]
{
{ "Feature.BatchTracking", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "false" },
{ "Feature.CustomWarrantyOverride", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "false" },
{ "Feature.DefaultWarrantyPeriodDays", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "0" },
{ "Feature.ExpiryDateTracking", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "false" },
{ "Feature.SerialNumberTracking", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "false" },
{ "Feature.WarrantyTracking", new DateTime(2026, 10, 1, 0, 0, 0, 0, DateTimeKind.Unspecified), null, "false" }
});
migrationBuilder.CreateIndex(
name: "IX_StockMovements_FK_ItemBatchId",
table: "StockMovements",
column: "FK_ItemBatchId");
migrationBuilder.CreateIndex(
name: "IX_StockMovements_FK_ItemSerialNumberId",
table: "StockMovements",
column: "FK_ItemSerialNumberId");
migrationBuilder.CreateIndex(
name: "IX_StockBalances_FK_ItemBatchId",
table: "StockBalances",
column: "FK_ItemBatchId");
migrationBuilder.CreateIndex(
name: "IX_StockBalances_FK_ItemId_LocationId_FK_ItemBatchId_FK_ItemSer~",
table: "StockBalances",
columns: new[] { "FK_ItemId", "LocationId", "FK_ItemBatchId", "FK_ItemSerialNumberId" },
unique: true);
// The old 2-column index must be dropped only after the new composite index exists,
// since MySQL won't drop an index that's the only one backing a FK (FK_ItemId here).
migrationBuilder.DropIndex(
name: "IX_StockBalances_FK_ItemId_LocationId",
table: "StockBalances");
migrationBuilder.CreateIndex(
name: "IX_StockBalances_FK_ItemSerialNumberId",
table: "StockBalances",
column: "FK_ItemSerialNumberId");
migrationBuilder.CreateIndex(
name: "IX_GoodsReceiptItemBatches_FK_GoodsReceiptItemId",
table: "GoodsReceiptItemBatches",
column: "FK_GoodsReceiptItemId");
migrationBuilder.CreateIndex(
name: "IX_GoodsReceiptItemBatches_FK_ItemBatchId",
table: "GoodsReceiptItemBatches",
column: "FK_ItemBatchId");
migrationBuilder.CreateIndex(
name: "IX_GoodsReceiptItemSerials_FK_GoodsReceiptItemId",
table: "GoodsReceiptItemSerials",
column: "FK_GoodsReceiptItemId");
migrationBuilder.CreateIndex(
name: "IX_GoodsReceiptItemSerials_FK_ItemSerialNumberId",
table: "GoodsReceiptItemSerials",
column: "FK_ItemSerialNumberId");
migrationBuilder.CreateIndex(
name: "IX_ItemBatches_FK_ItemId_BatchNumber",
table: "ItemBatches",
columns: new[] { "FK_ItemId", "BatchNumber" });
migrationBuilder.CreateIndex(
name: "IX_ItemSerialNumbers_FK_ItemId_SerialNumber",
table: "ItemSerialNumbers",
columns: new[] { "FK_ItemId", "SerialNumber" },
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_StockBalances_ItemBatches_FK_ItemBatchId",
table: "StockBalances",
column: "FK_ItemBatchId",
principalTable: "ItemBatches",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_StockBalances_ItemSerialNumbers_FK_ItemSerialNumberId",
table: "StockBalances",
column: "FK_ItemSerialNumberId",
principalTable: "ItemSerialNumbers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_StockMovements_ItemBatches_FK_ItemBatchId",
table: "StockMovements",
column: "FK_ItemBatchId",
principalTable: "ItemBatches",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
migrationBuilder.AddForeignKey(
name: "FK_StockMovements_ItemSerialNumbers_FK_ItemSerialNumberId",
table: "StockMovements",
column: "FK_ItemSerialNumberId",
principalTable: "ItemSerialNumbers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_StockBalances_ItemBatches_FK_ItemBatchId",
table: "StockBalances");
migrationBuilder.DropForeignKey(
name: "FK_StockBalances_ItemSerialNumbers_FK_ItemSerialNumberId",
table: "StockBalances");
migrationBuilder.DropForeignKey(
name: "FK_StockMovements_ItemBatches_FK_ItemBatchId",
table: "StockMovements");
migrationBuilder.DropForeignKey(
name: "FK_StockMovements_ItemSerialNumbers_FK_ItemSerialNumberId",
table: "StockMovements");
migrationBuilder.DropTable(
name: "GoodsReceiptItemBatches");
migrationBuilder.DropTable(
name: "GoodsReceiptItemSerials");
migrationBuilder.DropTable(
name: "ItemBatches");
migrationBuilder.DropTable(
name: "ItemSerialNumbers");
migrationBuilder.DropIndex(
name: "IX_StockMovements_FK_ItemBatchId",
table: "StockMovements");
migrationBuilder.DropIndex(
name: "IX_StockMovements_FK_ItemSerialNumberId",
table: "StockMovements");
migrationBuilder.DropIndex(
name: "IX_StockBalances_FK_ItemBatchId",
table: "StockBalances");
// Recreate the old 2-column index before dropping the composite one, since MySQL
// won't drop an index that's the only one backing the FK_ItemId FK constraint.
migrationBuilder.CreateIndex(
name: "IX_StockBalances_FK_ItemId_LocationId",
table: "StockBalances",
columns: new[] { "FK_ItemId", "LocationId" },
unique: true);
migrationBuilder.DropIndex(
name: "IX_StockBalances_FK_ItemId_LocationId_FK_ItemBatchId_FK_ItemSer~",
table: "StockBalances");
migrationBuilder.DropIndex(
name: "IX_StockBalances_FK_ItemSerialNumberId",
table: "StockBalances");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.BatchTracking");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.CustomWarrantyOverride");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.DefaultWarrantyPeriodDays");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.ExpiryDateTracking");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.SerialNumberTracking");
migrationBuilder.DeleteData(
table: "SysConfigs",
keyColumn: "Key",
keyValue: "Feature.WarrantyTracking");
migrationBuilder.DropColumn(
name: "FK_ItemBatchId",
table: "StockMovements");
migrationBuilder.DropColumn(
name: "FK_ItemSerialNumberId",
table: "StockMovements");
migrationBuilder.DropColumn(
name: "FK_ItemBatchId",
table: "StockBalances");
migrationBuilder.DropColumn(
name: "FK_ItemSerialNumberId",
table: "StockBalances");
migrationBuilder.DropColumn(
name: "AllowCustomWarrantyPeriod",
table: "Items");
migrationBuilder.DropColumn(
name: "HasWarranty",
table: "Items");
migrationBuilder.DropColumn(
name: "TracksBatches",
table: "Items");
migrationBuilder.DropColumn(
name: "TracksExpiryDate",
table: "Items");
migrationBuilder.DropColumn(
name: "TracksSerialNumbers",
table: "Items");
migrationBuilder.DropColumn(
name: "WarrantyPeriodDays",
table: "Items");
}
}
}