fixes: MRN generation race condition and Sepsis bundle creation race condition (TOCTOU)

This commit is contained in:
voltsrage
2026-06-21 16:37:22 +08:00
parent 4583bd7f44
commit 076e8364b2
8 changed files with 2635 additions and 7 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace VigilCareClinicalAPI.Migrations
{
/// <inheritdoc />
public partial class AddMrnSequence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
DO $$
DECLARE
next_val bigint;
BEGIN
SELECT COALESCE(MAX(
CASE WHEN mrn ~ '^MRN-[0-9]+$'
THEN CAST(SUBSTRING(mrn FROM 5) AS bigint)
ELSE 0
END
), 0) + 1 INTO next_val FROM patients;
EXECUTE format('CREATE SEQUENCE mrn_seq START WITH %s INCREMENT BY 1', next_val);
END $$;
");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("DROP SEQUENCE IF EXISTS mrn_seq;");
}
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace VigilCareClinicalAPI.Migrations
{
/// <inheritdoc />
public partial class AddSepsisBundleUniqueInProgressIndex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"
CREATE UNIQUE INDEX ix_sepsis_bundles_encounter_in_progress
ON sepsis_bundles (encounter_id)
WHERE compliance_status = 'IN_PROGRESS';
");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("DROP INDEX IF EXISTS ix_sepsis_bundles_encounter_in_progress;");
}
}
}