Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,23 @@ export async function deleteByProject(
projectId: number
): Promise<Result<{ deletedCount: number }>> {
try {
return await db.transaction(async (tx) => {
const [projectUnit] = await tx
.select({ id: project_units.id })
.from(project_units)
.where(eq(project_units.projectId, projectId))
.limit(1);

if (!projectUnit) return ok({ deletedCount: 0 });

const deletedAssignments = await tx
.delete(chapter_assignments)
.where(eq(chapter_assignments.projectUnitId, projectUnit.id))
.returning({ id: chapter_assignments.id });

return ok({ deletedCount: deletedAssignments.length });
});
// Delete assignments across all of the project's units in one statement.
// (Previously this selected a single unit with limit(1) and deleted only its
// assignments, orphaning the rest if a project ever had more than one unit.)
const deletedAssignments = await db
.delete(chapter_assignments)
.where(
inArray(
chapter_assignments.projectUnitId,
db
.select({ id: project_units.id })
.from(project_units)
.where(eq(project_units.projectId, projectId))
)
)
.returning({ id: chapter_assignments.id });

return ok({ deletedCount: deletedAssignments.length });
} catch (error) {
logger.error({
cause: error,
Expand Down
Loading