优化考试排考

This commit is contained in:
2026-07-27 19:31:19 +08:00 Unverified
parent 64decdab7e
commit c3e8a42641
14 changed files with 7013 additions and 272 deletions
@@ -64,6 +64,8 @@ public sealed class DevelopmentSqliteMigrator(
"20260727_34_course_adjustment_occurrences";
private const string AcademicPlanningPrerequisitesMigration =
"20260727_35_academic_planning_prerequisites";
private const string ExamRoomMixingMigration =
"20260727_36_exam_room_mixing";
public async Task MigrateAsync(CancellationToken cancellationToken = default)
{
@@ -468,6 +470,19 @@ public sealed class DevelopmentSqliteMigrator(
AcademicPlanningPrerequisitesMigration,
coursePrerequisitesExist ? [] : AcademicPlanningPrerequisiteStatements,
cancellationToken);
var examRoomsExist = await db.Database
.SqlQueryRaw<int>(
"""
SELECT COUNT(*) AS "Value"
FROM sqlite_master
WHERE type = 'table' AND name = 'ExamRooms'
""")
.AnyAsync(value => value > 0, cancellationToken);
await ApplyMigrationAsync(
ExamRoomMixingMigration,
examRoomsExist ? [] : ExamRoomMixingStatements,
cancellationToken);
}
private async Task ApplyMigrationAsync(
@@ -2132,4 +2147,112 @@ public sealed class DevelopmentSqliteMigrator(
ON "CoursePrerequisites" ("PrerequisiteCourseId");
"""
];
private static readonly string[] ExamRoomMixingStatements =
[
"""
CREATE TABLE "ExamRooms" (
"Id" TEXT NOT NULL CONSTRAINT "PK_ExamRooms" PRIMARY KEY,
"ExamPlanId" TEXT NOT NULL,
"CourseId" TEXT NOT NULL,
"ClassroomId" TEXT NOT NULL,
"ExamDate" TEXT NOT NULL,
"StartPeriod" INTEGER NOT NULL,
"PeriodCount" INTEGER NOT NULL,
"StartsAt" TEXT NOT NULL,
"EndsAt" TEXT NOT NULL,
"RequiredInvigilatorCount" INTEGER NOT NULL,
"CreatedAt" TEXT NOT NULL,
"UpdatedAt" TEXT NOT NULL,
CONSTRAINT "FK_ExamRooms_ExamPlans_ExamPlanId"
FOREIGN KEY ("ExamPlanId") REFERENCES "ExamPlans" ("Id")
ON DELETE CASCADE,
CONSTRAINT "FK_ExamRooms_Courses_CourseId"
FOREIGN KEY ("CourseId") REFERENCES "Courses" ("Id")
ON DELETE RESTRICT,
CONSTRAINT "FK_ExamRooms_Classrooms_ClassroomId"
FOREIGN KEY ("ClassroomId") REFERENCES "Classrooms" ("Id")
ON DELETE RESTRICT
);
""",
"""
CREATE INDEX "IX_ExamRooms_Plan_Time"
ON "ExamRooms" ("ExamPlanId", "StartsAt");
""",
"""
CREATE INDEX "IX_ExamRooms_Plan_Room_Time"
ON "ExamRooms" ("ExamPlanId", "ClassroomId", "StartsAt");
""",
"""
CREATE INDEX "IX_ExamRooms_CourseId"
ON "ExamRooms" ("CourseId");
""",
"""
CREATE INDEX "IX_ExamRooms_ClassroomId"
ON "ExamRooms" ("ClassroomId");
""",
"""
CREATE TABLE "ExamRoomSessions" (
"ExamRoomId" TEXT NOT NULL,
"ExamSessionId" TEXT NOT NULL,
CONSTRAINT "PK_ExamRoomSessions"
PRIMARY KEY ("ExamRoomId", "ExamSessionId"),
CONSTRAINT "FK_ExamRoomSessions_ExamRooms_ExamRoomId"
FOREIGN KEY ("ExamRoomId") REFERENCES "ExamRooms" ("Id")
ON DELETE CASCADE,
CONSTRAINT "FK_ExamRoomSessions_ExamSessions_ExamSessionId"
FOREIGN KEY ("ExamSessionId") REFERENCES "ExamSessions" ("Id")
ON DELETE RESTRICT
);
""",
"""
CREATE INDEX "IX_ExamRoomSessions_SessionId"
ON "ExamRoomSessions" ("ExamSessionId");
""",
"""
CREATE TABLE "ExamSeats" (
"ExamRoomId" TEXT NOT NULL,
"ExamSessionId" TEXT NOT NULL,
"StudentId" TEXT NOT NULL,
"SeatNumber" INTEGER NOT NULL,
CONSTRAINT "PK_ExamSeats"
PRIMARY KEY ("ExamRoomId", "StudentId"),
CONSTRAINT "FK_ExamSeats_ExamRooms_ExamRoomId"
FOREIGN KEY ("ExamRoomId") REFERENCES "ExamRooms" ("Id")
ON DELETE CASCADE,
CONSTRAINT "FK_ExamSeats_ExamSessions_ExamSessionId"
FOREIGN KEY ("ExamSessionId") REFERENCES "ExamSessions" ("Id")
ON DELETE RESTRICT,
CONSTRAINT "FK_ExamSeats_Students_StudentId"
FOREIGN KEY ("StudentId") REFERENCES "Students" ("Id")
ON DELETE RESTRICT
);
""",
"""
CREATE UNIQUE INDEX "UX_ExamSeats_Session_Student"
ON "ExamSeats" ("ExamSessionId", "StudentId");
""",
"""
CREATE INDEX "IX_ExamSeats_StudentId"
ON "ExamSeats" ("StudentId");
""",
"""
CREATE TABLE "ExamRoomInvigilators" (
"ExamRoomId" TEXT NOT NULL,
"TeacherId" TEXT NOT NULL,
CONSTRAINT "PK_ExamRoomInvigilators"
PRIMARY KEY ("ExamRoomId", "TeacherId"),
CONSTRAINT "FK_ExamRoomInvigilators_ExamRooms_ExamRoomId"
FOREIGN KEY ("ExamRoomId") REFERENCES "ExamRooms" ("Id")
ON DELETE CASCADE,
CONSTRAINT "FK_ExamRoomInvigilators_Teachers_TeacherId"
FOREIGN KEY ("TeacherId") REFERENCES "Teachers" ("Id")
ON DELETE RESTRICT
);
""",
"""
CREATE INDEX "IX_ExamRoomInvigilators_TeacherId"
ON "ExamRoomInvigilators" ("TeacherId");
"""
];
}