58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace backend.Migrations
|
|
{
|
|
/// <inheritdoc />
|
|
public partial class UpdateTrackingCodeLength : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
// First, update existing tracking codes to 4-digit numeric values
|
|
// Using a hash-based approach to generate unique 4-digit codes from existing ones
|
|
migrationBuilder.Sql(@"
|
|
DECLARE @Id int, @RowNum int, @NewCode nvarchar(4)
|
|
DECLARE code_cursor CURSOR FOR
|
|
SELECT Id, ROW_NUMBER() OVER (ORDER BY Id) AS RowNum
|
|
FROM Users
|
|
OPEN code_cursor
|
|
FETCH NEXT FROM code_cursor INTO @Id, @RowNum
|
|
WHILE @@FETCH_STATUS = 0
|
|
BEGIN
|
|
SET @NewCode = RIGHT('0000' + CAST((@RowNum + 1000) AS nvarchar(4)), 4)
|
|
UPDATE Users SET TrackingCode = @NewCode WHERE Id = @Id
|
|
FETCH NEXT FROM code_cursor INTO @Id, @RowNum
|
|
END
|
|
CLOSE code_cursor
|
|
DEALLOCATE code_cursor
|
|
");
|
|
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "TrackingCode",
|
|
table: "Users",
|
|
type: "nvarchar(4)",
|
|
maxLength: 4,
|
|
nullable: false,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(8)",
|
|
oldMaxLength: 8);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.AlterColumn<string>(
|
|
name: "TrackingCode",
|
|
table: "Users",
|
|
type: "nvarchar(8)",
|
|
maxLength: 8,
|
|
nullable: false,
|
|
oldClrType: typeof(string),
|
|
oldType: "nvarchar(4)",
|
|
oldMaxLength: 4);
|
|
}
|
|
}
|
|
}
|