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 // Handle SpecialSong relations separately
if (effectiveSpecialIds !== undefined) { if (effectiveSpecialIds !== undefined) {
// First, get current special assignments // First, get current special assignments (within transaction)
const currentSpecials = await prisma.specialSong.findMany({ const currentSpecials = await tx.specialSong.findMany({
where: { songId: Number(id) } where: { songId: Number(id) }
}); });
@@ -482,7 +484,7 @@ export async function PUT(request: Request) {
// Delete removed specials // Delete removed specials
const toDelete = currentSpecialIds.filter(sid => !newSpecialIds.includes(sid)); const toDelete = currentSpecialIds.filter(sid => !newSpecialIds.includes(sid));
if (toDelete.length > 0) { if (toDelete.length > 0) {
await prisma.specialSong.deleteMany({ await tx.specialSong.deleteMany({
where: { where: {
songId: Number(id), songId: Number(id),
specialId: { in: toDelete } specialId: { in: toDelete }
@@ -493,7 +495,7 @@ export async function PUT(request: Request) {
// Add new specials // Add new specials
const toAdd = newSpecialIds.filter(sid => !currentSpecialIds.includes(sid)); const toAdd = newSpecialIds.filter(sid => !currentSpecialIds.includes(sid));
if (toAdd.length > 0) { if (toAdd.length > 0) {
await prisma.specialSong.createMany({ await tx.specialSong.createMany({
data: toAdd.map(specialId => ({ data: toAdd.map(specialId => ({
songId: Number(id), songId: Number(id),
specialId, 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) }, where: { id: Number(id) },
data, data,
include: { include: {
@@ -515,6 +518,7 @@ export async function PUT(request: Request) {
} }
} }
}); });
});
return NextResponse.json(updatedSong); return NextResponse.json(updatedSong);
} catch (error) { } 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); const filePath = path.join(process.cwd(), 'public/uploads', song.filename);
try { try {
await unlink(filePath); await unlink(filePath);
@@ -578,10 +582,12 @@ export async function DELETE(request: Request) {
} }
} }
// Delete from database (will cascade delete related puzzles) // Delete from database in transaction (will cascade delete related puzzles, SpecialSong, etc.)
await prisma.song.delete({ await prisma.$transaction(async (tx) => {
await tx.song.delete({
where: { id: Number(id) }, where: { id: Number(id) },
}); });
});
return NextResponse.json({ success: true }); return NextResponse.json({ success: true });
} catch (error) { } catch (error) {