using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace backend.Migrations { /// public partial class UpdateTrackingCodeLength : Migration { /// 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( name: "TrackingCode", table: "Users", type: "nvarchar(4)", maxLength: 4, nullable: false, oldClrType: typeof(string), oldType: "nvarchar(8)", oldMaxLength: 8); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.AlterColumn( name: "TrackingCode", table: "Users", type: "nvarchar(8)", maxLength: 8, nullable: false, oldClrType: typeof(string), oldType: "nvarchar(4)", oldMaxLength: 4); } } }