158 lines
7.3 KiB
C#
158 lines
7.3 KiB
C#
using System;
|
|
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Distribution.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class AddInvoiceVersionsAndLineEdits : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.AlterColumn<Guid>(
|
|
name: "FK_ShopPOItemId",
|
|
table: "InvoiceItems",
|
|
type: "char(36)",
|
|
nullable: true,
|
|
collation: "ascii_general_ci",
|
|
oldClrType: typeof(Guid),
|
|
oldType: "char(36)")
|
|
.OldAnnotation("Relational:Collation", "ascii_general_ci");
|
|
|
|
migrationBuilder.AddColumn<bool>(
|
|
name: "IsRemoved",
|
|
table: "InvoiceItems",
|
|
type: "tinyint(1)",
|
|
nullable: false,
|
|
defaultValue: false);
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "InvoiceVersions",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_InvoiceId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
VersionNumber = table.Column<int>(type: "int", nullable: false),
|
|
Stage = table.Column<int>(type: "int", nullable: false),
|
|
TotalAmount = table.Column<decimal>(type: "decimal(65,30)", nullable: false),
|
|
CreatedBy = 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_InvoiceVersions", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_InvoiceVersions_Invoices_FK_InvoiceId",
|
|
column: x => x.FK_InvoiceId,
|
|
principalTable: "Invoices",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
})
|
|
.Annotation("MySql:CharSet", "utf8mb4");
|
|
|
|
migrationBuilder.CreateTable(
|
|
name: "InvoiceVersionItems",
|
|
columns: table => new
|
|
{
|
|
Id = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_InvoiceVersionId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
FK_ShopPOItemId = table.Column<Guid>(type: "char(36)", nullable: true, collation: "ascii_general_ci"),
|
|
FK_ItemId = table.Column<Guid>(type: "char(36)", nullable: false, collation: "ascii_general_ci"),
|
|
Quantity = table.Column<int>(type: "int", nullable: false),
|
|
UnitPrice = table.Column<decimal>(type: "decimal(65,30)", nullable: false),
|
|
IsRemoved = table.Column<bool>(type: "tinyint(1)", 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_InvoiceVersionItems", x => x.Id);
|
|
table.ForeignKey(
|
|
name: "FK_InvoiceVersionItems_InvoiceVersions_FK_InvoiceVersionId",
|
|
column: x => x.FK_InvoiceVersionId,
|
|
principalTable: "InvoiceVersions",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
table.ForeignKey(
|
|
name: "FK_InvoiceVersionItems_Items_FK_ItemId",
|
|
column: x => x.FK_ItemId,
|
|
principalTable: "Items",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
table.ForeignKey(
|
|
name: "FK_InvoiceVersionItems_ShopPOItems_FK_ShopPOItemId",
|
|
column: x => x.FK_ShopPOItemId,
|
|
principalTable: "ShopPOItems",
|
|
principalColumn: "Id",
|
|
onDelete: ReferentialAction.Restrict);
|
|
})
|
|
.Annotation("MySql:CharSet", "utf8mb4");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_InvoiceVersionItems_FK_InvoiceVersionId",
|
|
table: "InvoiceVersionItems",
|
|
column: "FK_InvoiceVersionId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_InvoiceVersionItems_FK_ItemId",
|
|
table: "InvoiceVersionItems",
|
|
column: "FK_ItemId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_InvoiceVersionItems_FK_ShopPOItemId",
|
|
table: "InvoiceVersionItems",
|
|
column: "FK_ShopPOItemId");
|
|
|
|
migrationBuilder.CreateIndex(
|
|
name: "IX_InvoiceVersions_FK_InvoiceId_VersionNumber",
|
|
table: "InvoiceVersions",
|
|
columns: new[] { "FK_InvoiceId", "VersionNumber" },
|
|
unique: true);
|
|
|
|
// Backfill: one baseline version per existing invoice from its current lines.
|
|
migrationBuilder.Sql(@"
|
|
INSERT INTO InvoiceVersions (Id, FK_InvoiceId, VersionNumber, Stage, TotalAmount, CreatedBy, CreatedAt, IsActive)
|
|
SELECT UUID(), inv.Id, 1,
|
|
CASE inv.Status WHEN 0 THEN 0 WHEN 1 THEN 2 ELSE 3 END,
|
|
inv.TotalAmount, inv.CreatedBy, NOW(6), 1
|
|
FROM Invoices inv;");
|
|
|
|
migrationBuilder.Sql(@"
|
|
INSERT INTO InvoiceVersionItems (Id, FK_InvoiceVersionId, FK_ShopPOItemId, FK_ItemId, Quantity, UnitPrice, IsRemoved, CreatedAt, IsActive)
|
|
SELECT UUID(), v.Id, ii.FK_ShopPOItemId, ii.FK_ItemId, ii.Quantity, ii.UnitPrice, ii.IsRemoved, NOW(6), 1
|
|
FROM InvoiceVersions v
|
|
JOIN InvoiceItems ii ON ii.FK_InvoiceId = v.FK_InvoiceId AND ii.IsActive = 1
|
|
WHERE v.VersionNumber = 1;");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.DropTable(
|
|
name: "InvoiceVersionItems");
|
|
|
|
migrationBuilder.DropTable(
|
|
name: "InvoiceVersions");
|
|
|
|
migrationBuilder.DropColumn(
|
|
name: "IsRemoved",
|
|
table: "InvoiceItems");
|
|
|
|
migrationBuilder.AlterColumn<Guid>(
|
|
name: "FK_ShopPOItemId",
|
|
table: "InvoiceItems",
|
|
type: "char(36)",
|
|
nullable: false,
|
|
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
|
|
collation: "ascii_general_ci",
|
|
oldClrType: typeof(Guid),
|
|
oldType: "char(36)",
|
|
oldNullable: true)
|
|
.OldAnnotation("Relational:Collation", "ascii_general_ci");
|
|
}
|
|
}
|
|
}
|