Add region code
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using Distribution.Data;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Distribution.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
[DbContext(typeof(AppDBContext))]
|
||||
[Migration("20261006100000_AddRegionCode")]
|
||||
public partial class AddRegionCode : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "Code",
|
||||
table: "Regions",
|
||||
type: "varchar(50)",
|
||||
maxLength: 50,
|
||||
nullable: false,
|
||||
defaultValue: "");
|
||||
|
||||
// Backfill existing rows (seeded regions first) with REG-0001, REG-0002, ... before the unique index.
|
||||
migrationBuilder.Sql(@"
|
||||
UPDATE `Regions` r
|
||||
JOIN (
|
||||
SELECT `Id`, ROW_NUMBER() OVER (
|
||||
ORDER BY CASE WHEN `Id` LIKE '24000000-%' THEN 0 ELSE 1 END, `CreatedAt`, `Id`) AS rn
|
||||
FROM `Regions`
|
||||
) x ON r.`Id` = x.`Id`
|
||||
SET r.`Code` = CONCAT('REG-', LPAD(x.rn, 4, '0'));");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Regions_Code",
|
||||
table: "Regions",
|
||||
column: "Code",
|
||||
unique: true);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_Regions_Code",
|
||||
table: "Regions");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "Code",
|
||||
table: "Regions");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1474,6 +1474,11 @@ namespace Distribution.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("char(36)");
|
||||
|
||||
b.Property<string>("Code")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime(6)");
|
||||
|
||||
@@ -1486,6 +1491,9 @@ namespace Distribution.Migrations
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("Code")
|
||||
.IsUnique();
|
||||
|
||||
b.ToTable("Regions");
|
||||
|
||||
b.HasData(
|
||||
@@ -1494,6 +1502,7 @@ namespace Distribution.Migrations
|
||||
Id = new Guid("24000000-0000-0000-0000-000000000001"),
|
||||
CreatedAt = new DateTime(2026, 8, 20, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
IsActive = true,
|
||||
Code = "REG-0001",
|
||||
Name = "Western Province"
|
||||
},
|
||||
new
|
||||
@@ -1501,6 +1510,7 @@ namespace Distribution.Migrations
|
||||
Id = new Guid("24000000-0000-0000-0000-000000000002"),
|
||||
CreatedAt = new DateTime(2026, 8, 20, 0, 0, 0, 0, DateTimeKind.Unspecified),
|
||||
IsActive = true,
|
||||
Code = "REG-0002",
|
||||
Name = "Central Province"
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user