issue #2551
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 2m58s

This commit is contained in:
Suphonchai Phoonsawat 2026-06-19 10:51:18 +07:00
parent 71966eb4e9
commit 2cdae3578e
7 changed files with 1996 additions and 40 deletions

View file

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
{
/// <inheritdoc />
public partial class AddUniqueIndexLeaveBeginningProfileIdYearTypeId : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "IX_LeaveBeginnings_ProfileId_LeaveYear_LeaveTypeId",
table: "LeaveBeginnings",
columns: new[] { "ProfileId", "LeaveYear", "LeaveTypeId" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_LeaveBeginnings_ProfileId_LeaveYear_LeaveTypeId",
table: "LeaveBeginnings");
}
}
}

View file

@ -226,6 +226,10 @@ namespace BMA.EHR.Infrastructure.Migrations.LeaveDb
b.HasIndex("LeaveTypeId");
b.HasIndex("ProfileId", "LeaveYear", "LeaveTypeId")
.IsUnique()
.HasDatabaseName("IX_LeaveBeginnings_ProfileId_LeaveYear_LeaveTypeId");
b.ToTable("LeaveBeginnings");
});

View file

@ -61,5 +61,18 @@ namespace BMA.EHR.Infrastructure.Persistence
{
base.Entry(entity).State = EntityState.Detached;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Composite unique index on the natural key for LeaveBeginning.
// Prevents duplicate rows when concurrent requests (e.g. UI calling /user/check twice)
// race through the get-or-create flow in LeaveBeginningRepository.
modelBuilder.Entity<LeaveBeginning>()
.HasIndex(b => new { b.ProfileId, b.LeaveYear, b.LeaveTypeId })
.HasDatabaseName("IX_LeaveBeginnings_ProfileId_LeaveYear_LeaveTypeId")
.IsUnique();
}
}
}