Wrap song updates and deletes in database transactions for consistency

This commit is contained in:
Hördle Bot
2025-12-03 18:36:32 +01:00
parent 71abb7c322
commit 8ecf430bf5

View File

@@ -469,10 +469,12 @@ export async function PUT(request: Request) {
};
}
// Execute all database write operations in a transaction to ensure consistency
const updatedSong = await prisma.$transaction(async (tx) => {
// Handle SpecialSong relations separately
if (effectiveSpecialIds !== undefined) {
// First, get current special assignments
const currentSpecials = await prisma.specialSong.findMany({
// First, get current special assignments (within transaction)
const currentSpecials = await tx.specialSong.findMany({
where: { songId: Number(id) }
});
@@ -482,7 +484,7 @@ export async function PUT(request: Request) {
// Delete removed specials
const toDelete = currentSpecialIds.filter(sid => !newSpecialIds.includes(sid));
if (toDelete.length > 0) {
await prisma.specialSong.deleteMany({
await tx.specialSong.deleteMany({
where: {
songId: Number(id),
specialId: { in: toDelete }
@@ -493,7 +495,7 @@ export async function PUT(request: Request) {
// Add new specials
const toAdd = newSpecialIds.filter(sid => !currentSpecialIds.includes(sid));
if (toAdd.length > 0) {
await prisma.specialSong.createMany({
await tx.specialSong.createMany({
data: toAdd.map(specialId => ({
songId: Number(id),
specialId,
@@ -503,7 +505,8 @@ export async function PUT(request: Request) {
}
}
const updatedSong = await prisma.song.update({
// Update song (this also handles genre relations via Prisma's set operation)
return await tx.song.update({
where: { id: Number(id) },
data,
include: {
@@ -515,6 +518,7 @@ export async function PUT(request: Request) {
}
}
});
});
return NextResponse.json(updatedSong);
} catch (error) {
@@ -559,7 +563,7 @@ export async function DELETE(request: Request) {
}
}
// Delete file
// Delete files first (outside transaction, as file system operations can't be rolled back)
const filePath = path.join(process.cwd(), 'public/uploads', song.filename);
try {
await unlink(filePath);
@@ -578,10 +582,12 @@ export async function DELETE(request: Request) {
}
}
// Delete from database (will cascade delete related puzzles)
await prisma.song.delete({
// Delete from database in transaction (will cascade delete related puzzles, SpecialSong, etc.)
await prisma.$transaction(async (tx) => {
await tx.song.delete({
where: { id: Number(id) },
});
});
return NextResponse.json({ success: true });
} catch (error) {