55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
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");
|
|
}
|
|
}
|
|
}
|